Skip to main content

OnceLock

Struct OnceLock 

Source
pub struct OnceLock<T, O: Once> { /* private fields */ }
Expand description

A synchronization primitive which can nominally be written to only once.

Implementations§

Source§

impl<T, O: Once> OnceLock<T, O>

Source

pub fn new() -> Self

Creates a new empty cell.

Source

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

Gets the reference to the underlying value.

Returns None if the cell is empty, or being initialized. This method never blocks.

Source

pub fn get_or_init<F: FnOnce() -> T>(&self, f: F) -> &T

Gets the contents of the cell, initializing it with f if the cell was empty.

Many threads may call get_or_init concurrently with different initializing functions, but it is guaranteed that only one function will be executed.

§Panics

Panics if f panics. If f panics, the cell is left uninitialized and subsequent attempts to initialize the cell are expected to panic.

Trait Implementations§

Source§

impl<T: Debug, O: Once> Debug for OnceLock<T, O>

Source§

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

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

impl<T, O: Once> Default for OnceLock<T, O>

Source§

fn default() -> Self

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

impl<T, O: Once> Drop for OnceLock<T, O>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: Eq, O: Once> Eq for OnceLock<T, O>

Source§

impl<T: PartialEq, O: Once> PartialEq for OnceLock<T, O>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: Send, O: Once> Send for OnceLock<T, O>

Source§

impl<T: Send + Sync, O: Once> Sync for OnceLock<T, O>

Auto Trait Implementations§

§

impl<T, O> !Freeze for OnceLock<T, O>

§

impl<T, O> !RefUnwindSafe for OnceLock<T, O>

§

impl<T, O> Unpin for OnceLock<T, O>

§

impl<T, O> UnsafeUnpin for OnceLock<T, O>

§

impl<T, O> UnwindSafe for OnceLock<T, O>

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<B, A> LockBefore<B> for A
where B: LockAfter<A>,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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

impl<L, T> UnlockedAccess<L> for T

Source§

type Data = <L as UnlockedAccessMarkerFor<T>>::Data

The type of state being accessed.
Source§

type Guard<'l> = &'l <L as UnlockedAccessMarkerFor<T>>::Data where T: 'l

A guard providing read access to the data.
Source§

fn access(&self) -> <T as UnlockedAccess<L>>::Guard<'_>

How to access the state.