Skip to main content

Reader

Struct Reader 

pub struct Reader<'a, T>
where T: Storable + 'static,
{ waiter: Waiter<'a, T>, }
Expand description

Reader for a Storable type.

Allows Actors to read a value of a type written by another actor. 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 to yet, Reader::read will return None.

§Usage

Reader::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 Reader::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 Reader 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 Reader::read or Reader::read_updated. A new write from a Writer marks the value as unseen.

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

§Example

#[veecle_os_runtime::actor]
async fn foo_reader(mut reader: Reader<'_, Foo>) -> veecle_os_runtime::Never {
    loop {
        let processed_value = reader.read_updated(|value: &Foo| {
            // do something with the value.
        }).await;
    }
}

Fields§

§waiter: Waiter<'a, T>

Implementations§

§

impl<T> Reader<'_, 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, or similar read methods.

pub async fn wait_for_update(&mut self) -> &mut Reader<'_, 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.

Trait Implementations§

§

impl<T> CombinableReader for Reader<'_, 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 Reader<'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 Reader<'a, T>
where T: Storable + 'static,

§

impl<'pin, 'a, T> Unpin for Reader<'a, T>
where T: Storable + 'static, <PinnedFieldsOfHelperStruct<__Reader<'pin, 'a, T>> as PinnedFieldsOfHelperTrait>::Actual: Unpin,

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<'a, T> !UnwindSafe for Reader<'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.