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§
Required Methods§
Provided Methods§
Sourcefn init(&mut self) -> Cmd<Self::Message>
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.
Sourcefn subscriptions(&self) -> Vec<Box<dyn Subscription<Self::Message>>>
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).
Sourcefn as_screen_tick_dispatch(&mut self) -> Option<&mut dyn ScreenTickDispatch>
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).
Sourcefn on_shutdown(&mut self) -> Cmd<Self::Message>
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.
Sourcefn on_error(&mut self, _error: &str) -> Cmd<Self::Message>
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.