rstm_core/state/halt/
wrap.rs

1/*
2    Appellation: wrap <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::state::{Halt, State};
6
7/// [HaltState] extends the [State] by allowing for an 'imaginary' state that is not actually
8/// part of the machine's state space.
9#[derive(
10    Clone,
11    Copy,
12    Debug,
13    Eq,
14    Hash,
15    Ord,
16    PartialEq,
17    PartialOrd,
18    strum::EnumDiscriminants,
19    strum::EnumIs,
20)]
21#[cfg_attr(
22    feature = "serde",
23    derive(serde::Deserialize, serde::Serialize),
24    strum_discriminants(derive(serde::Deserialize, serde::Serialize))
25)]
26#[strum_discriminants(name(HaltTag), derive(Hash, Ord, PartialOrd))]
27pub enum HaltState<Q> {
28    Halt(Halt<Q>),
29    State(State<Q>),
30}
31
32impl<Q> HaltState<Q> {
33    /// Creates a new instance of a [HaltState] with a halted state.
34    pub fn halt(Halt(state): Halt<Q>) -> Self {
35        Self::Halt(Halt(state))
36    }
37    /// Creates a new instance of a [HaltState] with a continuing state.
38    pub fn state(state: State<Q>) -> Self {
39        Self::State(state)
40    }
41
42    pub fn into_state(self) -> State<Q> {
43        match self {
44            Self::State(state) => state,
45            Self::Halt(halt) => State(halt.0),
46        }
47    }
48
49    pub fn as_state(&self) -> State<&Q> {
50        State(self.get())
51    }
52
53    pub fn as_mut_state(&mut self) -> State<&mut Q> {
54        State(self.get_mut())
55    }
56
57    pub fn get(&self) -> &Q {
58        match self {
59            Self::State(inner) => inner.get_ref(),
60            Self::Halt(inner) => inner.get_ref(),
61        }
62    }
63
64    pub fn get_mut(&mut self) -> &mut Q {
65        match self {
66            Self::State(inner) => inner.get_mut(),
67            Self::Halt(inner) => inner.get_mut(),
68        }
69    }
70
71    pub fn set(&mut self, state: Q) {
72        match self {
73            Self::State(inner) => inner.set(state),
74            Self::Halt(inner) => inner.set(state),
75        }
76    }
77}
78
79impl<Q: Default> Default for HaltState<Q> {
80    fn default() -> Self {
81        Self::State(State::default())
82    }
83}
84
85impl<Q> From<State<Q>> for HaltState<Q> {
86    fn from(state: State<Q>) -> Self {
87        Self::State(state)
88    }
89}
90
91impl<Q> From<Halt<Q>> for HaltState<Q> {
92    fn from(halt: Halt<Q>) -> Self {
93        Self::Halt(halt)
94    }
95}