pub struct OnceLock<T> { /* private fields */ }
Expand description
Wrapper for std::sync::OnceLock
The exact locking behaviour of std::sync::OnceLock
is currently undefined, but may
deadlock in the event of reentrant initialization attempts. This wrapper participates in
cycle detection as normal and will therefore panic in the event of reentrancy.
Most of this primitive’s methods do not involve locking and as such are simply passed through to the inner implementation.
§Examples
use tracing_mutex::stdsync::tracing::OnceLock;
static LOCK: OnceLock<i32> = OnceLock::new();
assert!(LOCK.get().is_none());
std::thread::spawn(|| {
let value: &i32 = LOCK.get_or_init(|| 42);
assert_eq!(value, &42);
}).join().unwrap();
let value: Option<&i32> = LOCK.get();
assert_eq!(value, Some(&42));
Implementations§
Source§impl<T> OnceLock<T>
impl<T> OnceLock<T>
Sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Gets a reference to the underlying value.
This method does not attempt to lock and therefore does not participate in cycle detection.
Sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
Gets a mutable reference to the underlying value.
This method does not attempt to lock and therefore does not participate in cycle detection.
Sourcepub fn set(&self, value: T) -> Result<(), T>
pub fn set(&self, value: T) -> Result<(), T>
Sets the contents of this cell to the underlying value
As this method may block until initialization is complete, it participates in cycle detection.
Sourcepub fn get_or_init<F>(&self, f: F) -> &Twhere
F: FnOnce() -> T,
pub fn get_or_init<F>(&self, f: F) -> &Twhere
F: FnOnce() -> T,
Gets the contents of the cell, initializing it with f
if the cell was empty.
This method participates in cycle detection. Reentrancy is considered a cycle.
Sourcepub fn take(&mut self) -> Option<T>
pub fn take(&mut self) -> Option<T>
Takes the value out of this OnceLock
, moving it back to an uninitialized state.
This method does not attempt to lock and therefore does not participate in cycle detection.
Sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Consumes the OnceLock
, returning the wrapped value. Returns None if the cell was
empty.
This method does not attempt to lock and therefore does not participate in cycle detection.
Trait Implementations§
impl<T: Eq> Eq for OnceLock<T>
Auto Trait Implementations§
impl<T> !Freeze for OnceLock<T>
impl<T> RefUnwindSafe for OnceLock<T>where
T: RefUnwindSafe + UnwindSafe,
impl<T> Send for OnceLock<T>where
T: Send,
impl<T> Sync for OnceLock<T>
impl<T> Unpin for OnceLock<T>where
T: Unpin,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)