Skip to main content

Cmd

Enum Cmd 

Source
pub enum Cmd<Msg> {
    None,
    Batch(Vec<Cmd<Msg>>),
    Task(Pin<Box<dyn Future<Output = Msg> + Send + 'static>>),
    Msg(Msg),
}
Expand description

A command representing a side effect to be executed

Commands are declarative descriptions of side effects. The runtime executes them and feeds resulting messages back into the update loop.

Variants§

§

None

No side effect

§

Batch(Vec<Cmd<Msg>>)

Multiple commands to execute

§

Task(Pin<Box<dyn Future<Output = Msg> + Send + 'static>>)

An async task that produces a message

§

Msg(Msg)

Emit a message immediately (next frame)

Implementations§

Source§

impl<Msg> Cmd<Msg>

Source

pub fn none() -> Self

Create an empty command (no side effect)

Source

pub fn batch(cmds: impl IntoIterator<Item = Cmd<Msg>>) -> Self

Create a batch of commands

Source

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

Create a command from an async task

Source

pub fn msg(msg: Msg) -> Self

Create a command that emits a message immediately

Source

pub fn map<F, NewMsg>(self, f: F) -> Cmd<NewMsg>
where F: Fn(Msg) -> NewMsg + Send + Sync + Clone + 'static, Msg: Send + 'static, NewMsg: Send + 'static,

Map the message type

Source§

impl<Msg> Cmd<Msg>

Source

pub fn is_none(&self) -> bool

Check if this is Cmd::None

Source

pub fn is_task(&self) -> bool

Check if this is Cmd::Task

Source

pub fn is_msg(&self) -> bool

Check if this is Cmd::Msg

Source

pub fn is_batch(&self) -> bool

Check if this is Cmd::Batch

Source

pub fn unwrap_msg(self) -> Msg

Get the message if this is Cmd::Msg, panics otherwise

§Panics

Panics if the command is not Cmd::Msg

Source

pub fn as_msg(&self) -> Option<&Msg>

Get the message if this is Cmd::Msg

Source

pub fn as_batch(&self) -> Option<&[Cmd<Msg>]>

Get the batch if this is Cmd::Batch

Source

pub fn len(&self) -> usize

Get the number of commands (1 for non-batch, n for batch, 0 for none)

Source

pub fn is_empty(&self) -> bool

Check if empty (only true for Cmd::None)

Source§

impl<Msg: PartialEq> Cmd<Msg>

Source

pub fn is_msg_eq(&self, expected: &Msg) -> bool

Check if this is Cmd::Msg with a specific message

Source§

impl<Msg: Debug> Cmd<Msg>

Source

pub fn assert_none(&self)

Assert this is Cmd::None, panics with debug info otherwise

Source

pub fn assert_task(&self)

Assert this is Cmd::Task, panics with debug info otherwise

Source

pub fn assert_msg(&self)

Assert this is Cmd::Msg, panics with debug info otherwise

Source§

impl<Msg: Send + 'static> Cmd<Msg>

Source

pub fn try_task<F, T, E, FnOk, FnErr>( future: F, on_ok: FnOk, on_err: FnErr, ) -> Self
where F: Future<Output = Result<T, E>> + Send + 'static, FnOk: FnOnce(T) -> Msg + Send + 'static, FnErr: FnOnce(E) -> Msg + Send + 'static,

Create a command from an async task that returns Result

§Example
Cmd::try_task(
    async { fetch_user(id).await },
    |user| Msg::UserLoaded(user),
    |err| Msg::Error(err.to_string()),
)
Source

pub fn from_result<T, E, FnOk, FnErr>( result: Result<T, E>, on_ok: FnOk, on_err: FnErr, ) -> Self
where FnOk: FnOnce(T) -> Msg, FnErr: FnOnce(E) -> Msg,

Create a command from a Result, converting to Msg immediately

§Example
Cmd::from_result(
    parse_config(),
    |config| Msg::ConfigLoaded(config),
    |err| Msg::Error(err.to_string()),
)

Trait Implementations§

Source§

impl<Msg> Default for Cmd<Msg>

Source§

fn default() -> Cmd<Msg>

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

Auto Trait Implementations§

§

impl<Msg> Freeze for Cmd<Msg>
where Msg: Freeze,

§

impl<Msg> !RefUnwindSafe for Cmd<Msg>

§

impl<Msg> Send for Cmd<Msg>
where Msg: Send,

§

impl<Msg> !Sync for Cmd<Msg>

§

impl<Msg> Unpin for Cmd<Msg>
where Msg: Unpin,

§

impl<Msg> UnsafeUnpin for Cmd<Msg>
where Msg: UnsafeUnpin,

§

impl<Msg> !UnwindSafe for Cmd<Msg>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more