[−][src]Struct waitcell::WaitCell
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 dereference 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
impl<T> WaitCell<T>
[src]
#[must_use]pub const fn new() -> Self
[src]
Constructs an uninitialized WaitCell
value.
Attempts to dereference this value will block until an initialization
function such as init
is invoked.
#[must_use]pub const fn initialized(value: T) -> Self
[src]
Constructs an initialized WaitCell
value.
Attempts to dereference this value will not block.
pub fn init(&self, value: T) -> &T
[src]
Initializes the value, allowing dereferencing to proceed.
Panics
If the value is already initialized or is currently undergoing
initialization. To conditionally initialize the value, consider using
try_init
.
pub fn try_init<F: FnOnce() -> T>(&self, func: F) -> bool
[src]
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.
pub fn get_or_init<F: FnOnce() -> T>(&self, func: F) -> &T
[src]
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.
#[must_use]pub fn get(&self) -> &T
[src]
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.
#[must_use]pub fn try_get(&self) -> Option<&T>
[src]
Returns a reference to the initialized value, or None
if it is not yet
initialized.
pub fn set(&mut self, value: T) -> &mut T
[src]
Sets the initialized value, returning a mutable reference to it.
The previous value, if any, is dropped.
pub fn unset(&mut self) -> bool
[src]
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.
#[must_use]pub fn is_set(&mut self) -> bool
[src]
Returns true
if the value is currently initialized and false
otherwise.
Notes
This method is only available when the WaitCell
is exclusively borrowed.
If the WaitCell
is shared, consider using
wait_cell.
try_get
().
is_some
()
instead.
#[must_use]pub fn as_inner(&mut self) -> Option<&mut T>
[src]
Returns a mutable reference to the initialized value, or None
if the value
is not initialized.
#[must_use]pub fn into_inner(self) -> Option<T>
[src]
Returns the initialized value, or None
if the value is not initialized.
impl<T: Default> WaitCell<T>
[src]
#[must_use]pub fn with_default() -> Self
[src]
Constructs a default-initialized WaitCell
value.
Attempts to dereference this value will not block.
Trait Implementations
impl<T> AsRef<T> for WaitCell<T>
[src]
impl<T> Debug for WaitCell<T>
[src]
impl<T> Default for WaitCell<T>
[src]
impl<T> Deref for WaitCell<T>
[src]
impl<T> Drop for WaitCell<T>
[src]
impl<T> From<T> for WaitCell<T>
[src]
impl<T: Sync> Sync for WaitCell<T>
[src]
Auto Trait Implementations
impl<T> !RefUnwindSafe for WaitCell<T>
[src]
impl<T> Send for WaitCell<T> where
T: Send,
[src]
T: Send,
impl<T> Unpin for WaitCell<T> where
T: Unpin,
[src]
T: Unpin,
impl<T> UnwindSafe for WaitCell<T> where
T: UnwindSafe,
[src]
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<!> for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,