StateMachine

Struct StateMachine 

Source
pub struct StateMachine<S, E>
where S: Clone + Hash + Eq + Debug, E: Hash + Eq + Debug,
{ pub transitions: HashMap<S, StateMachineTransitions<S, E>>, /* private fields */ }
Expand description

The primitive state machine type that holds all context

  • List of available states
  • Maps of input events per state. An event not in the list is ignored by default
  • Maps of transition functions per input event It also holds the live status of the state machine:
  • Queue with incoming input events to be processed
  • Current state

Important Note: This type is used to implement the async (and later the sync) versions of the generic state machine You should not use this type for most of use cases. Use the async (or sync) implementations instead!

Fields§

§transitions: HashMap<S, StateMachineTransitions<S, E>>

Implementations§

Source§

impl<S, E> StateMachine<S, E>
where S: Clone + Hash + Eq + Debug, E: Hash + Eq + Debug,

Source

pub fn new() -> Self

Create a new empty state machine The state machine needs to be confifures properly using:

   use generic_state_machine::primitives::StateMachine;

   let mut fsm = StateMachine::<String, String>::new();
   fsm.add_states(&mut vec![
       "alpha".to_string(),
       "beta".to_string(),
       "gamma".to_string(),
       ])
       .add_transition("alpha".to_string(), "beta".to_string(), |_, _| {
           "beta".to_string()
       })
       .initial_state("alpha".to_string());

   println!("{:?}", fsm);
Source

pub fn current_state(&self) -> Result<S, Error>

Get the current state

Source

pub fn initial_state(&mut self, state: S) -> Result<&mut Self, Error>

Source

pub fn add_states(&mut self, states: &[S]) -> &mut Self

Add the provided states to the list of available states

Source

pub fn add_transition( &mut self, state: S, event: E, function: TransitionFunction<S, E>, ) -> &mut Self

Add the provided function as the transition function to be executed if the machine is in the provided state and receives the provided input event

Source

pub fn execute(&mut self, event: E) -> Result<S, Error>

Executes the received event Checks if a transition function is provided for the current state and for the incoming event It calls the function and sets the state to the output of the transition function. If no transition function is available, the incoming event is dropped.

Trait Implementations§

Source§

impl<S, E> Debug for StateMachine<S, E>
where S: Clone + Hash + Eq + Debug + Debug, E: Hash + Eq + Debug + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S, E> Default for StateMachine<S, E>
where S: Clone + Hash + Eq + Debug, E: Hash + Eq + Debug,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<S, E> !Freeze for StateMachine<S, E>

§

impl<S, E> RefUnwindSafe for StateMachine<S, E>

§

impl<S, E> Send for StateMachine<S, E>
where S: Send, E: Send,

§

impl<S, E> Sync for StateMachine<S, E>
where S: Send + Sync, E: Sync,

§

impl<S, E> Unpin for StateMachine<S, E>
where S: Unpin, E: Unpin,

§

impl<S, E> UnwindSafe for StateMachine<S, E>
where S: UnwindSafe, E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.