Trait kaori_hsm::State

source ·
pub trait State<Tag>
where Self: TopState,
{ // Required methods fn get_parent_state() -> ParentState<Self>; fn handle(&mut self, evt: &<Self as TopState>::Evt) -> HandleResult<Self>; // Provided methods fn init(&mut self) -> InitResult<Self> { ... } fn entry(&mut self) { ... } fn exit(&mut self) { ... } }
Expand description

Generic trait which must be implemented on the state machine structure for defining each of its states.

The tag argument, which corresponds to the state name, has no other purpose than to create a unique variant of this trait for the specific state to implement. If the #[state()] procedural macro is used, the tag is automatically defined by parsing the state implementation.

§Example

#[state(super_state= Top)]
impl State<S0> for BasicStateMachine{

   fn init(&mut self) -> InitResult<Self> {
       println!("S0-INIT");
       init_transition!(S1)
   }

   fn exit(&mut self) {
       println!("S0-EXIT");
   }

   fn entry(&mut self) {
       println!("S0-ENTRY");
   }

   fn handle(&mut self, evt: & BasicEvt) -> HandleResult<Self> {
       match evt{
           BasicEvt::A => {
               println!("S0-HANDLES-A");
               handled!()
           },
           BasicEvt::B => {
               println!("S0-HANDLES-D");
               transition!(S1)
          },
          _ => ignored!()
       }
   }    
}

Note: It is recommended to use the #[state()] procedural macro before the state implementation in order to limit code verbosity.

Required Methods§

source

fn get_parent_state() -> ParentState<Self>

Return the parent state of this state, which can be either the top state or another user-defined state.

§Implementation policy

Must be implemented for every state

Note: This method is automatically implemented if you use the #[state()] procedural macro

source

fn handle(&mut self, evt: &<Self as TopState>::Evt) -> HandleResult<Self>

Events injected into the state machine through the StateMachine::dispatch() method are received by this method if the present state is the current state of the state machine.

§Implementation policy

No default implementation, must be implemented for every state. This method implementation is typically a match statement on the event variant. The handling of each event may return either:

  • HandleResult::Transition: Immediately trigger a transition to the target state, which may become the next current state of the state machine.
  • HandleResult::Handled: The event is handled without transition.
  • HandleResult::Ignored: the event is dispatched to the parent state.
    Note: It is recommended to use the provided transition!(), handled!() and ignored!() macros instead of assembling manually the enum variants of HandleResult

Provided Methods§

source

fn init(&mut self) -> InitResult<Self>

Define the operations to perform when the initial transition of a state is triggered. Is called when a transition targets the present state, after its entry statement has been executed.

§Implementation policy

Leaving the default implementation is mandatory for all leaf states (states without children) but all non-leaf state must implement this method. The implemented method must return only TargetState::InitResult variant containing the target substate.
Note: It is recommended to use the ìnit_transition!() macro for returning the target substate.

source

fn entry(&mut self)

Executed when the state machine enters the present state during a transition.

§Implementation policy

The implementation of this method is optional.

source

fn exit(&mut self)

Executed when the state machine exits the present state during a transition.

§Implementation policy

The implementation of this method is optional.

Object Safety§

This trait is not object safe.

Implementors§