pub struct Reader<'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.
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,
impl<T> Reader<'_, T>where
T: Storable + 'static,
Sourcepub fn read<U>(&self, f: impl FnOnce(Option<&T::DataType>) -> U) -> U
pub fn read<U>(&self, f: impl FnOnce(Option<&T::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) -> Option<T::DataType>
pub fn read_cloned(&self) -> Option<T::DataType>
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())
.
Sourcepub async fn wait_for_update(&mut self) -> &mut Self
pub async fn wait_for_update(&mut self) -> &mut Self
Source§impl<'a, T> Reader<'a, T>where
T: Storable + 'static,
impl<'a, T> Reader<'a, T>where
T: Storable + 'static,
Sourcepub async fn wait_init(self) -> InitializedReader<'a, T>
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.