pub struct OnceCell<T> { /* private fields */ }
Expand description

A cell which can be written to only once.

This allows initialization using an async closure that borrows from its environment.

use std::rc::Rc;
use std::sync::Arc;
use async_once_cell::OnceCell;

let non_send_value = Rc::new(4);
let shared = Arc::new(OnceCell::new());

let value : &i32 = shared.get_or_init(async {
    *non_send_value
}).await;
assert_eq!(value, &4);

// A second init is not called
let second = shared.get_or_init(async {
    unreachable!()
}).await;
assert_eq!(second, &4);

Implementations§

source§

impl<T> OnceCell<T>

source

pub const fn new() -> Self

Creates a new empty cell.

source

pub const fn new_with(value: T) -> Self

Creates a new cell with the given contents.

source

pub async fn get_or_init(&self, init: impl Future<Output = T>) -> &T

Gets the contents of the cell, initializing it with init if the cell was empty.

Many tasks may call get_or_init concurrently with different initializing futures, but it is guaranteed that only one future will be executed as long as the resulting future is polled to completion.

If init panics, the panic is propagated to the caller, and the cell remains uninitialized.

If the Future returned by this function is dropped prior to completion, the cell remains uninitialized, and another init function will be started (if any are available).

Attempting to reentrantly initialize the cell from init will generally cause a deadlock; the reentrant call will immediately yield and wait for the pending initialization. If the actual initialization can complete despite this (for example, by polling multiple futures and discarding incomplete ones instead of polling them to completion), then the cell will successfully be initialized.

source

pub async fn get_or_try_init<E>( &self, init: impl Future<Output = Result<T, E>> ) -> Result<&T, E>

Gets the contents of the cell, initializing it with init if the cell was empty. If the cell was empty and init failed, an error is returned.

Many tasks may call get_or_init and/or get_or_try_init concurrently with different initializing futures, but it is guaranteed that only one of the futures will be executed as long as the resulting future is polled to completion.

If init panics or returns an error, the panic or error is propagated to the caller, and the cell remains uninitialized. In this case, another init function from a concurrent caller will be selected to execute, if one is available.

If the Future returned by this function is dropped prior to completion, the cell remains uninitialized, and another init function will be started (if any are available).

Attempting to reentrantly initialize the cell from init will generally cause a deadlock; the reentrant call will immediately yield and wait for the pending initialization. If the actual initialization can complete despite this (for example, by polling multiple futures and discarding incomplete ones instead of polling them to completion), then the cell will successfully be initialized.

source

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

Gets the reference to the underlying value.

Returns None if the cell is empty or being initialized. This method never blocks.

source

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

Gets a mutable reference to the underlying value.

source

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

Takes the value out of this OnceCell, moving it back to an uninitialized state.

source

pub fn into_inner(self) -> Option<T>

Consumes the OnceCell, returning the wrapped value. Returns None if the cell was empty.

Trait Implementations§

source§

impl<T: Debug> Debug for OnceCell<T>

source§

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

Formats the value using the given formatter. Read more
source§

impl<T> Default for OnceCell<T>

source§

fn default() -> Self

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

impl<T> Drop for OnceCell<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> From<T> for OnceCell<T>

source§

fn from(value: T) -> Self

Converts to this type from the input type.
source§

impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceCell<T>

source§

impl<T: Send> Send for OnceCell<T>

source§

impl<T: Sync + Send> Sync for OnceCell<T>

source§

impl<T> Unpin for OnceCell<T>

source§

impl<T: UnwindSafe> UnwindSafe for OnceCell<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.