pub struct StateMachine<A, E, F, S> { /* private fields */ }
Expand description
A simple, generic state machine. The argument can be
- an owned value which implements
Clone
- an immutable reference
- a mutable reference
In case 3 you need to specify the type of the
state machine by either writing StateMachine<Ref<str>, _, _, _>
or by using StateMachineRef
.
Implementations§
Source§impl<A, E, F, S> StateMachine<A, E, F, S>
impl<A, E, F, S> StateMachine<A, E, F, S>
Sourcepub fn new(initial: S) -> Self
pub fn new(initial: S) -> Self
Creates a new state machine with a give initial
state.
Examples found in repository?
More examples
38fn run() -> Result<(), ()> {
39 // note that we use `StateMachineRef` here.
40 let mut machine = StateMachineRef::new(HelloState::Hello);
41
42 let mut context = 5;
43 machine.start(&mut context)?;
44 machine.update(&mut context)?;
45 machine.update(&mut context)?;
46
47 assert!(!machine.running());
48
49 Ok(())
50}
Sourcepub fn running(&self) -> bool
pub fn running(&self) -> bool
Checks if the state machine is running.
Examples found in repository?
More examples
38fn run() -> Result<(), ()> {
39 // note that we use `StateMachineRef` here.
40 let mut machine = StateMachineRef::new(HelloState::Hello);
41
42 let mut context = 5;
43 machine.start(&mut context)?;
44 machine.update(&mut context)?;
45 machine.update(&mut context)?;
46
47 assert!(!machine.running());
48
49 Ok(())
50}
Source§impl<A, E, F, S> StateMachine<Ref<A>, E, F, S>
impl<A, E, F, S> StateMachine<Ref<A>, E, F, S>
Sourcepub fn start(&mut self, args: &mut A) -> Result<(), E>
pub fn start(&mut self, args: &mut A) -> Result<(), E>
Starts the state machine, calling .start
on the initial state.
§Panics
Panics if the state machine is running already.
Examples found in repository?
38fn run() -> Result<(), ()> {
39 // note that we use `StateMachineRef` here.
40 let mut machine = StateMachineRef::new(HelloState::Hello);
41
42 let mut context = 5;
43 machine.start(&mut context)?;
44 machine.update(&mut context)?;
45 machine.update(&mut context)?;
46
47 assert!(!machine.running());
48
49 Ok(())
50}
Sourcepub fn update(&mut self, args: &mut A) -> Result<(), E>
pub fn update(&mut self, args: &mut A) -> Result<(), E>
Updates the current state by calling State::update
with args
.
§Panics
Panics if the state machine is not running.
Examples found in repository?
38fn run() -> Result<(), ()> {
39 // note that we use `StateMachineRef` here.
40 let mut machine = StateMachineRef::new(HelloState::Hello);
41
42 let mut context = 5;
43 machine.start(&mut context)?;
44 machine.update(&mut context)?;
45 machine.update(&mut context)?;
46
47 assert!(!machine.running());
48
49 Ok(())
50}
Sourcepub fn fixed_update(&mut self, args: &mut A) -> Result<(), E>
pub fn fixed_update(&mut self, args: &mut A) -> Result<(), E>
Performs a fixed update on the current state
by calling State::fixed_update
with args
.
§Panics
Panics if the state machine is not running.
Sourcepub fn stop(&mut self, args: &mut A)
pub fn stop(&mut self, args: &mut A)
Stops the state machine.
This removes all states from the state machine and calls stop
on them.
It is highly recommended that you call this method
in case you want to stop the state machine, otherwise the states can’t
do anything on a shut down. If running()
is false already, don’t call this method.
§Examples
while machine.running() {
if shut_down_state_machine() {
// If you end the machine yourself you should call
machine.stop(());
break;
}
}
// If the loop exited because `machine.running()` was `false`,
// the machine is already stopped.
§Panics
Panics if the state machine is not running.
Source§impl<A, E, F, S> StateMachine<A, E, F, S>
impl<A, E, F, S> StateMachine<A, E, F, S>
Sourcepub fn start(&mut self, args: A) -> Result<(), E>
pub fn start(&mut self, args: A) -> Result<(), E>
Starts the state machine, calling .start
on the initial state.
§Panics
Panics if the state machine is running already.
Examples found in repository?
More examples
Sourcepub fn update(&mut self, args: A) -> Result<(), E>
pub fn update(&mut self, args: A) -> Result<(), E>
Updates the current state by calling State::update
with args
.
§Panics
Panics if the state machine is not running.
Examples found in repository?
More examples
Sourcepub fn fixed_update(&mut self, args: A) -> Result<(), E>
pub fn fixed_update(&mut self, args: A) -> Result<(), E>
Performs a fixed update on the current state
by calling State::fixed_update
with args
.
§Panics
Panics if the state machine is not running.
Sourcepub fn stop(&mut self, args: A)
pub fn stop(&mut self, args: A)
Stops the state machine.
This removes all states from the state machine and calls stop
on them.
It is highly recommended that you call this method
in case you want to stop the state machine, otherwise the states can’t
do anything on a shut down. If running()
is false already, don’t call this method.
§Examples
while machine.running() {
if shut_down_state_machine() {
// If you end the machine yourself you should call
machine.stop(());
break;
}
}
// If the loop exited because `machine.running()` was `false`,
// the machine is already stopped.
§Panics
Panics if the state machine is not running.