pub struct InitializedReader<'a, T>where
T: Storable + 'static,{ /* private fields */ }
Expand description
Reader for a Storable
type.
Allows Actor
s 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 InitializedReader
s 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,
impl<T> InitializedReader<'_, T>where
T: Storable + 'static,
Sourcepub fn read<U>(&self, f: impl FnOnce(&<T as Storable>::DataType) -> U) -> U
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.
Sourcepub fn read_cloned(&self) -> <T as Storable>::DataType
pub fn read_cloned(&self) -> <T as Storable>::DataType
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())
.