1use crate::{FsmBackend, lib::*};
2
3use crate::FsmResult;
4
5pub trait FsmStates<TFsm>: FsmStateFactory<TFsm> where TFsm: FsmBackend {
7 type StateKind: Clone + Copy + Debug + PartialEq + 'static;
9 type CurrentState: Clone + Copy + Debug + Default + AsRef<[FsmCurrentState<Self::StateKind>]> + AsMut<[FsmCurrentState<Self::StateKind>]> + 'static;
11}
12
13#[derive(Copy, Clone, PartialEq)]
15pub enum FsmCurrentState<S> where S: Clone + Copy {
16 Stopped,
18 State(S)
20}
21
22impl<S> FsmCurrentState<S> where S: Clone + Copy {
23 pub fn all_stopped(current_states: &[Self]) -> bool {
24 current_states.iter().all(|s| match s {
25 FsmCurrentState::Stopped => true,
26 _ => false
27 })
28 }
29}
30
31impl<S> Debug for FsmCurrentState<S> where S: Debug + Copy {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 match self {
34 FsmCurrentState::Stopped => f.write_str("Fsm::Stopped"),
35 FsmCurrentState::State(s) => s.fmt(f)
36 }
37 }
38}
39
40impl<S> Default for FsmCurrentState<S> where S: Clone + Copy {
41 fn default() -> Self {
42 Self::Stopped
43 }
44}
45
46pub trait FsmStateFactory<TFsm> where Self: Sized, TFsm: FsmBackend {
48 fn new_state(context: &<TFsm as FsmBackend>::Context) -> FsmResult<Self>;
50}
51
52impl<TState, TFsm> FsmStateFactory<TFsm> for TState where TState: Default, TFsm: FsmBackend {
54 fn new_state(_context: &<TFsm as FsmBackend>::Context) -> FsmResult<Self> {
55 Ok(Default::default())
56 }
57}
58
59pub trait FsmStateTransitionAsRef<T1, T2> {
61 fn as_state_transition_ref(&self) -> (&T1, &T2);
62}
63
64pub trait FsmStateTransitionAsMut<T1, T2> {
66 fn as_state_transition_mut(&mut self) -> (&mut T1, &mut T2);
67}