LoopSyncCell

Struct LoopSyncCell 

Source
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>

Source

pub const fn new(initial_value: T) -> Self

Create a new LoopSyncCell with the given value

Source

pub fn new_default() -> Self
where T: Default,

Create a new LoopSyncCell using the default value of the type it holds

Source

pub const fn new_empty() -> Self

Create a new, empty LoopSyncCell that can never provide a value.

Source

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

Trait Implementations§

Source§

impl<T: Copy + Debug> Debug for LoopSyncCell<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for LoopSyncCell<T>

Source§

fn default() -> Self

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

impl<T> From<T> for LoopSyncCell<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> !Freeze for LoopSyncCell<T>

§

impl<T> !RefUnwindSafe for LoopSyncCell<T>

§

impl<T> Send for LoopSyncCell<T>
where T: Send,

§

impl<T> Sync for LoopSyncCell<T>
where T: Send,

§

impl<T> Unpin for LoopSyncCell<T>
where T: Unpin,

§

impl<T> UnwindSafe for LoopSyncCell<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.