Skip to main content

OnceCell

Struct OnceCell 

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

A cell that’s initialized at most once, asynchronously.

Concurrent callers of OnceCell::get_or_init that race to be first all wait for the same single initialization to complete rather than each running their own initializer.

Implementations§

Source§

impl<T> OnceCell<T>

Source

pub const fn new() -> Self

Create an empty, uninitialized cell.

Source

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

Create a cell already initialized with value.

Source

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

The cell’s value, if it’s already been initialized.

Source

pub fn set(&self, value: T) -> Result<(), T>

Set the cell’s value if it isn’t already initialized.

§Errors

Returns value back if the cell was already initialized (or is concurrently being initialized by Self::get_or_init).

Source

pub async fn get_or_init<F, Fut>(&self, init: F) -> &T
where F: FnOnce() -> Fut, Fut: Future<Output = T>, T: Send + Sync,

Return the cell’s value, initializing it with (the result of awaiting) init first if necessary. Concurrent callers racing to initialize the same cell all observe the same initialization — only the winner’s init future actually runs.

Source

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

Consume the cell, returning its value if initialized.

Trait Implementations§

Source§

impl<T> Default for OnceCell<T>

Source§

fn default() -> Self

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

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

Source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for OnceCell<T>

§

impl<T> !RefUnwindSafe for OnceCell<T>

§

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

§

impl<T> UnsafeUnpin for OnceCell<T>
where T: UnsafeUnpin,

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.