Struct InitializedReader

Source
pub struct InitializedReader<'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.

This reader can be requested directly as an actor input in simple cases, this will mean your actor does not start running until all InitializedReaders it takes have been initialized by their writers. If you need to do something more complex (e.g. you have interdependencies between actors so one must write an initial value earlier) then you can take a Reader and convert via Reader::wait_init when ready. By ensuring the presence of a value for T has been written at least once, this reader avoids Option when reading.

§Example

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

#[veecle_os_runtime::actor]
async fn foo_reader_complex(mut reader: Reader<'_, Foo>) -> std::convert::Infallible {
    // Do some initialization that must be completed before waiting for the reader to have an initial value.
    let mut reader = reader.wait_init().await;
    loop {
        let processed_value = reader.wait_for_update().await.read(|value: &Foo| {
            // Do something with the value.
        });
    }
}

Implementations§

Source§

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

Source

pub fn read<U>(&self, f: impl FnOnce(&<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) -> <T as Storable>::DataType
where <T as Storable>::DataType: Clone,

Reads and clones the current value of a type.

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 InitializedReader<'_, 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 InitializedReader<'_, T>
where T: Storable,

Source§

type ToBeRead = <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 InitializedReader<'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 InitializedReader<'a, T>
where T: Storable + 'static,

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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