Struct tokio::sync::OnceCell[][src]

pub struct OnceCell<T> { /* fields omitted */ }
This is supported on crate feature sync only.
Expand description

A thread-safe cell which can be written to only once.

Provides the functionality to either set the value, in case OnceCell is uninitialized, or get the already initialized value by using an async function via OnceCell::get_or_init.

Examples

use tokio::sync::OnceCell;

async fn some_computation() -> u32 {
    1 + 1
}

static ONCE: OnceCell<u32> = OnceCell::const_new();

#[tokio::main]
async fn main() {
    let result1 = ONCE.get_or_init(some_computation).await;
    assert_eq!(*result1, 2);
}

Implementations

Creates a new uninitialized OnceCell instance.

Creates a new initialized OnceCell instance if value is Some, otherwise has the same functionality as OnceCell::new.

This is supported on crate feature parking_lot only.

Creates a new uninitialized OnceCell instance.

Whether the value of the OnceCell is set or not.

Tries to get a reference to the value of the OnceCell.

Returns None if the value of the OnceCell hasn’t previously been initialized.

Tries to return a mutable reference to the value of the cell.

Returns None if the cell hasn’t previously been initialized.

Sets the value of the OnceCell to the argument value.

If the value of the OnceCell was already set prior to this call then SetError::AlreadyInitializedError is returned. If another thread is initializing the cell while this method is called, SetError::InitializingError is returned. In order to wait for an ongoing initialization to finish, call OnceCell::get_or_init instead.

Tries to initialize the value of the OnceCell using the async function f. If the value of the OnceCell was already initialized prior to this call, a reference to that initialized value is returned. If some other thread initiated the initialization prior to this call and the initialization hasn’t completed, this call waits until the initialization is finished.

This will deadlock if f tries to initialize the cell itself.

Tries to initialize the value of the OnceCell using the async function f. If the value of the OnceCell was already initialized prior to this call, a reference to that initialized value is returned. If some other thread initiated the initialization prior to this call and the initialization hasn’t completed, this call waits until the initialization is finished. If the function argument f returns an error, get_or_try_init returns that error, otherwise the result of f will be stored in the cell.

This will deadlock if f tries to initialize the cell itself.

Moves the value out of the cell, destroying the cell in the process.

Returns None if the cell is uninitialized.

Takes ownership of the current value, leaving the cell uninitialized.

Returns None if the cell is uninitialized.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Executes the destructor for this type. Read more

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

Performs the conversion.

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

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.