Struct Writer

Source
pub struct Writer<'a, T>
where T: Storable + 'static,
{ /* private fields */ }
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;
    }
}

Implementations§

Source§

impl<T> Writer<'_, T>
where T: Storable + 'static,

Source

pub async fn write(&mut self, item: T::DataType)

Writes a new value and notifies readers.

Source

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.

Source

pub async fn modify(&mut self, f: impl FnOnce(&mut Option<T::DataType>))

Updates the value in-place and notifies readers.

Source

pub fn read<U>(&self, f: impl FnOnce(Option<&T::DataType>) -> U) -> U

Reads the current value of a type.

This method takes a closure to ensure the reference is not held across await points.

Trait Implementations§

Source§

impl<'a, T> Debug for Writer<'a, T>
where T: Storable + 'static + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> StoreRequest<'a> for Writer<'a, T>
where T: Storable + 'static,

Auto Trait Implementations§

§

impl<'a, T> Freeze for Writer<'a, T>

§

impl<'a, T> !RefUnwindSafe for Writer<'a, T>

§

impl<'a, T> !Send for Writer<'a, T>

§

impl<'a, T> !Sync for Writer<'a, T>

§

impl<'a, T> Unpin for Writer<'a, T>

§

impl<'a, T> !UnwindSafe for Writer<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V