OnceCell

Struct OnceCell 

Source
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>

Source

pub const fn new() -> OnceCell<T>

Create an uninitialized cell.

Source

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

Get a reference to the current value, or None if uninitialized.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Consumes the cell, returning the wrapped value (if any).

Trait Implementations§

Source§

impl<T: Clone> Clone for OnceCell<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for OnceCell<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for OnceCell<T>

Source§

fn default() -> Self

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

impl<T> From<T> for OnceCell<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for OnceCell<T>

§

impl<T> !RefUnwindSafe for OnceCell<T>

§

impl<T> !Sync for OnceCell<T>

§

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

§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.