pub trait TopState: Sized {
type State: StateEnum;
type Event: StateMachineEvent;
// Provided methods
fn init(&mut self) -> impl Into<Next<Self::State>> { ... }
fn update(&mut self) -> impl Into<Next<Self::State>> { ... }
fn top_down_update(&mut self) -> impl Into<Next<Self::State>> { ... }
fn handle_event(
&mut self,
event: &Self::Event,
) -> impl Into<Next<Self::State>> { ... }
}Expand description
The topmost state of a StateMachine.
Implement this trait for exactly one state in a state_machine module.
§Example
#[moku::state_machine]
mod example {
#[moku::machine_module]
mod machine {}
use machine::State;
struct Top {
counter: u32,
}
impl moku::TopState for Top {
fn init(&mut self) -> impl Into<moku::Next<Self::State>> {
State::Foo
}
}
struct Foo;
impl moku::Substate<Top> for Foo {}
}Required Associated Types§
Sourcetype State: StateEnum
type State: StateEnum
The state enum type. Auto-filled by the state_machine macro.
Sourcetype Event: StateMachineEvent
type Event: StateMachineEvent
The event type. Auto-filled by the state_machine macro.
Provided Methods§
Sourcefn init(&mut self) -> impl Into<Next<Self::State>>
fn init(&mut self) -> impl Into<Next<Self::State>>
Called when a StateMachine initializes (upon calling StateMachineBuilder::build) and
upon transitions directly to this state.
This method can return a target state representing the initial transition to take upon entry of this state.
Returning a value of Next::None results in the StateMachine remaining in this state after
transition.
§Example
// ...
pub struct Top;
impl TopState for Top {
fn init(&mut self) -> impl Into<Next<Self::State>> {
State::Foo
}
}
// ...Sourcefn update(&mut self) -> impl Into<Next<Self::State>>
fn update(&mut self) -> impl Into<Next<Self::State>>
Called when StateMachine::update is called.
This method may return a target state to transition to as a result of updating.
§Example
// ...
pub struct Top;
impl TopState for Top {
fn update(&mut self) -> impl Into<Next<Self::State>> {
State::Foo
}
}
// ...Sourcefn top_down_update(&mut self) -> impl Into<Next<Self::State>>
fn top_down_update(&mut self) -> impl Into<Next<Self::State>>
Called when StateMachine::top_down_update is called.
This method may return a target state to transition to as a result of updating.
§Example
// ...
pub struct Top;
impl TopState for Top {
fn top_down_update(&mut self) -> impl Into<Next<Self::State>> {
State::Foo
}
}
// ...Sourcefn handle_event(&mut self, event: &Self::Event) -> impl Into<Next<Self::State>>
fn handle_event(&mut self, event: &Self::Event) -> impl Into<Next<Self::State>>
Called when StateMachine::handle_event is called.
This is the final state called during event handling.
Return Next::None to make no transition.
§Example
// ...
pub struct Top;
impl TopState for Top {
fn handle_event(&mut self, event: &Self::Event) -> impl Into<Next<Self::State>> {
match event {
Event::A => Next::Target(State::Foo),
_ => Next::None,
}
}
}
// ...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.