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
impl Cmd
Sourcepub fn new_optional<F>(f: F) -> Self
pub fn new_optional<F>(f: F) -> Self
Create a command that may not produce a message.
Sourcepub fn blocking<F>(f: F) -> Self
pub fn blocking<F>(f: F) -> Self
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)
})
}Sourcepub fn blocking_result<F, T, E, S, Err>(
f: F,
on_success: S,
on_error: Err,
) -> Self
pub fn blocking_result<F, T, E, S, Err>( f: F, on_success: S, on_error: Err, ) -> Self
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more