pub struct OnceLock<T> { /* private fields */ }Expand description
A cell that can be written exactly once and is safe to share across threads.
Equivalent to std::sync::OnceLock. The first call to get_or_init
runs the initializer; all subsequent calls block until initialization completes
and then return a reference to the stored value.
Implementations§
Source§impl<T> OnceLock<T>
impl<T> OnceLock<T>
Sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Returns a reference to the value if initialized, otherwise None.
Sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the value if initialized, otherwise None.
Sourcepub fn get_or_init(&self, f: impl FnOnce() -> T) -> &T
pub fn get_or_init(&self, f: impl FnOnce() -> T) -> &T
Returns the value, initializing it with f if this is the first call.
If another thread is already initializing the value, this call blocks until initialization completes.
Trait Implementations§
impl<T: Send> Send for OnceLock<T>
impl<T: Send + Sync> Sync for OnceLock<T>
Auto Trait Implementations§
impl<T> !Freeze for OnceLock<T>
impl<T> !RefUnwindSafe for OnceLock<T>
impl<T> Unpin for OnceLock<T>where
T: Unpin,
impl<T> UnsafeUnpin for OnceLock<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for OnceLock<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more