pub struct Writer<'a, T>where
T: Storable + 'static,{
slot: Pin<&'a Slot<T>>,
waiter: Waiter<'a>,
marker: PhantomData<fn(T)>,
}Expand description
Writer for a Storable type.
Allows Actors 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 Readers are guaranteed to be able to observe every write.
For this reason, Writer::write is an async method.
It will resolve once all Actors 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 Readers 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;
}
}Fields§
§slot: Pin<&'a Slot<T>>§waiter: Waiter<'a>§marker: PhantomData<fn(T)>Implementations§
Source§impl<T> Writer<'_, T>where
T: Storable + 'static,
impl<T> Writer<'_, T>where
T: Storable + 'static,
Sourcepub async fn write(&mut self, item: <T as Storable>::DataType)
Available on crate feature data-support-can only.
pub async fn write(&mut self, item: <T as Storable>::DataType)
data-support-can only.Writes a new value and notifies readers.
Sourcepub async fn ready(&mut self)
Available on crate feature data-support-can only.
pub async fn ready(&mut self)
data-support-can only.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.