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>
impl<T> OnceCell<T>
Sourcepub fn set(&self, value: T) -> Result<(), T>
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).
Sourcepub async fn get_or_init<F, Fut>(&self, init: F) -> &T
pub async fn get_or_init<F, Fut>(&self, init: F) -> &T
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.
Sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Consume the cell, returning its value if initialized.
Trait Implementations§
impl<T: Send> Send for OnceCell<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more