Skip to main content

Fsm

Struct Fsm 

Source
pub struct Fsm<S, E, M>
where S: Hash + Eq + Clone + Debug, E: Hash + Eq + Clone + Debug, M: Clone + Debug,
{ /* private fields */ }
Expand description

哈希表驱动的有限状态机

§类型参数

  • S: 状态类型,需实现 Hash + Eq + Clone + Debug
  • E: 事件类型,需实现 Hash + Eq + Clone + Debug
  • M: 动作类型,需实现 Clone + Debug

§示例

use fsm_engine::Fsm;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum State { A, B }

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Event { Go }

#[derive(Debug, Clone)]
enum Motion { Print }

let fsm = Fsm::builder()
    .initial_state(State::A)
    .on(State::A, Event::Go, State::B, Motion::Print)
    .build()
    .unwrap();

Implementations§

Source§

impl<S, E, M> Fsm<S, E, M>
where S: Hash + Eq + Clone + Debug, E: Hash + Eq + Clone + Debug, M: Clone + Debug,

Source

pub fn builder() -> FsmBuilder<S, E, M>

获取 Builder,用于声明式地构建转移表

Source

pub fn step(&mut self, event: &E) -> Result<(&S, &M), FsmError<S, E>>

摄入一个事件,触发状态迁移并返回 (下一状态, 动作)

§Errors

若当前状态下没有为该事件注册的转移,返回 FsmError::NoTransition

Source

pub fn step_state(&mut self, event: &E) -> Result<&S, FsmError<S, E>>

只做状态迁移,忽略 motion(语法糖)

Source

pub fn current_state(&self) -> &S

查询当前状态

Source

pub fn initial_state(&self) -> &S

查询初始状态

Source

pub fn reset(&mut self)

重置到初始状态

Source

pub fn transition_count(&self) -> usize

转移表的大小(注册的转移弧数量)

Source

pub fn has_transition(&self, state: &S, event: &E) -> bool

检查某个转移是否存在

Source

pub fn transitions(&self) -> Vec<(&S, &E, &S, &M)>

遍历所有已注册的转移弧,返回 Vec<(cs, evt, ns, motion)>

Trait Implementations§

Source§

impl<S, E, M> Clone for Fsm<S, E, M>
where S: Hash + Eq + Clone + Debug + Clone, E: Hash + Eq + Clone + Debug + Clone, M: Clone + Debug + Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S, E, M> Debug for Fsm<S, E, M>
where S: Hash + Eq + Clone + Debug + Debug, E: Hash + Eq + Clone + Debug + Debug, M: Clone + Debug + Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S, E, M> Freeze for Fsm<S, E, M>
where HashMap<(S, E), Transition<S, M>>: Freeze, S: Freeze,

§

impl<S, E, M> RefUnwindSafe for Fsm<S, E, M>
where HashMap<(S, E), Transition<S, M>>: RefUnwindSafe, S: RefUnwindSafe,

§

impl<S, E, M> Send for Fsm<S, E, M>
where HashMap<(S, E), Transition<S, M>>: Send, S: Send,

§

impl<S, E, M> Sync for Fsm<S, E, M>
where HashMap<(S, E), Transition<S, M>>: Sync, S: Sync,

§

impl<S, E, M> Unpin for Fsm<S, E, M>
where HashMap<(S, E), Transition<S, M>>: Unpin, S: Unpin,

§

impl<S, E, M> UnsafeUnpin for Fsm<S, E, M>
where HashMap<(S, E), Transition<S, M>>: UnsafeUnpin, S: UnsafeUnpin,

§

impl<S, E, M> UnwindSafe for Fsm<S, E, M>
where HashMap<(S, E), Transition<S, M>>: UnwindSafe, 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.