pub struct TOnce<P, T> { /* private fields */ }Expand description
A thread-safe cell which can be written to only once using a parameterized initializer.
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<P, T> TOnce<P, T>
impl<P, T> TOnce<P, T>
Sourcepub const fn new(param: P) -> Self
pub const fn new(param: P) -> Self
Creates a new, uninitialized TOnce cell with the given parameter.
Sourcepub const fn with_value(value: T) -> Self
pub const fn with_value(value: T) -> Self
Creates a new TOnce cell that is already initialized with the given value, bypassing the parameter.
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, ignoring the parameter.
- 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, discards the parameter, 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, discards the parameter, 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, discards the parameter, 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 TOnce 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 TOnce cell is undefined behavior.
The caller must ensure the cell is initialized and that they have exclusive access.
Sourcepub fn take(&mut self, param: P) -> Result<T, P>
pub fn take(&mut self, param: P) -> Result<T, P>
Takes the value out of the cell, leaving it uninitialized with the given parameter.
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, ignoring the parameter. Blocks if another thread is initializing.
- If uninitialized, discards the parameter, 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, discards the parameter, 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(P) -> T,
pub fn get_or_init<F>(&self, f: F) -> &Twhere
F: FnOnce(P) -> T,
Gets the value, initializing with f(param) if needed. Blocks if needed.
- If initialized, returns a reference to the existing value.
- If uninitialized, calls
f(param), stores the result, and returns a reference.
If multiple threads call this concurrently, only one f(param) execution happens.
Sourcepub fn get_mut_or_init<F>(&mut self, f: F) -> &mut Twhere
F: FnOnce(P) -> T,
pub fn get_mut_or_init<F>(&mut self, f: F) -> &mut Twhere
F: FnOnce(P) -> T,
Gets a mutable reference, initializing with f(param) if needed.
- If initialized, returns a mutable reference to the existing value.
- If uninitialized, calls
f(param), 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(param) if needed. Blocks if needed.
- If initialized, returns
Ok(&value). - If uninitialized, calls
f(param):- 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(param) 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(param) if needed.
- If initialized, returns
Ok(&mut value). - If uninitialized, calls
f(param):- 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(param) if needed. Blocks if needed.
- If initialized, returns a reference to the existing value.
- If uninitialized, awaits
f(param), stores the result, and returns a reference.
If multiple tasks call this concurrently, only one f(param) 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(param) if needed.
- If initialized, returns a mutable reference to the existing value.
- If uninitialized, awaits
f(param), 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(param) if needed. Blocks if needed.
- If initialized, returns
Ok(&value). - If uninitialized, awaits
f(param):- 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(param) 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(param) if needed.
- If initialized, returns
Ok(&mut value). - If uninitialized, awaits
f(param):- 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<P: Clone, T: Clone> Clone for TOnce<P, T>
impl<P: Clone, T: Clone> Clone for TOnce<P, T>
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the TOnce 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 with a cloned parameter.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more