Struct state_machine::StateMachine
source · Implementations
sourceimpl<State> StateMachine<State>where
State: PartialEq + Eq + Hash,
impl<State> StateMachine<State>where
State: PartialEq + Eq + Hash,
sourcepub fn new(start_state: State) -> Self
pub fn new(start_state: State) -> Self
Create new StateMachine and init start state
Examples
use StateMachine;
let mut machine = StateMachine::new(States::Empty)
sourcepub fn run(&mut self) -> Result<(), MachineError>
pub fn run(&mut self) -> Result<(), MachineError>
Run state machine loop. if on_enter, update, or on_exit return false loop will be stopped
sourcepub fn next(&mut self) -> bool
pub fn next(&mut self) -> bool
Run current state, if state can run
Examples
use Machine;
use StateMachine;
let mut machine = StateMachine::new(States::Empty);
machine.add(States::Empty, Box::new(EmptyImpl));
machine.add(States::Fire, Box::new(FireImpl));
machine.add(States::Stop, Box::new(StopImpl));
// Run empty state, and switch to next state
machine.next();
sourcepub fn add(
&mut self,
state: State,
machine: Box<dyn Machine<State>>
) -> Option<Box<dyn Machine<State>>>
pub fn add(
&mut self,
state: State,
machine: Box<dyn Machine<State>>
) -> Option<Box<dyn Machine<State>>>
Add new state implementation to machine
If the machine did not have this state present, None
is returned.
If the machine did have this key present, the value is updated, and the old
value is returned. The key is not updated, though; this matters for
types that can be ==
without being identical. See the module-level
documentation for more.
Examples
use Machine;
use StateMachine;
let mut machine = StateMachine::new(States::Empty);
machine.add(States::Fire, Box::new(FireStateImpl));
assert_eq!(machine.add(States::Fire, Box::new(StopStateImpl)), Some(Box<Machine<States>>));
assert_eq!(machine.add(&States::Block), None);
sourcepub fn add_builder(
&mut self,
state: State,
machine: Box<dyn Machine<State>>
) -> &Self
pub fn add_builder(
&mut self,
state: State,
machine: Box<dyn Machine<State>>
) -> &Self
Add new machine
Example
let mut mach = StateMachine::new(1);
mach
.add_builder(2, State2)
.add_builder(3, State3);
sourcepub fn remove(&mut self, state: &State) -> Option<Box<dyn Machine<State>>>
pub fn remove(&mut self, state: &State) -> Option<Box<dyn Machine<State>>>
Remove state from machine, return the machine if the state was previously in machine
Examples
use Machine;
use StateMachine;
let mut machine = StateMachine::new(States::Empty);
machine.add(States::Fire, Box::new(FireStateImpl));
assert_eq!(machine.remove(&States::Fire), Some(Box<Machine<States>>));
assert_eq!(machine.remove(&States::Stop), None);
sourcepub fn contain(&self, state: &State) -> bool
pub fn contain(&self, state: &State) -> bool
Returns true if the machine contains state.
Examples
use Machine;
use StateMachine;
let mut machine = StateMachine::new(States::Empty);
machine.add(States::Fire, Box::new(FireStateImpl));
assert_eq!(machine.contain(&States::Fire), true);
assert_eq!(machine.contain(&States::Stop), false);
sourcepub fn get_machine(&self, state: &State) -> Option<&Box<dyn Machine<State>>>
pub fn get_machine(&self, state: &State) -> Option<&Box<dyn Machine<State>>>
Returns a reference to the value corresponding to the state.
Examples
use Machine;
use StateMachine;
let mut machine = StateMachine::new(States::Empty);
machine.add(States::Fire, Box::new(FireStateImpl));
assert_eq!(machine.contain(&States::Fire), true);
assert_eq!(machine.contain(&States::Stop), false);
assert_eq!(machine.get(&States::Fire), Some(&Box<Machine<States>>));
assert_eq!(machine.get(&States::Empty), None);
sourcepub fn get_state(&self) -> &State
pub fn get_state(&self) -> &State
Return actual state machine state See full example in test. method ‘get’
Examples
use Machine;
use StateMachine;
let mut machine = StateMachine::new(States::Empty)
machine.add(States::Music, Box::new(MusicImpl));
machine.add(States::Empty, Box::new(EmptyImpl));
assert_eq!(*machine.get_state(), States::Empty);
machine.next();
assert_eq!(*machine.get_state(), States::Music);