ResettableOnceLock

Struct ResettableOnceLock 

Source
pub struct ResettableOnceLock<T> { /* private fields */ }
Expand description

ResettableOnceLock provides thread-safe access to a value of type T via a specific state machine.

  • Fresh values of the lock start in a “to be updated” state.
  • When values are in “to be updated” then calls to get will return None.
  • In the to be updated state, the first call to get_or_update will run update on the object stored in the lock and transition the lock to the “updated” state.
  • Once in the updated state, calls to get will return a reference to the shared object, future calls to get_or_update will behave just like a call to get in this way.
  • A call to reset will transition the lock back to the “to be updated” state. Crucially, this requires mutable access to the lock object, making the patterns expressable via this lock less expressive than a regular mutex.

Implementations§

Source§

impl<T> ResettableOnceLock<T>

Source

pub fn new(elt: T) -> ResettableOnceLock<T>

Source

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

Source

pub fn get_or_update(&self, update: impl FnOnce(&mut T)) -> &T

Source

pub fn reset(&mut self)

Reset the state of the lock to gate further accesses to the underlying value on another call to get_or_update.

Trait Implementations§

Auto Trait Implementations§

§

impl<T> !Freeze for ResettableOnceLock<T>

§

impl<T> !RefUnwindSafe for ResettableOnceLock<T>

§

impl<T> Unpin for ResettableOnceLock<T>
where T: Unpin,

§

impl<T> UnwindSafe for ResettableOnceLock<T>
where T: UnwindSafe,

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.