pub struct OnceCell<T>(/* private fields */);Expand description
A cell which can only be initialized once.
Like crate::sync::OnceLock, but not thread-safe.
Implementations§
Source§impl<T> OnceCell<T>
impl<T> OnceCell<T>
Sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Get a reference to the current value, or None if uninitialized.
Sourcepub fn get_or_init(&self, func: impl FnOnce() -> T) -> &T
pub fn get_or_init(&self, func: impl FnOnce() -> T) -> &T
Get a reference to the current value if initialed, invoking the specified callback otherwise.
Sourcepub fn get_or_try_init<E>(
&self,
func: impl FnOnce() -> Result<T, E>,
) -> Result<&T, E>
pub fn get_or_try_init<E>( &self, func: impl FnOnce() -> Result<T, E>, ) -> Result<&T, E>
Get a reference to the current value if initialized, invoking the specified callback otherwise.
§Errors
Any errors from callback function will be propagated upwards, and in that case the cell will remain unchanged.
Sourcepub fn set(&self, value: T) -> Result<(), T>
pub fn set(&self, value: T) -> Result<(), T>
Initializes the contents of the cell to value, returning an error if already initialized to some other value.
§Errors
Returns Ok(()) if successfully initialized,
or Err(given_Value) if already initialized.
Sourcepub fn take(&mut self) -> Option<T>
pub fn take(&mut self) -> Option<T>
Takes the value out of this OnceCell,
moving it back to an uninitialized state.
Returns None if not currently initialized.
This is safe because it takes a mutable reference.
Sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Consumes the cell, returning the wrapped value (if any).