Skip to main content

AtomicOnce

Struct AtomicOnce 

Source
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>

Source

pub const fn new() -> Self

Create a new instance.

Source

pub fn new_initialized(val: Box<T>) -> Self

Creates a new instance already initialized with the given value.

Source

pub fn get(&self) -> Option<&T>

Returns a reference to the initialized value, or None if uninitialized.

Source

pub unsafe fn get_unchecked(&self) -> &T

Source

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.

Source

pub fn get_or_init<F>(&self, f: F) -> Result<&T, (&T, Box<T>)>
where F: FnOnce() -> 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.

Source

pub fn into_inner(self) -> Option<T>

Consumes the AtomicOnce, returning the inner value if initialized.

Trait Implementations§

Source§

impl<T> Drop for AtomicOnce<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Send> Send for AtomicOnce<T>

Source§

impl<T: Sync + Send> Sync for AtomicOnce<T>

Auto Trait Implementations§

§

impl<T> !Freeze for AtomicOnce<T>

§

impl<T> RefUnwindSafe for AtomicOnce<T>

§

impl<T> Unpin for AtomicOnce<T>

§

impl<T> UnsafeUnpin for AtomicOnce<T>

§

impl<T> UnwindSafe for AtomicOnce<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.