pub struct Lazy<T, Fut = Pin<Box<dyn Future<Output = T> + Send>>, F = fn() -> Fut> { /* private fields */ }
Expand description
A value that is initialized on the first access.
The initialization procedure is allowed to be asycnhronous,
so access to the value requires an await
.
§Example
use std::time::Duration;
use async_lazy::Lazy;
async fn some_computation() -> u32 {
tokio::time::sleep(Duration::from_secs(1)).await;
1 + 1
}
#[tokio::main]
async fn main() {
let lazy : Lazy<u32> = Lazy::new(|| Box::pin(async { some_computation().await }));
let result = tokio::spawn(async move {
*lazy.force().await
}).await.unwrap();
assert_eq!(result, 2);
}
Implementations§
Source§impl<T, Fut, F> Lazy<T, Fut, F>
impl<T, Fut, F> Lazy<T, Fut, F>
Sourcepub const fn new(init: F) -> Self
pub const fn new(init: F) -> Self
Creates a new Lazy
instance with the given initializing
async function.
Equivalent to Lazy::new
, except that it can be used in static
variables.
§Example
use std::time::Duration;
use async_lazy::Lazy;
async fn some_computation() -> u32 {
tokio::time::sleep(Duration::from_secs(1)).await;
1 + 1
}
static LAZY: Lazy<u32> = Lazy::new(|| Box::pin(async { some_computation().await }));
#[tokio::main]
async fn main() {
let result = tokio::spawn(async {
*LAZY.force().await
}).await.unwrap();
assert_eq!(result, 2);
}
Sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Gets a reference to the result of this lazy value if it was initialized,
otherwise returns None
.
Sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
Gets a mutable reference to the result of this lazy value if it was initialized,
otherwise returns None
.
Source§impl<T, Fut, F> Lazy<T, Fut, F>
impl<T, Fut, F> Lazy<T, Fut, F>
Sourcepub async fn force(&self) -> &T
pub async fn force(&self) -> &T
Forces the evaluation of this lazy value and returns a reference to the result.
If the caller of force
is cancelled, the state of initialization is preserved;
the next call to force
starts polling the initializing future again where the last left off.
§Panics
If the initialization function panics, the Lazy
is poisoned
and all subsequent calls to this function will panic.
Trait Implementations§
Source§impl<'a, T, Fut, F> IntoFuture for &'a Lazy<T, Fut, F>
Available on crate feature nightly
only.
impl<'a, T, Fut, F> IntoFuture for &'a Lazy<T, Fut, F>
nightly
only.Source§type IntoFuture = impl Future<Output = <&'a Lazy<T, Fut, F> as IntoFuture>::Output>
type IntoFuture = impl Future<Output = <&'a Lazy<T, Fut, F> as IntoFuture>::Output>
Source§fn into_future(self) -> Self::IntoFuture
fn into_future(self) -> Self::IntoFuture
Source§impl<'a, T, Fut, F> IntoFuture for &'a mut Lazy<T, Fut, F>
Available on crate feature nightly
only.
impl<'a, T, Fut, F> IntoFuture for &'a mut Lazy<T, Fut, F>
nightly
only.Source§type IntoFuture = impl Future<Output = <&'a mut Lazy<T, Fut, F> as IntoFuture>::Output>
type IntoFuture = impl Future<Output = <&'a mut Lazy<T, Fut, F> as IntoFuture>::Output>
Source§fn into_future(self) -> Self::IntoFuture
fn into_future(self) -> Self::IntoFuture
Source§impl<'a, T, Fut, F> IntoFuture for Pin<&'a Lazy<T, Fut, F>>
Available on crate feature nightly
only.
impl<'a, T, Fut, F> IntoFuture for Pin<&'a Lazy<T, Fut, F>>
nightly
only.Source§type IntoFuture = impl Future<Output = <Pin<&'a Lazy<T, Fut, F>> as IntoFuture>::Output>
type IntoFuture = impl Future<Output = <Pin<&'a Lazy<T, Fut, F>> as IntoFuture>::Output>
Source§fn into_future(self) -> Self::IntoFuture
fn into_future(self) -> Self::IntoFuture
Source§impl<'a, T, Fut, F> IntoFuture for Pin<&'a mut Lazy<T, Fut, F>>
Available on crate feature nightly
only.
impl<'a, T, Fut, F> IntoFuture for Pin<&'a mut Lazy<T, Fut, F>>
nightly
only.