Skip to main content

ExclusiveReader

Struct ExclusiveReader 

pub struct ExclusiveReader<'a, T>
where T: Storable + 'static,
{ waiter: Waiter<'a, T>, }
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.

§Seen values

The reader tracks whether the current value has been “seen”. A value is marked as seen when any read method is called, such as ExclusiveReader::read or ExclusiveReader::take. A new write from a Writer marks the value as unseen.

ExclusiveReader::is_updated returns true if the current value is unseen. ExclusiveReader::wait_for_update waits until an unseen value is available.

§Example

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

Fields§

§waiter: Waiter<'a, T>

Implementations§

§

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

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

Available on crate feature data-support-can only.

Reads the current value of a type.

Marks the current value as seen. This method takes a closure to ensure the reference is not held across await points.

pub async fn read_updated<U>( &mut self, f: impl FnOnce(&<T as Storable>::DataType) -> U, ) -> U

Available on crate feature data-support-can only.

Reads the next unseen value of a type.

Waits until an unseen value is available, then reads it. Marks the current value as seen. This method takes a closure to ensure the reference is not held across await points.

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

Available on crate feature data-support-can only.

Reads and clones the current value.

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

pub async fn read_updated_cloned(&mut self) -> <T as Storable>::DataType
where <T as Storable>::DataType: Clone,

Available on crate feature data-support-can only.

Reads and clones the next unseen value.

Waits until an unseen value is available, then reads it. Marks the current value as seen. This is a wrapper around Self::read_updated that additionally clones the value. You can use it instead of reader.read_updated(|c| c.clone()).

pub fn is_updated(&self) -> bool

Available on crate feature data-support-can only.

Returns true if an unseen value is available.

A value becomes “seen” after calling read, read_updated, take, take_updated, or similar methods.

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

Available on crate feature data-support-can only.

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 read operation.

This returns &mut Self to allow chaining a call to read.

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

Available on crate feature data-support-can only.

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

Marks the current value as seen.

pub async fn take_updated(&mut self) -> <T as Storable>::DataType

Available on crate feature data-support-can only.

Takes the next unseen value of the type, leaving behind None.

Waits until an unseen value is available, then takes it. Marks the current value as seen.

Trait Implementations§

§

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

§

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.
§

fn is_updated(&self) -> bool

Internal implementation details. Read more
§

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

§

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

Formats the value using the given formatter. Read more
§

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.