Skip to main content

Model

Trait Model 

Source
pub trait Model: Sized {
    type Message: From<Event> + Send + 'static;

    // Required methods
    fn update(&mut self, msg: Self::Message) -> Cmd<Self::Message>;
    fn view(&self, frame: &mut Frame<'_>);

    // Provided methods
    fn init(&mut self) -> Cmd<Self::Message> { ... }
    fn subscriptions(&self) -> Vec<Box<dyn Subscription<Self::Message>>> { ... }
    fn as_screen_tick_dispatch(&mut self) -> Option<&mut dyn ScreenTickDispatch> { ... }
    fn on_shutdown(&mut self) -> Cmd<Self::Message> { ... }
    fn on_error(&mut self, _error: &str) -> Cmd<Self::Message> { ... }
}
Expand description

The Model trait defines application state and behavior.

Implementations define how the application responds to events and renders its current state.

Required Associated Types§

Source

type Message: From<Event> + Send + 'static

The message type for this model.

Messages represent actions that update the model state. Must be convertible from terminal events.

Required Methods§

Source

fn update(&mut self, msg: Self::Message) -> Cmd<Self::Message>

Update the model in response to a message.

This is the core state transition function. Returns commands for any side effects that should be executed.

Source

fn view(&self, frame: &mut Frame<'_>)

Render the current state to a frame.

Called after updates when the UI needs to be redrawn.

Provided Methods§

Source

fn init(&mut self) -> Cmd<Self::Message>

Initialize the model with startup commands.

Called once when the program starts. Return commands to execute initial side effects like loading data.

Source

fn subscriptions(&self) -> Vec<Box<dyn Subscription<Self::Message>>>

Declare active subscriptions.

Called after each update(). The runtime compares the returned set (by SubId) against currently running subscriptions and starts/stops as needed. Returning an empty vec stops all subscriptions.

§Default

Returns an empty vec (no subscriptions).

Source

fn as_screen_tick_dispatch(&mut self) -> Option<&mut dyn ScreenTickDispatch>

Downcast to ScreenTickDispatch for per-screen tick control.

Override this to return Some(self) in multi-screen Models. The runtime will then consult the active TickStrategy for each inactive screen instead of ticking monolithically.

Default: None (all screens tick every frame, backwards-compatible).

Source

fn on_shutdown(&mut self) -> Cmd<Self::Message>

Called before the runtime exits, whether via Cmd::Quit or signal.

Return cleanup commands (e.g., saving state, closing connections). The runtime executes these before teardown.

§Migration rationale

Source frameworks use componentWillUnmount, useEffect cleanup, or beforeDestroy hooks. This provides an equivalent lifecycle point.

Source

fn on_error(&mut self, _error: &str) -> Cmd<Self::Message>

Called when an unrecoverable error occurs during the runtime loop.

Return commands for error recovery or graceful degradation. The error string contains the error description.

§Migration rationale

Source frameworks use componentDidCatch, error boundaries, or onError hooks. This provides an equivalent error recovery point.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§