Struct Reader

Source
pub struct Reader<'a, T>
where T: Storable + 'static,
{ /* private fields */ }
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. See Self::wait_init for creating a reader that ensures available values for T.

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

§Example

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

Implementations§

Source§

impl<T> Reader<'_, 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 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 Reader<'_, 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.

Source§

impl<'a, T> Reader<'a, T>
where T: Storable + 'static,

Source

pub async fn wait_init(self) -> InitializedReader<'a, T>

Converts the Reader into an InitializedReader.

Pends until a value for T is available or resolves immediately if a value is already available. This will not mark the value as seen, InitializedReader::wait_for_update is unaffected by this method.

Trait Implementations§

Source§

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

Source§

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

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

Source§

fn vzip(self) -> V