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>
impl<S> StateMachine<S>
Sourcepub fn replace_state<F>(&mut self, map: F) -> &mut Selfwhere
F: FnOnce(S) -> S,
pub fn replace_state<F>(&mut self, map: F) -> &mut Selfwhere
F: FnOnce(S) -> S,
Replaces the current state with one lazily constructed by map
.
Sourcepub fn try_replace_state<F, E>(&mut self, map: F) -> Result<&mut Self, E>
pub fn try_replace_state<F, E>(&mut self, map: F) -> Result<&mut Self, E>
Replaces the current state with one lazily constructed by map
.
Sourcepub fn replace_state_with(&mut self, new_state: S) -> &mut Self
pub fn replace_state_with(&mut self, new_state: S) -> &mut Self
Replaces the current state with new_state
.
Sourcepub fn into_state(self) -> S
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>
impl<S> AsMut<S> for StateMachine<S>
Source§impl<S> AsRef<S> for StateMachine<S>
impl<S> AsRef<S> for StateMachine<S>
Source§impl<S: Debug> Debug for StateMachine<S>
impl<S: Debug> Debug for StateMachine<S>
Source§impl<S> Deref for StateMachine<S>
impl<S> Deref for StateMachine<S>
Source§impl<S> DerefMut for StateMachine<S>
impl<S> DerefMut for StateMachine<S>
Source§impl<S: PartialEq> PartialEq for StateMachine<S>
impl<S: PartialEq> PartialEq for StateMachine<S>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more