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

impl<T> OnceCell<T>[src]

pub fn new() -> Self[src]

Creates a new uninitialized OnceCell instance.

pub fn new_with(value: Option<T>) -> Self[src]

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

pub const fn const_new() -> Self[src]

This is supported on crate feature parking_lot only.

Creates a new uninitialized OnceCell instance.

pub fn initialized(&self) -> bool[src]

Whether the value of the OnceCell is set or not.

pub fn get(&self) -> Option<&T>[src]

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

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

pub fn get_mut(&mut self) -> Option<&mut T>[src]

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

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

pub fn set(&self, value: T) -> Result<(), SetError<T>>[src]

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.

pub async fn get_or_init<F, Fut>(&self, f: F) -> &T where
    F: FnOnce() -> Fut,
    Fut: Future<Output = T>, 
[src]

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.

pub async fn get_or_try_init<E, F, Fut>(&self, f: F) -> Result<&T, E> where
    F: FnOnce() -> Fut,
    Fut: Future<Output = Result<T, E>>, 
[src]

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.

pub fn into_inner(self) -> Option<T>[src]

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

Returns None if the cell is uninitialized.

pub fn take(&mut self) -> Option<T>[src]

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

Returns None if the cell is uninitialized.

Trait Implementations

impl<T: Clone> Clone for OnceCell<T>[src]

fn clone(&self) -> OnceCell<T>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: Debug> Debug for OnceCell<T>[src]

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl<T> Default for OnceCell<T>[src]

fn default() -> OnceCell<T>[src]

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

impl<T> Drop for OnceCell<T>[src]

fn drop(&mut self)[src]

Executes the destructor for this type. Read more

impl<T: PartialEq> PartialEq<OnceCell<T>> for OnceCell<T>[src]

fn eq(&self, other: &OnceCell<T>) -> bool[src]

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

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl<T: Eq> Eq for OnceCell<T>[src]

impl<T: Send> Send for OnceCell<T>[src]

impl<T: Sync + Send> Sync for OnceCell<T>[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for OnceCell<T>

impl<T> Unpin for OnceCell<T> where
    T: Unpin

impl<T> !UnwindSafe for OnceCell<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

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

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.