eryon_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) => {
50                let _ = inner.set(state);
51            }
52            Self::Halt(inner) => {
53                inner.set(state);
54            }
55        }
56    }
57}
58
59impl<Q> Default for HaltState<Q>
60where
61    Q: Default,
62{
63    fn default() -> Self {
64        Self::State(State::default())
65    }
66}
67
68impl<Q> From<State<Q>> for HaltState<Q> {
69    fn from(state: State<Q>) -> Self {
70        Self::State(state)
71    }
72}
73
74impl<Q> From<Halt<Q>> for HaltState<Q> {
75    fn from(halt: Halt<Q>) -> Self {
76        Self::Halt(halt)
77    }
78}