pub struct AtomicOnce<T> { /* private fields */ }Expand description
Lock-free, single-assignment cell.
Allows multiple threads to race to initialize a value. The winning thread stores its value, while losing threads silently discard their work.
Implementations§
Source§impl<T> AtomicOnce<T>
impl<T> AtomicOnce<T>
Sourcepub fn new_initialized(val: Box<T>) -> Self
pub fn new_initialized(val: Box<T>) -> Self
Creates a new instance already initialized with the given value.
Sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Returns a reference to the initialized value, or None if
uninitialized.
pub unsafe fn get_unchecked(&self) -> &T
Sourcepub fn init(&self, val: Box<T>) -> Result<(), (&T, Box<T>)>
pub fn init(&self, val: Box<T>) -> Result<(), (&T, Box<T>)>
Attempts to initialize the cell with the provided value.
If the cell was already initialized or we lost the CAS race, returns
the reference to the initialized value and the owned value val.
Sourcepub fn get_or_init<F>(&self, f: F) -> Result<&T, (&T, Box<T>)>
pub fn get_or_init<F>(&self, f: F) -> Result<&T, (&T, Box<T>)>
Returns a reference to the value, initializing it with f if necessary.
If the cell was already initialized or we lost the CAS race, returns
the reference to the initialized value and the owned value that was
computed by the f.
Note that f may be executed multiple times concurrently if multiple
threads attempt initialization simultaneously. Only one result will
be retained.
Sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Consumes the AtomicOnce, returning the inner value if initialized.