Skip to main content

Cmd

Struct Cmd 

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

A command that produces a message when executed.

Commands are lazy - they don’t execute until the program runs them. This allows for pure update functions that return commands without side effects.

§Example

use bubbletea::{Cmd, Message};
use std::time::Duration;

// A command that produces a message after a delay
fn delayed_message() -> Cmd {
    Cmd::new(|| {
        std::thread::sleep(Duration::from_secs(1));
        Message::new("done")
    })
}

Implementations§

Source§

impl Cmd

Source

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

Create a new command from a closure.

Source

pub fn new_optional<F>(f: F) -> Self
where F: FnOnce() -> Option<Message> + Send + 'static,

Create a command that may not produce a message.

Source

pub fn none() -> Option<Self>

Create an empty command that does nothing.

Source

pub fn execute(self) -> Option<Message>

Execute the command and return the resulting message.

Source

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

Create a command that performs blocking I/O.

This is semantically equivalent to Cmd::new() but makes the blocking intent explicit. When the async feature is enabled, blocking commands are automatically run on tokio’s blocking thread pool via spawn_blocking.

Use this for operations like:

  • File I/O (std::fs::read, std::fs::write)
  • Network operations with blocking APIs
  • CPU-intensive computations
  • Thread sleep operations
§Example
use bubbletea::{Cmd, Message};

fn read_config() -> Cmd {
    Cmd::blocking(|| {
        let content = std::fs::read_to_string("config.toml").unwrap();
        Message::new(content)
    })
}
Source

pub fn blocking_result<F, T, E, S, Err>( f: F, on_success: S, on_error: Err, ) -> Self
where F: FnOnce() -> Result<T, E> + Send + 'static, S: FnOnce(T) -> Message + Send + 'static, Err: FnOnce(E) -> Message + Send + 'static,

Create a command that performs a blocking operation returning a Result.

Converts Result<T, E> into a message, wrapping both success and error cases. This is convenient for I/O operations that can fail.

§Example
use bubbletea::{Cmd, Message};
use std::io;

struct FileContent(String);
struct FileError(io::Error);

fn read_file(path: &'static str) -> Cmd {
    Cmd::blocking_result(
        move || std::fs::read_to_string(path),
        |content| Message::new(FileContent(content)),
        |err| Message::new(FileError(err)),
    )
}

Auto Trait Implementations§

§

impl Freeze for Cmd

§

impl !RefUnwindSafe for Cmd

§

impl Send for Cmd

§

impl !Sync for Cmd

§

impl Unpin for Cmd

§

impl !UnwindSafe for Cmd

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