Skip to main content

OnceSlot

Struct OnceSlot 

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

Zero-initialized slot for one-time initialization of a T.

The slot itself starts zeroed (state UNINIT), which is what makes it usable with generic_static. The T value is written exactly once by the thread that wins the claim race; other threads block until INIT (or POISONED) becomes visible.

With the std feature, waiters block on a Mutex/Condvar pair (no spinning); without it they spin with core::hint::spin_loop.

§Panics

get_or_init must not be called reentrantly with the same slot (the initializer calling back into the same slot). That deadlocks: the thread already holds the BUSY claim, so the inner call blocks forever waiting for itself.

If the initializer panics, the slot is poisoned.

Implementations§

Source§

impl<T> OnceSlot<T>

Source

pub const fn new() -> Self

Create a new uninitialized slot.

Source

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

Returns the value if already initialized, otherwise None.

Source

pub fn is_completed(&'static self) -> bool

Returns true if initialization completed successfully.

Source

pub fn is_poisoned(&'static self) -> bool

Returns true if the initializer panicked and poisoned the slot.

Source

pub fn get_or_init(&'static self, init: impl FnOnce() -> T) -> &'static T

Returns the value, running init exactly once to produce it.

Concurrent callers block (on a condvar with std, by spinning without it) until the winner finishes. If init panics, the slot is poisoned and every caller — past, present, and future — panics.

§Panics

Panics if the slot is poisoned (including when a concurrent initializer panics while this call is blocked). Must not be called reentrantly with the same slot.

Trait Implementations§

Source§

impl<T> Default for OnceSlot<T>

Source§

fn default() -> Self

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

impl<T: Send> Send for OnceSlot<T>

Source§

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

Source§

impl<T> Zeroable for OnceSlot<T>

Source§

fn zeroed() -> Self

Auto Trait Implementations§

§

impl<T> !Freeze for OnceSlot<T>

§

impl<T> !RefUnwindSafe for OnceSlot<T>

§

impl<T> Unpin for OnceSlot<T>

§

impl<T> UnsafeUnpin for OnceSlot<T>

§

impl<T> UnwindSafe for OnceSlot<T>

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 = !

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.