pub struct Writer<'a, T>where
T: Storable + 'static,{ /* private fields */ }
Expand description
Writer for a Storable
type.
Allows Actor
s to write a particular type read by another actor.
The generic type T
from the writer specifies the type of the value that is being written.
§Usage
All Reader
s are guaranteed to be able to observe every write.
For this reason, Writer::write
is an async method.
It will resolve once all Actor
s awaiting a Reader
for the same type had the chance to read the value.
Typically, this only occurs when trying to write two values back to back.
If all Reader
s already had the chance to read the value, Writer::write
will resolve immediately.
The same is true for Writer::modify
.
§Examples
// Writing a value.
#[veecle_os_runtime::actor]
async fn foo_writer(mut writer: Writer<'_, Foo>) -> std::convert::Infallible {
loop {
// This call will yield to any readers needing to read the last value.
writer.write(Foo::default()).await;
}
}
// Modifying a value.
#[veecle_os_runtime::actor]
async fn foo_writer(
mut writer: Writer<'_, Foo>,
) -> std::convert::Infallible {
loop {
// This call will yield to any readers needing to read the last value.
// The closure will run after yielding and right before continuing to the rest of the function.
writer.modify(|previous_value: &mut Option<Foo>| {
// mutate the previous value
}).await;
}
}
Writer::ready
allows separating the “waiting” from the “writing”,
After Writer::ready
returns, the next write or modification will happen immediately.
#[veecle_os_runtime::actor]
async fn foo_writer(mut writer: Writer<'_, Foo>) -> std::convert::Infallible {
loop {
// This call may yield to any readers needing to read the last value.
writer.ready().await;
// This call will return immediately.
writer.write(Foo::default()).await;
// This call will yield to any readers needing to read the last value.
writer.write(Foo::default()).await;
}
}
Implementations§
Source§impl<T> Writer<'_, T>where
T: Storable + 'static,
impl<T> Writer<'_, T>where
T: Storable + 'static,
Sourcepub async fn ready(&mut self)
pub async fn ready(&mut self)
Waits for the writer to be ready to perform a write operation.
After awaiting this method, the next call to Writer::write()
or Writer::modify()
is guaranteed to resolve immediately.