Struct StateMachine

Source
pub struct StateMachine<A, E, F, S> { /* private fields */ }
Expand description

A simple, generic state machine. The argument can be

  1. an owned value which implements Clone
  2. an immutable reference
  3. 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>

Source

pub fn new(initial: S) -> Self

Creates a new state machine with a give initial state.

Examples found in repository?
examples/boxed.rs (line 35)
34fn run() -> Result<(), ()> {
35    let mut machine = DynMachine::new(Box::new(State1));
36
37    machine.start(1)?;
38    machine.update(2)?;
39
40    assert!(!machine.running());
41
42    Ok(())
43}
More examples
Hide additional examples
examples/ref.rs (line 37)
36fn run() -> Result<(), ()> {
37    let mut machine = StateMachine::new(HelloState::Hello);
38
39    machine.start("you!")?;
40    machine.update("Whatever")?;
41    machine.update("Irrelevant")?;
42
43    assert!(!machine.running());
44
45    Ok(())
46}
examples/basic.rs (line 37)
36fn run() -> Result<(), ()> {
37    let mut machine = StateMachine::new(HelloState::Hello);
38
39    machine.start("you!".to_owned())?;
40    machine.update("Whatever".to_owned())?;
41    machine.update("Irrelevant".to_owned())?;
42
43    assert!(!machine.running());
44
45    Ok(())
46}
examples/ref_mut.rs (line 40)
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

pub fn running(&self) -> bool

Checks if the state machine is running.

Examples found in repository?
examples/boxed.rs (line 40)
34fn run() -> Result<(), ()> {
35    let mut machine = DynMachine::new(Box::new(State1));
36
37    machine.start(1)?;
38    machine.update(2)?;
39
40    assert!(!machine.running());
41
42    Ok(())
43}
More examples
Hide additional examples
examples/ref.rs (line 43)
36fn run() -> Result<(), ()> {
37    let mut machine = StateMachine::new(HelloState::Hello);
38
39    machine.start("you!")?;
40    machine.update("Whatever")?;
41    machine.update("Irrelevant")?;
42
43    assert!(!machine.running());
44
45    Ok(())
46}
examples/basic.rs (line 43)
36fn run() -> Result<(), ()> {
37    let mut machine = StateMachine::new(HelloState::Hello);
38
39    machine.start("you!".to_owned())?;
40    machine.update("Whatever".to_owned())?;
41    machine.update("Irrelevant".to_owned())?;
42
43    assert!(!machine.running());
44
45    Ok(())
46}
examples/ref_mut.rs (line 47)
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>
where A: ?Sized, S: for<'a> State<&'a mut A, E, F>,

Source

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?
examples/ref_mut.rs (line 43)
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

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?
examples/ref_mut.rs (line 44)
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

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.

Source

pub fn event(&mut self, args: &mut A, event: F) -> Result<(), E>

Sends an event to the current state.

§Panics

Panics if the state machine is not running.

Source

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>
where A: Clone, S: State<A, E, F>,

Source

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?
examples/boxed.rs (line 37)
34fn run() -> Result<(), ()> {
35    let mut machine = DynMachine::new(Box::new(State1));
36
37    machine.start(1)?;
38    machine.update(2)?;
39
40    assert!(!machine.running());
41
42    Ok(())
43}
More examples
Hide additional examples
examples/ref.rs (line 39)
36fn run() -> Result<(), ()> {
37    let mut machine = StateMachine::new(HelloState::Hello);
38
39    machine.start("you!")?;
40    machine.update("Whatever")?;
41    machine.update("Irrelevant")?;
42
43    assert!(!machine.running());
44
45    Ok(())
46}
examples/basic.rs (line 39)
36fn run() -> Result<(), ()> {
37    let mut machine = StateMachine::new(HelloState::Hello);
38
39    machine.start("you!".to_owned())?;
40    machine.update("Whatever".to_owned())?;
41    machine.update("Irrelevant".to_owned())?;
42
43    assert!(!machine.running());
44
45    Ok(())
46}
Source

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?
examples/boxed.rs (line 38)
34fn run() -> Result<(), ()> {
35    let mut machine = DynMachine::new(Box::new(State1));
36
37    machine.start(1)?;
38    machine.update(2)?;
39
40    assert!(!machine.running());
41
42    Ok(())
43}
More examples
Hide additional examples
examples/ref.rs (line 40)
36fn run() -> Result<(), ()> {
37    let mut machine = StateMachine::new(HelloState::Hello);
38
39    machine.start("you!")?;
40    machine.update("Whatever")?;
41    machine.update("Irrelevant")?;
42
43    assert!(!machine.running());
44
45    Ok(())
46}
examples/basic.rs (line 40)
36fn run() -> Result<(), ()> {
37    let mut machine = StateMachine::new(HelloState::Hello);
38
39    machine.start("you!".to_owned())?;
40    machine.update("Whatever".to_owned())?;
41    machine.update("Irrelevant".to_owned())?;
42
43    assert!(!machine.running());
44
45    Ok(())
46}
Source

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.

Source

pub fn event(&mut self, args: A, event: F) -> Result<(), E>

Sends an event to the current state.

§Panics

Panics if the state machine is not running.

Source

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.

Trait Implementations§

Source§

impl<A, E, F, S> Default for StateMachine<A, E, F, S>
where S: Default,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<A, E, F, S> Freeze for StateMachine<A, E, F, S>

§

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

§

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

§

impl<A, E, F, S> Sync for StateMachine<A, E, F, S>
where A: Sync, E: Sync, F: Sync, S: Sync,

§

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

§

impl<A, E, F, S> UnwindSafe for StateMachine<A, E, F, S>

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.