Skip to main content

Effect

Struct Effect 

Source
pub struct Effect<Message> { /* private fields */ }
Expand description

Work the runtime performs on behalf of an update, whose result returns as a message.

An effect is a value. It runs nothing when it is built, it cannot touch the model, and it names no executor: which thread runs a closure and what polls a future is the runtime’s, behind a boundary an application never sees. Building one in a test therefore costs nothing and needs no terminal.

use urushi_tui_app::Effect;

enum Message {
    Loaded(usize),
}

let effect = Effect::perform(|| Message::Loaded(1));

§Which constructor

perform carries blocking work and future carries a future, so an application whose I/O is already asynchronous does not wrap it in a thread and one whose work is blocking does not reach for an executor by name. The _latest forms of each carry a Key: starting one replaces an unfinished effect running under the same key, which is the one staleness the runtime knows about on its own. Every other reason a completion no longer applies is the application’s to check in update.

after is the one-shot timer: it waits and then sends its message, reading the runtime’s clock rather than an executor the application brought. after_latest is the same under a key, so a second one restarts the wait rather than adding a second timer — which is what debouncing is. A timer that repeats is Subscription::interval instead, because it lives for as long as the application declares it.

batch starts several effects concurrently and promises nothing about the order their completions arrive in. There is no sequencing combinator: work that must follow other work is returned from the update that receives the first completion.

Implementations§

Source§

impl<Message> Effect<Message>

Source

pub fn none() -> Self

An effect that does nothing.

Source

pub fn shutdown() -> Self

The request to stop the runtime.

The runtime reads the request from update’s return value rather than delivering it, so it never enters admission and is not a message. On it, no sibling effect in the same return value starts, the completions of effects still in flight are discarded, every subscription stops, the terminal session is restored, and the entry point returns the final model. Work that must finish before the application exits is an ordinary effect whose completion is the update that returns this.

Source

pub fn perform<F>(work: F) -> Self
where F: FnOnce() -> Message + Send + 'static,

Blocking work, run off the thread that runs update.

Source

pub fn perform_latest<F>(key: impl Into<Key>, work: F) -> Self
where F: FnOnce() -> Message + Send + 'static,

Blocking work that replaces any unfinished work running under key.

Source

pub fn future<F>(future: F) -> Self
where F: Future<Output = Message> + Send + 'static,

Asynchronous work.

Source

pub fn future_latest<F>(key: impl Into<Key>, future: F) -> Self
where F: Future<Output = Message> + Send + 'static,

Asynchronous work that replaces any unfinished work running under key.

Source

pub fn after<F>(delay: Duration, f: F) -> Self
where F: FnOnce(Instant) -> Message + Send + 'static,

A message sent once delay has passed, timed by the runtime’s clock.

The clock is the runtime’s, which is what keeps a waiting application free of an executor of its own and what lets a test drive the wait by hand instead of sleeping.

Source

pub fn after_latest<F>(key: impl Into<Key>, delay: Duration, f: F) -> Self
where F: FnOnce(Instant) -> Message + Send + 'static,

A delayed message that replaces any unfired timer under key.

Replacing a timer restarts its wait, so an update that returns this on every keystroke fires once the keystrokes stop: the debounce a search field or a live preview needs, with no state in the model.

Source

pub fn batch(effects: impl IntoIterator<Item = Self>) -> Self

Several effects, started concurrently.

Their completions may arrive in any order.

Source

pub fn map<To>( self, f: impl Fn(Message) -> To + Send + Sync + 'static, ) -> Effect<To>
where Message: Send + 'static, To: Send + 'static,

The same effect with its message passed through f.

This is what lets a program hold another program: a parent whose message wraps a child’s calls the child’s update, receives the child’s effect, and returns it mapped, without the child knowing the parent’s message type.

Trait Implementations§

Source§

impl<Message> Debug for Effect<Message>

Source§

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

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

impl<Message> Default for Effect<Message>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<Message> Freeze for Effect<Message>

§

impl<Message> !RefUnwindSafe for Effect<Message>

§

impl<Message> Send for Effect<Message>

§

impl<Message> !Sync for Effect<Message>

§

impl<Message> Unpin for Effect<Message>

§

impl<Message> UnsafeUnpin for Effect<Message>

§

impl<Message> !UnwindSafe for Effect<Message>

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, 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.