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>
impl<T> OnceSlot<T>
Sourcepub fn get(&'static self) -> Option<&'static T>
pub fn get(&'static self) -> Option<&'static T>
Returns the value if already initialized, otherwise None.
Sourcepub fn is_completed(&'static self) -> bool
pub fn is_completed(&'static self) -> bool
Returns true if initialization completed successfully.
Sourcepub fn is_poisoned(&'static self) -> bool
pub fn is_poisoned(&'static self) -> bool
Returns true if the initializer panicked and poisoned the slot.
Sourcepub fn get_or_init(&'static self, init: impl FnOnce() -> T) -> &'static T
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.