rstm_core/state/halt/
impl_enum.rs

1/*
2    Appellation: wrap <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use super::{Halt, HaltState};
6use crate::state::State;
7
8impl<Q> HaltState<Q> {
9    /// Creates a new instance of a [HaltState] with a halted state.
10    pub fn halt(Halt(state): Halt<Q>) -> Self {
11        Self::Halt(Halt(state))
12    }
13    /// Creates a new instance of a [HaltState] with a continuing state.
14    pub fn state(state: State<Q>) -> Self {
15        Self::State(state)
16    }
17
18    pub fn into_state(self) -> State<Q> {
19        match self {
20            Self::State(state) => state,
21            Self::Halt(halt) => State(halt.0),
22        }
23    }
24
25    pub fn as_state(&self) -> State<&Q> {
26        State(self.get())
27    }
28
29    pub fn as_mut_state(&mut self) -> State<&mut Q> {
30        State(self.get_mut())
31    }
32
33    pub fn get(&self) -> &Q {
34        match self {
35            Self::State(inner) => inner.get(),
36            Self::Halt(inner) => inner.get_ref(),
37        }
38    }
39
40    pub fn get_mut(&mut self) -> &mut Q {
41        match self {
42            Self::State(inner) => inner.get_mut(),
43            Self::Halt(inner) => inner.get_mut(),
44        }
45    }
46
47    pub fn set(&mut self, state: Q) {
48        match self {
49            Self::State(inner) => inner.set(state),
50            Self::Halt(inner) => inner.set(state),
51        }
52    }
53}
54
55impl<Q: Default> Default for HaltState<Q> {
56    fn default() -> Self {
57        Self::State(State::default())
58    }
59}
60
61impl<Q> From<State<Q>> for HaltState<Q> {
62    fn from(state: State<Q>) -> Self {
63        Self::State(state)
64    }
65}
66
67impl<Q> From<Halt<Q>> for HaltState<Q> {
68    fn from(halt: Halt<Q>) -> Self {
69        Self::Halt(halt)
70    }
71}