crux_core::capability

Struct CapabilityContext

source
pub struct CapabilityContext<Op, Event>
where Op: Operation,
{ /* private fields */ }
Expand description

An interface for capabilities to interact with the app and the shell.

To use update_app, notify_shell or request_from_shell, spawn a task first.

For example (from crux_time)


pub fn get<F>(&self, callback: F)
where
    F: FnOnce(TimeResponse) -> Ev + Send + Sync + 'static,
{
    let ctx = self.context.clone();
    self.context.spawn(async move {
        let response = ctx.request_from_shell(TimeRequest).await;

        ctx.update_app(callback(response));
    });
}

Implementations§

source§

impl<Op, Ev> CapabilityContext<Op, Ev>
where Op: Operation, Ev: 'static,

source

pub fn request_from_shell(&self, operation: Op) -> ShellRequest<Op::Output>

Send an effect request to the shell, expecting an output. The provided operation describes the effect input in a serialisable fashion, and must implement the Operation trait to declare the expected output type.

request_from_shell returns a future of the output, which can be awaited. You should only call this method inside an async task created with CapabilityContext::spawn.

source§

impl<Op, Ev> CapabilityContext<Op, Ev>
where Op: Operation, Ev: 'static,

source

pub fn stream_from_shell(&self, operation: Op) -> ShellStream<Op::Output>

Send an effect request to the shell, expecting a stream of responses

source§

impl<Op, Ev> CapabilityContext<Op, Ev>
where Op: Operation, Ev: 'static,

source

pub fn spawn(&self, f: impl Future<Output = ()> + 'static + Send)

Spawn a task to do the asynchronous work. Within the task, async code can be used to interact with the Shell and the App.

source

pub async fn notify_shell(&self, operation: Op)

Send an effect request to the shell in a fire and forget fashion. The provided operation does not expect anything to be returned back.

source

pub fn update_app(&self, event: Ev)

Send an event to the app. The event will be processed on the next run of the update loop. You can call update_app several times, the events will be queued up and processed sequentially after your async task either awaits or finishes.

source

pub fn map_event<NewEv, F>(&self, func: F) -> CapabilityContext<Op, NewEv>
where F: Fn(NewEv) -> Ev + Sync + Send + 'static, NewEv: 'static,

Transform the CapabilityContext into one which uses the provided function to map each event dispatched with update_app to a different event type.

This is useful when composing apps from modules to wrap a submodule’s event type with a specific variant of the parent module’s event, so it can be forwarded to the submodule when received.

In a typical case you would implement From on the submodule’s Capabilities type

impl From<&Capabilities> for child::Capabilities {
   fn from(incoming: &Capabilities) -> Self {
       child::Capabilities {
           some_capability: incoming.some_capability.map_event(Event::Submodule),
           render: incoming.render.map_event(Event::Submodule),
       }
   }
}

in the parent module’s update function, you can then call .into() on the capabilities, before passing them down to the submodule.

Trait Implementations§

source§

impl<Op, Ev> Clone for CapabilityContext<Op, Ev>
where Op: Operation,

source§

fn clone(&self) -> Self

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

Auto Trait Implementations§

§

impl<Op, Event> Freeze for CapabilityContext<Op, Event>

§

impl<Op, Event> !RefUnwindSafe for CapabilityContext<Op, Event>

§

impl<Op, Event> Send for CapabilityContext<Op, Event>

§

impl<Op, Event> Sync for CapabilityContext<Op, Event>

§

impl<Op, Event> Unpin for CapabilityContext<Op, Event>

§

impl<Op, Event> !UnwindSafe for CapabilityContext<Op, Event>

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.