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>
impl<T> OnceCell<T>
Sourcepub fn set(&self, value: T) -> Result<(), T>
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.
Sourcepub fn set_with<F: FnOnce() -> T>(&self, ctor: F) -> Result<(), F>
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.
Sourcepub fn get_ptr(&self) -> Option<*mut T>
pub fn get_ptr(&self) -> Option<*mut T>
Gets the pointer to the underlying value.
Returns None if the cell is empty.
Sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Gets the reference to the underlying value.
Returns None if the cell is empty, or being initialized.
Sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
Gets the mutable reference to the underlying value.
Returns None if the cell is empty.
Sourcepub fn take(&mut self) -> Option<T>
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.
Sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Consumes the cell, returning the wrapped value.
Returns None if the cell was empty.