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::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::Lazy;
struct Foo {
    value: Lazy<i32>,
}

let foo = Foo {
    value : Lazy::new(Box::pin(async { 4 })),
};

assert_eq!((&foo.value).await, &4);

Implementations

Creates a new lazy value with the given initializing future.

Forces the evaluation of this lazy value and returns a reference to the result.

This is equivalent to the Future impl on &Lazy, but is explicit and may be simpler to call. This will panic if the initializing closure panics or has panicked.

Creates an already-initialized lazy value.

Gets the value without blocking or starting the initialization.

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.

Gets the value if it was set.

Trait Implementations

Formats the value using the given formatter. Read more

The type of value produced on completion.

Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.