pub struct LoopSyncCell<T>(/* private fields */);Expand description
Specialized cell for when you want to build an iterator that produces a value, to then be
modified and used, but then finally made available for the next iteration. The value of this
cell can be restored from an accessor on a different thread to the one where it was taken (i.e.
it is Sync).
This form is particularly useful when writing asynchronous event processors with shared data,
where you might need to hold an access across an await point. It is an alternative to
LoopCell, and is likely to be used often.
Unlike the underlying option_lock::OptionLock, this only implements Default if the type
parameter T implements Default, and it is created with the default value of T in that
case. Also note that option_lock::OptionLock is implemented entirely with atomics - it’s
very lightweight and doesn’t carry the overhead of allocation or normal mutex. This is
essentially close to the most minimal equivalent to how we use Cell<Option<T>> in LoopCell.
If you access the LoopSyncCell while something else is holding on to an accessor, then you
won’t be able to get any access. If something else deliberately consumes it’s accessor, then
you will not be able to get any values out of this any more. You can also
create a LoopSyncCell which cannot have anything retrieved
Implementations§
Source§impl<T> LoopSyncCell<T>
impl<T> LoopSyncCell<T>
Sourcepub const fn new(initial_value: T) -> Self
pub const fn new(initial_value: T) -> Self
Create a new LoopSyncCell with the given value
Sourcepub fn new_default() -> Selfwhere
T: Default,
pub fn new_default() -> Selfwhere
T: Default,
Create a new LoopSyncCell using the default value of the type it holds
Sourcepub const fn new_empty() -> Self
pub const fn new_empty() -> Self
Create a new, empty LoopSyncCell that can never provide a value.
Sourcepub fn access(&self) -> Option<LoopSyncCellAccess<'_, T>>
pub fn access(&self) -> Option<LoopSyncCellAccess<'_, T>>
Attempt to access the loop cell, producing an accessor that will write back any changes to the cell once it is dropped (unless it’s manually disarmed).
Only produces Some if no other accessors are using this LoopSyncCell