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

Constructs an uninitialized WaitCell value.

Attempts to dereference this value will block until an initialization function such as init is invoked.

Constructs an initialized WaitCell value.

Attempts to dereference this value will not block.

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.

Conditionally initializes the value, allowing dereferencing to proceed.

Returns
  • true – The value was initialized using func.
  • 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.

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.

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.

Returns a reference to the initialized value, or None if it is not yet initialized.

Sets the initialized value, returning a mutable reference to it.

The previous value, if any, is dropped.

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.

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.

Returns a mutable reference to the initialized value, or None if the value is not initialized.

Returns the initialized value, or None if the value is not initialized.

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.

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.

Directly sets the state of the value as initialized.

Safety

If the value is later accessed, it must actually have been initialized.

Directly clears the initialization state of the value, as if by core::mem::forget.

Constructs a default-initialized WaitCell value.

Attempts to dereference this value will not block.

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Executes the destructor for this type. Read more

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.