Skip to main content

StateMachineBehavior

Trait StateMachineBehavior 

Source
pub trait StateMachineBehavior: Sized {
    type State: Copy + PartialEq + Default + Display;
    type Transition: TransitionMeta<Self::State>;

    // Required methods
    fn current_state(&self) -> Self::State;
    fn set_current_state(&mut self, state: Self::State);
    fn can_transition(&self, transition: Self::Transition) -> bool;

    // Provided methods
    fn new() -> Self
       where Self: Default { ... }
    fn from_state(state: Self::State) -> Self
       where Self: Default { ... }
    fn can_transition_with_role(
        &self,
        transition: Self::Transition,
        role: &str,
    ) -> bool { ... }
    fn transition(
        &mut self,
        transition: Self::Transition,
    ) -> Result<Self::State, StateMachineError> { ... }
    fn transition_with_role(
        &mut self,
        transition: Self::Transition,
        role: &str,
    ) -> Result<Self::State, StateMachineError> { ... }
    fn available_transitions(&self) -> Vec<Self::Transition> { ... }
    fn available_transitions_for_role(
        &self,
        role: &str,
    ) -> Vec<Self::Transition> { ... }
    fn transition_to_state(
        &mut self,
        target: Self::State,
    ) -> Result<Self::State, StateMachineError> { ... }
}
Expand description

Trait that provides all mechanical state machine methods as default impls.

Implementors only need to define three methods:

  • current_state() — read the current state field
  • set_current_state() — write the current state field
  • can_transition() — the unique per-entity transition table

All other methods (transition, transition_with_role, available_transitions, available_transitions_for_role, transition_to_state, can_transition_with_role) are provided for free.

Required Associated Types§

Required Methods§

Source

fn current_state(&self) -> Self::State

Source

fn set_current_state(&mut self, state: Self::State)

Source

fn can_transition(&self, transition: Self::Transition) -> bool

The unique per-entity transition table.

Provided Methods§

Source

fn new() -> Self
where Self: Default,

Source

fn from_state(state: Self::State) -> Self
where Self: Default,

Source

fn can_transition_with_role( &self, transition: Self::Transition, role: &str, ) -> bool

Check if a transition is allowed for the given role.

Source

fn transition( &mut self, transition: Self::Transition, ) -> Result<Self::State, StateMachineError>

Apply a transition, returning the new state on success.

Source

fn transition_with_role( &mut self, transition: Self::Transition, role: &str, ) -> Result<Self::State, StateMachineError>

Apply a transition with role authorization check.

Source

fn available_transitions(&self) -> Vec<Self::Transition>

All transitions valid from the current state.

Source

fn available_transitions_for_role(&self, role: &str) -> Vec<Self::Transition>

All transitions valid from the current state for the given role.

Source

fn transition_to_state( &mut self, target: Self::State, ) -> Result<Self::State, StateMachineError>

Transition directly to a target state by finding any valid transition that leads there from the current state.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§