Struct async_oncecell::OnceCell[][src]

pub struct OnceCell<T> { /* fields omitted */ }

Cell which can be lazy instantiated with an asynchronous block and is safely share-able between threads.

Implementations

impl<T> OnceCell<T>[src]

pub fn new() -> Self[src]

Creates a new empty OnceCell. Currently this function is not const due to Mutex limitations, so to share between multiple threads an Arc needs to be used.

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

Get or initialize this cell with the given asynchronous block. If the cell is already initialized, the current value will be returned. Otherwise the asynchronous block is used to initialize the OnceCell. This function will always return a value.

Example

let cell = OnceCell::new();
cell.get_or_init(async {
    0 // expensive calculation
}).await;
assert_eq!(cell.get(), Some(&0));

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

Get or initialize this cell with the given asynchronous block. If the cell is already initialized, the current value will be returned. Otherwise the asynchronous block is used to initialize the OnceCell. This function will always return a value.

Example

let cell = OnceCell::new();
cell.get_or_try_init(async {
    Ok::<_, Infallible>(0) // expensive calculation
}).await;
assert_eq!(cell.get(), Some(&0));

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

Returns the value of the OnceCell or None if the OnceCell has not been initialized yet.

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

If the OnceCell is already initialized, either by having have called get_or_init() or get_or_try_init().

Trait Implementations

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

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

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

impl<T: PartialEq> PartialEq<OnceCell<T>> 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> where
    T: UnwindSafe

Blanket Implementations

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

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

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

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

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

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.

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.