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

Creates a new lazy value with the given initializing future.

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

The Pin::static_ref function may be useful if this is a static value.

Creates a new lazy value with the given initializing future.

This is equivalent to Self::new but with no type bound.

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 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

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.