Struct async_once_cell::Lazy
source · pub struct Lazy<T, F> { /* private fields */ }
Expand description
A value which is computed on demand by running a future.
Unlike OnceCell, if a task is cancelled, the initializing future’s execution will be continued by other (concurrent or future) callers of Lazy::get.
use std::sync::Arc;
use async_once_cell::Lazy;
struct Data {
id: u32,
}
let shared = Arc::pin(Lazy::new(async move {
Data { id: 4 }
}));
assert_eq!(shared.as_ref().get().await.id, 4);
Implementations§
source§impl<T, F> Lazy<T, F>where
F: Future<Output = T>,
impl<T, F> Lazy<T, F>where F: Future<Output = T>,
source§impl<T, F> Lazy<T, F>
impl<T, F> Lazy<T, F>
sourcepub const fn from_future(future: F) -> Self
pub const fn from_future(future: F) -> Self
Creates a new lazy value with the given initializing future.
This is equivalent to Self::new but with no type bound.
sourcepub const fn with_value(value: T) -> Self
pub const fn with_value(value: T) -> Self
Creates an already-initialized lazy value.
sourcepub fn try_get(&self) -> Option<&T>
pub fn try_get(&self) -> Option<&T>
Gets the value without blocking or starting the initialization.
sourcepub fn try_get_mut(self: Pin<&mut Self>) -> Option<Pin<&mut T>>
pub fn try_get_mut(self: Pin<&mut Self>) -> Option<Pin<&mut T>>
Gets the value without blocking or starting the initialization.
This requires mutable access to self, so rust’s aliasing rules prevent any concurrent access and allow violating the usual rules for accessing this cell.
sourcepub fn try_get_mut_unpin(&mut self) -> Option<&mut T>
pub fn try_get_mut_unpin(&mut self) -> Option<&mut T>
Gets the value without blocking or starting the initialization.
This requires mutable access to self, so rust’s aliasing rules prevent any concurrent access and allow violating the usual rules for accessing this cell.
sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Gets the value if it was set.