pub struct Once<T> { /* private fields */ }Expand description
A thread-safe cell which can be written to only once.
This structure provides safe initialization for values that might be accessed concurrently by multiple threads. It ensures that the initialization logic runs only once, even if multiple threads attempt to initialize the value simultaneously.
It uses atomic operations and parking_lot’s futex-based synchronization for
efficient blocking when necessary.
Implementations§
Source§impl<T> Once<T>
impl<T> Once<T>
Sourcepub const fn with_value(value: T) -> Self
pub const fn with_value(value: T) -> Self
Creates a new Once cell that is already initialized with the given value.
Sourcepub fn is_done(&self) -> bool
pub fn is_done(&self) -> bool
Checks if the cell has been initialized.
This method never blocks.
Sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Returns a reference to the contained value if initialized.
Returns None if the cell is uninitialized or currently being initialized.
This method never blocks.
Sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the contained value if initialized.
Returns None if the cell is uninitialized or currently being initialized.
This method requires exclusive access (&mut self) and never blocks.
Sourcepub fn try_set(&self, value: T) -> Result<&T, T>
pub fn try_set(&self, value: T) -> Result<&T, T>
Attempts to initialize the cell with value without blocking.
- If the cell is uninitialized and not locked, initializes it with
valueand returnsOk(&value). - If the cell is already initialized, returns
Err(value). - If the cell is currently locked by another thread, returns
Err(value).
Sourcepub fn replace_mut(&mut self, value: T) -> Option<T>
pub fn replace_mut(&mut self, value: T) -> Option<T>
Replaces the cell’s value, returning the old value if initialized.
- If uninitialized, sets the value to
valueand returnsNone. - If initialized, replaces the existing value with
valueand returns the old value.
Requires exclusive access (&mut self), so it never blocks.
Sourcepub fn get_mut_or_set(&mut self, value: T) -> &mut T
pub fn get_mut_or_set(&mut self, value: T) -> &mut T
Gets a mutable reference, initializing with value if needed.
- If initialized, returns a mutable reference to the existing value.
- If uninitialized, initializes it with
valueand returns a mutable reference.
Requires exclusive access (&mut self), so it never blocks.
Sourcepub fn get_mut_or_default(&mut self) -> &mut Twhere
T: Default,
pub fn get_mut_or_default(&mut self) -> &mut Twhere
T: Default,
Gets a mutable reference, initializing with Default::default() if needed.
- If initialized, returns a mutable reference to the existing value.
- If uninitialized, initializes it with
T::default()and returns a mutable reference.
Requires exclusive access (&mut self), so it never blocks.
Sourcepub unsafe fn get_unchecked(&self) -> &T
pub unsafe fn get_unchecked(&self) -> &T
Returns a reference to the value without checking if it’s initialized.
§Safety
Calling this method on an uninitialized Once cell is undefined behavior.
The caller must ensure the cell is initialized, e.g., by calling is_done() or get().
Sourcepub unsafe fn get_unchecked_mut(&mut self) -> &mut T
pub unsafe fn get_unchecked_mut(&mut self) -> &mut T
Returns a mutable reference to the value without checking if it’s initialized.
§Safety
Calling this method on an uninitialized Once cell is undefined behavior.
The caller must ensure the cell is initialized and that they have exclusive access.
Sourcepub fn take(&mut self) -> Option<T>
pub fn take(&mut self) -> Option<T>
Takes the value out of the cell, leaving it uninitialized.
Returns Some(value) if the cell was initialized, None otherwise.
Requires exclusive access (&mut self), so it never blocks.
Sourcepub fn set(&self, value: T) -> Result<(), T>
pub fn set(&self, value: T) -> Result<(), T>
Initializes the cell with value. Blocks if another thread is initializing.
- If uninitialized, initializes it with
valueand returnsOk(()). - If already initialized, returns
Err(value).
Guarantees the cell is initialized upon return, but not necessarily with value
if another thread completed initialization first.
Sourcepub fn try_insert(&self, value: T) -> Result<&T, (&T, T)>
pub fn try_insert(&self, value: T) -> Result<&T, (&T, T)>
Initializes the cell with value if uninitialized, returning a reference. Blocks if needed.
- If uninitialized, initializes with
valueand returnsOk(&value). - If already initialized, returns
Err((¤t_value, value)).
Guarantees the cell is initialized upon return.
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 value, initializing with f() if needed. Blocks if needed.
- If initialized, returns a reference to the existing value.
- If uninitialized, calls
f(), stores the result, and returns a reference.
If multiple threads call this concurrently, only one f() execution happens.
Sourcepub fn get_mut_or_init<F>(&mut self, f: F) -> &mut Twhere
F: FnOnce() -> T,
pub fn get_mut_or_init<F>(&mut self, f: F) -> &mut Twhere
F: FnOnce() -> T,
Gets a mutable reference, initializing with f() if needed.
- If initialized, returns a mutable reference to the existing value.
- If uninitialized, calls
f(), stores the result, and returns a mutable reference.
Requires exclusive access (&mut self), so it never blocks.
Sourcepub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
Gets the value, initializing with fallible f() if needed. Blocks if needed.
- If initialized, returns
Ok(&value). - If uninitialized, calls
f():- On
Ok(value), initializes the cell and returnsOk(&value). - On
Err(e), returnsErr(e)and leaves the cell uninitialized.
- On
If multiple threads call this concurrently, only one f() execution happens.
Sourcepub fn get_mut_or_try_init<F, E>(&mut self, f: F) -> Result<&mut T, E>
pub fn get_mut_or_try_init<F, E>(&mut self, f: F) -> Result<&mut T, E>
Gets a mutable reference, initializing with fallible f() if needed.
- If initialized, returns
Ok(&mut value). - If uninitialized, calls
f():- On
Ok(value), initializes the cell and returnsOk(&mut value). - On
Err(e), returnsErr(e)and leaves the cell uninitialized.
- On
Requires exclusive access (&mut self), so it never blocks (initialization happens inline).
Sourcepub async fn get_or_init_async<F, Fut>(&self, f: F) -> &T
pub async fn get_or_init_async<F, Fut>(&self, f: F) -> &T
Gets the value, initializing asynchronously with f() if needed. Blocks if needed.
- If initialized, returns a reference to the existing value.
- If uninitialized, awaits
f(), stores the result, and returns a reference.
If multiple tasks call this concurrently, only one f() execution happens.
Sourcepub async fn get_mut_or_init_async<F, Fut>(&mut self, f: F) -> &mut T
pub async fn get_mut_or_init_async<F, Fut>(&mut self, f: F) -> &mut T
Gets a mutable reference, initializing asynchronously with f() if needed.
- If initialized, returns a mutable reference to the existing value.
- If uninitialized, awaits
f(), stores the result, and returns a mutable reference.
Requires exclusive access (&mut self), so it never blocks (initialization happens inline).
Sourcepub async fn get_or_try_init_async<F, Fut, E>(&self, f: F) -> Result<&T, E>
pub async fn get_or_try_init_async<F, Fut, E>(&self, f: F) -> Result<&T, E>
Gets the value, initializing asynchronously with fallible f() if needed. Blocks if needed.
- If initialized, returns
Ok(&value). - If uninitialized, awaits
f():- On
Ok(value), initializes the cell and returnsOk(&value). - On
Err(e), returnsErr(e)and leaves the cell uninitialized.
- On
If multiple tasks call this concurrently, only one f() execution happens.
Sourcepub async fn get_mut_or_try_init_async<F, Fut, E>(
&mut self,
f: F,
) -> Result<&mut T, E>
pub async fn get_mut_or_try_init_async<F, Fut, E>( &mut self, f: F, ) -> Result<&mut T, E>
Gets a mutable reference, initializing asynchronously with fallible f() if needed.
- If initialized, returns
Ok(&mut value). - If uninitialized, awaits
f():- On
Ok(value), initializes the cell and returnsOk(&mut value). - On
Err(e), returnsErr(e)and leaves the cell uninitialized.
- On
Requires exclusive access (&mut self), so it never blocks (initialization happens inline).
Trait Implementations§
Source§impl<T: Clone> Clone for Once<T>
impl<T: Clone> Clone for Once<T>
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the Once cell.
If the original cell is initialized, the clone will be initialized with a cloned value. If the original is uninitialized, the clone will also be uninitialized.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more