OnceCell

Struct OnceCell 

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

Lock-free thread-safe cell which can be written to only once.

Implementations§

Source§

impl<T> OnceCell<T>

Source

pub const fn new() -> Self

Creates a new empty cell.

Source

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

Sets the contents of this cell to value.

Returns Ok(()) if the cell’s value was set by this call.

Source

pub fn set_with<F: FnOnce() -> T>(&self, ctor: F) -> Result<(), F>

Sets the contents of this cell to value returned by ctor call.

The ctor is called only if the cell’s value is going set by this call. Otherwice ctor returned in Err(..).

§Panics

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

Source

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

Gets the pointer to the underlying value.

Returns None if the cell is empty.

Source

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

Gets the reference to the underlying value.

Returns None if the cell is empty, or being initialized.

Source

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

Gets the mutable reference to the underlying value.

Returns None if the cell is empty.

Source

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

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

Has no effect and returns None if the cell hasn’t been initialized.

Source

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

Consumes the cell, returning the wrapped value.

Returns None if the cell was empty.

Trait Implementations§

Source§

impl<T> Drop for OnceCell<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

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

Source§

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

Source§

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

Source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for OnceCell<T>

§

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

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.