pub struct OnceFuture<T, F = Pin<Box<dyn Future<Output = T> + Send>>, I = Infallible> { /* private fields */ }
Expand description

A Future which is executed exactly once, producing an output accessible without locking.

This is primarily used as a building block for Lazy and ConstLazy, but can also be used on its own similar to OnceCell.

use std::sync::Arc;
use async_once_cell::OnceFuture;

let shared = Arc::new(OnceFuture::new());
let value : &i32 = shared.get_or_init_with(|| async {
    4
}).await;
assert_eq!(value, &4);

Implementations

Creates a new OnceFuture with an initializing value

Creates a new OnceFuture without an initializing value

The resulting Future must be produced by the closure passed to Self::get_or_init_with. This function is identical to Self::new but is more likely to need type hints.

Creates a new OnceFuture that is immediately ready

Gets the value without blocking or starting the initialization.

Get mutable access to the initializer or final value.

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 initializer or final value

Creates a new OnceFuture without an initializing value

The resulting Future must be produced by the closure passed to get_or_init_with

Creates a new OnceFuture directly from a Future.

The gen_future or into_future closures will never be called.

Create and run the future until it produces a result, then return a reference to that result.

This is a convenience wrapper around OnceFuture::get_or_populate_with for use when the initializer value is not used or not present.

Create and run the future until it produces a result, then return a reference to that result.

Only one into_future closure will be called per OnceFuture instance, and only if the future was not already set by from_future.

Create and run the future until it produces a result, then return a reference to that result.

Only one into_future closure will be called per OnceFuture instance, and only if the future was not already set by from_future.

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.