Skip to main content

TopState

Trait TopState 

Source
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§

Source

type State: StateEnum

The state enum type. Auto-filled by the state_machine macro.

Source

type Event: StateMachineEvent

The event type. Auto-filled by the state_machine macro.

Provided Methods§

Source

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
        }
    }
// ...
Source

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
        }
    }
// ...
Source

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
        }
    }
// ...
Source

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.

Implementors§