Struct async_once_cell::unpin::Lazy
source · pub struct Lazy<T, F = Pin<Box<dyn Future<Output = T> + Send>>> { /* private fields */ }
Expand description
A value which is initialized on the first access.
See ConstLazy if you need to initialize in a const context.
use std::sync::Arc;
use async_once_cell::unpin::Lazy;
let shared = Arc::new(Lazy::new(async {
4
}));
let value : &i32 = shared.get().await;
assert_eq!(value, &4);
You can also call await
on a reference:
use async_once_cell::unpin::Lazy;
struct Foo {
value: Lazy<i32>,
}
let foo = Foo {
value : Lazy::new(Box::pin(async { 4 })),
};
assert_eq!((&foo.value).await, &4);
Implementations§
source§impl<T, F> Lazy<T, F>
impl<T, F> Lazy<T, F>
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(&mut self) -> Option<&mut T>
pub fn try_get_mut(&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_value(self) -> Option<T>
pub fn into_value(self) -> Option<T>
Gets the value if it was set.