pub struct WaitCell<T> { /* private fields */ }
Expand description
A cell type containing a value which may not yet be available.
A WaitCell
generally begins in an uninitialized state. While in this
state, attempts to reference the value block until some other thread
provides a value. While shared, a value can only be set at most once.
If you require the value to be set multiple times while shared, consider
using RwLock
instead. WaitCell
is more performant than RwLock
,
however, as expensive atomic writes are not required to track readers and
writers.
In asynchronous contexts, a type implementing Future
should be used
instead.
Example
See the crate-level documentation.
Implementations
sourceimpl<T> WaitCell<T>
impl<T> WaitCell<T>
sourcepub const fn initialized(value: T) -> Self
pub const fn initialized(value: T) -> Self
Constructs an initialized WaitCell
value.
Attempts to dereference this value will not block.
sourcepub fn try_init<F: FnOnce() -> T>(&self, func: F) -> bool
pub fn try_init<F: FnOnce() -> T>(&self, func: F) -> bool
Conditionally initializes the value, allowing dereferencing to proceed.
Returns
true
– The value was initialized usingfunc
.false
– The value is already initialized or is currently initializing.func
was not invoked.
Panics
If func
panics, the WaitCell
remains uninitialized. Concurrent
initialization attempts may fail.
sourcepub fn get_or_init<F: FnOnce() -> T>(&self, func: F) -> &T
pub fn get_or_init<F: FnOnce() -> T>(&self, func: F) -> &T
Conditionally initializes the value or waits for the value to become available if it is not already so.
Panics
If func
panics, the WaitCell
remains uninitialized. Concurrent
initialization attempts may fail.
Notes
func
is only invoked in the case where the value is not currently
initialized or undergoing initialization.
This function blocks if the value is currently undergoing initialization.
sourcepub fn get(&self) -> &T
pub fn get(&self) -> &T
Waits for the value to become initialized and returns a reference to it.
Notes
This function will block until the value is initialized by another thread.
sourcepub fn try_get(&self) -> Option<&T>
pub fn try_get(&self) -> Option<&T>
Returns a reference to the initialized value, or None
if it is not yet
initialized.
sourcepub fn set(&mut self, value: T) -> &mut T
pub fn set(&mut self, value: T) -> &mut T
Sets the initialized value, returning a mutable reference to it.
The previous value, if any, is dropped.
sourcepub fn unset(&mut self) -> bool
pub fn unset(&mut self) -> bool
Drops the initialized value, if any. The value may be re-initialized again later.
Returns
true
- A value was present and was dropped.false
- A value was not present.
sourcepub fn as_inner(&mut self) -> Option<&mut T>
pub fn as_inner(&mut self) -> Option<&mut T>
Returns a mutable reference to the initialized value, or None
if the value
is not initialized.
sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Returns the initialized value, or None
if the value is not initialized.
sourcepub fn as_ptr(&self) -> *const T
pub fn as_ptr(&self) -> *const T
Provides direct immutable access to the value.
Safety
The value may or may not be initialized and may or may not have concurrent immutable references. If a concurrent mutable reference exists, it was created unsafely.
sourcepub fn as_mut_ptr(&mut self) -> *mut T
pub fn as_mut_ptr(&mut self) -> *mut T
Provides direct mutable access to the value.
Safety
The value may or may not be initialized, but it is guaranteed to have no concurrent references except those created unsafely.
sourcepub unsafe fn set_init(&mut self)
pub unsafe fn set_init(&mut self)
Directly sets the state of the value as initialized.
Safety
If the value is later accessed, it must actually have been initialized.
sourcepub fn set_uninit(&mut self)
pub fn set_uninit(&mut self)
Directly clears the initialization state of the value, as if by core::mem::forget.
sourceimpl<T: Default> WaitCell<T>
impl<T: Default> WaitCell<T>
sourcepub fn with_default() -> Self
pub fn with_default() -> Self
Constructs a default-initialized WaitCell
value.
Attempts to dereference this value will not block.
Trait Implementations
impl<T: Sync> Sync for WaitCell<T>
Auto Trait Implementations
impl<T> !RefUnwindSafe for WaitCell<T>
impl<T> Send for WaitCell<T> where
T: Send,
impl<T> Unpin for WaitCell<T> where
T: Unpin,
impl<T> UnwindSafe for WaitCell<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more