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,
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.
pub fn read<U>( &mut self, f: impl FnOnce(Option<&<T as Storable>::DataType>) -> U, ) -> U
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.
pub async fn read_updated<U>( &mut self, f: impl FnOnce(&<T as Storable>::DataType) -> U, ) -> U
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>
Available on crate feature data-support-can only.
pub fn read_cloned(&mut self) -> Option<<T as Storable>::DataType>
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
Available on crate feature data-support-can only.
pub async fn read_updated_cloned(&mut self) -> <T as Storable>::DataType
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.
pub fn is_updated(&self) -> bool
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.
pub async fn wait_for_update(&mut self) -> &mut Reader<'_, T>
data-support-can only.Trait Implementations§
§impl<T> CombinableReader for Reader<'_, T>where
T: Storable,
impl<T> CombinableReader for Reader<'_, T>where
T: Storable,
§type ToBeRead = Option<<T as Storable>::DataType>
type ToBeRead = Option<<T as Storable>::DataType>
CombineReaders::read callback.