Struct ExclusiveReader

Source
pub struct ExclusiveReader<'a, T>
where T: Storable + 'static,
{ /* private fields */ }
Expand description

Exclusive reader for a Storable type.

By being the sole reader for a Storable type, this reader can move the read value out. The generic type T from the reader specifies the type of the value that is being read.

The reader allows reading the current value. If no value for type T has been written yet, ExclusiveReader::read and ExclusiveReader::take will return None.

§Usage

ExclusiveReader::wait_for_update allows waiting until the type is written to. It will return immediately if an unseen value is available. Unseen does not imply the value actually changed, just that an Actor has written a value. A write of the same value still triggers ExclusiveReader::wait_for_update to resolve.

To illustrate:

- Writer writes 5
- Reader is woken and reads 5.
  Reader waits for updates.
...
- Writer writes 5 once again.
- Reader is woken and reads 5.
...

The reader is woken, even if the new value equals the old one. The ExclusiveReader is only aware of the act of writing.

§Example

#[veecle_os_runtime::actor]
async fn foo_reader(mut reader: ExclusiveReader<'_, Foo>) -> std::convert::Infallible {
    loop {
        let value = reader.wait_for_update().await.take();
    }
}

Implementations§

Source§

impl<T> ExclusiveReader<'_, T>
where T: Storable + 'static,

Source

pub fn read<U>( &self, f: impl FnOnce(Option<&<T as Storable>::DataType>) -> U, ) -> U

Reads the current value of a type.

Can be combined with Self::wait_for_update to wait for the value to be updated before reading it.

This method takes a closure to ensure the reference is not held across await points.

Source

pub fn take(&mut self) -> Option<<T as Storable>::DataType>

Takes the current value of the type, leaving behind None.

Source

pub fn read_cloned(&self) -> Option<<T as Storable>::DataType>
where <T as Storable>::DataType: Clone,

Reads and clones the current value.

This is a wrapper around Self::read that additionally clones the value. You can use it instead of reader.read(|c| c.clone()).

Source

pub async fn wait_for_update(&mut self) -> &mut ExclusiveReader<'_, T>

Waits for any write to occur.

This future resolving does not imply that previous_value != new_value, just that a Writer has written a value of T since the last time this future resolved.

This returns &mut Self to allow chaining a call to methods accessing the value, for example read.

Trait Implementations§

Source§

impl<T> CombinableReader for ExclusiveReader<'_, T>
where T: Storable,

Source§

type ToBeRead = Option<<T as Storable>::DataType>

The (owned) type that this type reads, will be exposed as a reference in the CombineReaders::read callback.
Source§

impl<'a, T> Debug for ExclusiveReader<'a, T>
where T: Debug + Storable + 'static,

Source§

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

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

impl<'a, T> StoreRequest<'a> for ExclusiveReader<'a, T>
where T: Storable + 'static,

Auto Trait Implementations§

§

impl<'a, T> Freeze for ExclusiveReader<'a, T>

§

impl<'a, T> !RefUnwindSafe for ExclusiveReader<'a, T>

§

impl<'a, T> !Send for ExclusiveReader<'a, T>

§

impl<'a, T> !Sync for ExclusiveReader<'a, T>

§

impl<'a, T> Unpin for ExclusiveReader<'a, T>

§

impl<'a, T> !UnwindSafe for ExclusiveReader<'a, T>

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<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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V