pub struct OnceLock<T> { /* private fields */ }Expand description
A cell that can be written to only once (Rust 2024 compatible)
Similar to std::sync::OnceLock but works in no_std environments.
Implementations§
Source§impl<T> OnceLock<T>
impl<T> OnceLock<T>
Sourcepub unsafe fn get_mut(&self) -> Option<&'static mut T>
pub unsafe fn get_mut(&self) -> Option<&'static mut T>
Get mutable reference if initialized (unsafe)
§Safety
Caller must ensure exclusive access to the contained value for the duration of the returned reference’s lifetime. No other references (mutable or immutable) to the inner value may exist concurrently. Violating this invariant causes undefined behavior due to aliased mutable references.
Sourcepub fn set(&self, value: T) -> Result<(), T>
pub fn set(&self, value: T) -> Result<(), T>
Initialize the cell with a value
Returns Ok(()) if initialization succeeds, Err(value) if already initialized
Sourcepub fn get_or_init<F>(&self, f: F) -> &'static Twhere
F: FnOnce() -> T,
pub fn get_or_init<F>(&self, f: F) -> &'static Twhere
F: FnOnce() -> T,
Get or initialize the value