pub struct ExclusiveReader<'a, T>where
T: Storable + 'static,{ /* private fields */ }
Expand description
Exclusive reader for a Storable
type.
By being the sole reader for a Storable
type, this reader can move the read value out.
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 yet, ExclusiveReader::read
and
ExclusiveReader::take
will return None
.
§Usage
ExclusiveReader::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 ExclusiveReader::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 ExclusiveReader
is only aware of the act of
writing.
§Example
#[veecle_os_runtime::actor]
async fn foo_reader(mut reader: ExclusiveReader<'_, Foo>) -> std::convert::Infallible {
loop {
let value = reader.wait_for_update().await.take();
}
}
Implementations§
Source§impl<T> ExclusiveReader<'_, T>where
T: Storable + 'static,
impl<T> ExclusiveReader<'_, 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 take(&mut self) -> Option<T::DataType>
pub fn take(&mut self) -> Option<T::DataType>
Takes the current value of the type, leaving behind None
.
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())
.