pub struct StateMachine<S> { /* private fields */ }
Expand description

Wrapper to safely replace states of state machine which don’t consume their states. Use this wrapper if state transitions are performed on mutable references rather than consumed states. Example:

fn on_event(event: Event, statemachine: &mut StateMachine<Foo>) {
    statemachine.replace_state(|state| match state {
        State::A(_) => match event {
            Event::A => State::B,
            _ => state,
        }
        State::B => {
            warn!("cannot receive events in State::B");
            state
        }
    })
}

Implementations§

source§

impl<S> StateMachine<S>

source

pub fn new(state: S) -> Self

Constructs a new StateMachine.

source

pub fn replace_state<F>(&mut self, map: F) -> &mut Self
where F: FnOnce(S) -> S,

Replaces the current state with one lazily constructed by map.

source

pub fn try_replace_state<F, E>(&mut self, map: F) -> Result<&mut Self, E>
where F: FnOnce(S) -> Result<S, E>, S: Debug,

Replaces the current state with one lazily constructed by map.

source

pub fn replace_state_with(&mut self, new_state: S) -> &mut Self

Replaces the current state with new_state.

source

pub fn into_state(self) -> S

Consumes the state machine and returns its current state.

Trait Implementations§

source§

impl<S> AsMut<S> for StateMachine<S>

source§

fn as_mut(&mut self) -> &mut S

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<S> AsRef<S> for StateMachine<S>

source§

fn as_ref(&self) -> &S

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<S: Debug> Debug for StateMachine<S>

source§

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

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

impl<S> Deref for StateMachine<S>

§

type Target = S

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<S> DerefMut for StateMachine<S>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<S: PartialEq> PartialEq for StateMachine<S>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<S> Freeze for StateMachine<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for StateMachine<S>
where S: RefUnwindSafe,

§

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

§

impl<S> Sync for StateMachine<S>
where S: Sync,

§

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

§

impl<S> UnwindSafe for StateMachine<S>
where S: 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>,

§

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>,

§

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.