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 fieldset_current_state()— write the current state fieldcan_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§
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)
Sourcefn can_transition(&self, transition: Self::Transition) -> bool
fn can_transition(&self, transition: Self::Transition) -> bool
The unique per-entity transition table.
Provided Methods§
fn new() -> Selfwhere
Self: Default,
fn from_state(state: Self::State) -> Selfwhere
Self: Default,
Sourcefn can_transition_with_role(
&self,
transition: Self::Transition,
role: &str,
) -> bool
fn can_transition_with_role( &self, transition: Self::Transition, role: &str, ) -> bool
Check if a transition is allowed for the given role.
Sourcefn transition(
&mut self,
transition: Self::Transition,
) -> Result<Self::State, StateMachineError>
fn transition( &mut self, transition: Self::Transition, ) -> Result<Self::State, StateMachineError>
Apply a transition, returning the new state on success.
Sourcefn transition_with_role(
&mut self,
transition: Self::Transition,
role: &str,
) -> Result<Self::State, StateMachineError>
fn transition_with_role( &mut self, transition: Self::Transition, role: &str, ) -> Result<Self::State, StateMachineError>
Apply a transition with role authorization check.
Sourcefn available_transitions(&self) -> Vec<Self::Transition>
fn available_transitions(&self) -> Vec<Self::Transition>
All transitions valid from the current state.
Sourcefn available_transitions_for_role(&self, role: &str) -> Vec<Self::Transition>
fn available_transitions_for_role(&self, role: &str) -> Vec<Self::Transition>
All transitions valid from the current state for the given role.
Sourcefn transition_to_state(
&mut self,
target: Self::State,
) -> Result<Self::State, StateMachineError>
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".