Struct Container

Source
pub struct Container(/* private fields */);
Expand description

Containers store the current data and state of the data flow graph created by capsules and their dependencies/dependents. See the README for more.

Implementations§

Source§

impl Container

Source

pub fn new() -> Self

Initializes a new Container.

Containers contain no data when first created. Use Container::read to populate and read some capsules!

Source

pub fn read<Capsules: CapsulesWithCloneRead>( &self, capsules: Capsules, ) -> Capsules::Data

Performs a consistent read on all supplied capsules that have cloneable data.

Consistency is important here: if you need the current data from a few different capsules, do not read them individually, but rather group them together with one read() call. If you read capsules one at a time, there will be increased overhead in addition to possible inconsistency (say if you read one capsule and then the container is updated right after).

§Concurrency

First attempts to grab a read lock; if any of the requested capsules are not initialized, falls back to grabbing a write lock.

Source

pub fn read_ref<Capsules, Callback, CallbackReturn>( &self, capsules: Capsules, callback: Callback, ) -> CallbackReturn
where Capsules: CapsulesWithRefRead, Callback: FnOnce(Capsules::Data<'_>) -> CallbackReturn,

Performs a consistent (ref) read on the supplied capsules.

Consistency is important here: if you need the current data from a few different capsules, do not read them individually, but rather group them together with one read_ref() call. If you read capsules one at a time, there will be increased overhead in addition to possible inconsistency (say if you read one capsule and then the container is updated right after).

It is typically recommended to use Container::read when your capsule data is Clone.

§Concurrency

First attempts to grab a read lock; if any of the requested capsules are not initialized, falls back to grabbing a write lock, and will downgrade the write lock to a read lock once initialized.

The callback will be invoked while holding a read lock on the container, so it is best to keep the callback on the quicker side (unless you don’t mind blocking side effect updates and uninitialized reads).

Source

pub fn listen<Effect, EffectFactory, Listener>( &self, effect_factory: EffectFactory, listener: Listener, ) -> ListenerHandle
where Effect: SideEffect, EffectFactory: 'static + Send + Fn() -> Effect, Listener: Fn(CapsuleReader<'_, '_>, <Effect as SideEffect>::Api<'_>) + Send + 'static,

Provides a mechanism to temporarily listen to changes in some capsule(s). The provided listener is called once at the time of the listener’s registration, and then once again everytime a dependency changes.

Returns a ListenerHandle, which doesn’t do anything other than implement Drop, and its Drop implementation will remove listener from the Container.

Thus, if you want the handle to live for as long as the Container itself, it is instead recommended to create a “non-idempotent” capsule (use the effects::as_listener() side effect) that acts as your listener. When you normally would call Container::listen(), instead call container.read(my_non_idempotent_listener) to initialize it.

§Concurrency

Internally tries to grab a write lock, so this function is blocking.

§Panics

Panics if you attempt to register the same listener twice, before the first ListenerHandle is dropped.

Trait Implementations§

Source§

impl Clone for Container

Source§

fn clone(&self) -> Container

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Container

Source§

fn default() -> Container

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<T> CData for T
where T: Clone + Send + Sync + 'static,