Skip to main content

rstm_state/impls/
impl_halt.rs

1/*
2    Appellation: wrap <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::{Halt, RawState, State};
6
7impl<Q, H> RawState for Halt<Q, H>
8where
9    Q: RawState,
10    H: RawState,
11{
12    seal! {}
13}
14
15impl<Q, H> Halt<Q, H>
16where
17    Q: RawState,
18    H: RawState,
19{
20    /// Creates a new instance of a [Halt] with a halted state.
21    pub const fn from_halt(state: H) -> Self {
22        Self::Halt(state)
23    }
24    /// Creates a new instance of a [Halt] with a continuing state.
25    pub const fn from_state(state: Q) -> Self {
26        Self::Step(state)
27    }
28    /// [`swap`](core::mem::swap) the inner value of the halt state with that of the given state.
29    pub const fn swap(&mut self, other: &mut Halt<Q, H>) {
30        match (self, other) {
31            (Self::Step(a), Self::Step(b)) => core::mem::swap(a, b),
32            (Self::Halt(a), Self::Halt(b)) => core::mem::swap(a, b),
33            _ => {}
34        }
35    }
36    /// returns a new instance of the halt state containing a reference to its inner value.
37    pub const fn view(&self) -> Halt<&Q, &H> {
38        match self {
39            Self::Step(inner) => Halt::Step(inner),
40            Self::Halt(inner) => Halt::Halt(inner),
41        }
42    }
43    /// returns a new instance of the halt state containing a mutable reference to its inner
44    /// value.
45    pub const fn view_mut(&mut self) -> Halt<&mut Q, &mut H> {
46        match self {
47            Self::Step(inner) => Halt::Step(inner),
48            Self::Halt(inner) => Halt::Halt(inner),
49        }
50    }
51    /// returns an owned version of the current haltable state
52    pub fn to_owned(&self) -> Halt<Q, H>
53    where
54        Q: Clone,
55        H: Clone,
56    {
57        match self {
58            Self::Step(inner) => Halt::Step(inner.clone()),
59            Self::Halt(inner) => Halt::Halt(inner.clone()),
60        }
61    }
62}
63
64impl<Q> Halt<Q, Q>
65where
66    Q: RawState,
67{
68    #[inline]
69    /// consumes the current haltable state, returning the inner state.
70    pub fn into_inner(self) -> Q {
71        match self {
72            Self::Step(inner) => inner,
73            Self::Halt(inner) => inner,
74        }
75    }
76    #[inline]
77    /// consumes the current instance to ensure a halting state
78    pub fn halt(self) -> Halt<Q, Q> {
79        match self {
80            Self::Step(inner) => Halt::Halt(inner),
81            _ => self,
82        }
83    }
84    #[inline]
85    /// consumes the current haltable state, returning the inner state.
86    pub fn into_state(self) -> State<Q> {
87        match self {
88            Self::Step(inner) => State(inner),
89            Self::Halt(inner) => State(inner),
90        }
91    }
92    #[inline]
93    /// consumes the current instance to initialize a wrapper instance
94    pub fn into_halt_state(self) -> State<Halt<Q>> {
95        State(self)
96    }
97    /// returns a wrapped instance of the halt state containing a reference to its inner value.
98    pub const fn as_halt_state(&self) -> State<Halt<&Q>> {
99        State(self.view())
100    }
101    /// returns a wrapped instance of the halt state containing a mutable reference to its
102    /// inner value.
103    pub const fn as_mut_halt_state(&mut self) -> State<Halt<&mut Q>> {
104        State(self.view_mut())
105    }
106    /// returns a wrapped reference to the inner value
107    pub const fn as_state(&self) -> State<&Q> {
108        State(self.get())
109    }
110    /// returns a wrapped mutable reference to the inner value
111    pub const fn as_mut_state(&mut self) -> State<&mut Q> {
112        State(self.get_mut())
113    }
114    /// returns a reference to the internal state
115    pub const fn get(&self) -> &Q {
116        match self {
117            Self::Step(inner) => inner,
118            Self::Halt(inner) => inner,
119        }
120    }
121    /// returns a mutable reference to the internal state
122    pub const fn get_mut(&mut self) -> &mut Q {
123        match self {
124            Self::Step(inner) => inner,
125            Self::Halt(inner) => inner,
126        }
127    }
128    /// [`replace`](core::mem::replace) the inner value of the halt state with the given state.
129    pub const fn replace(&mut self, state: Q) -> Q {
130        core::mem::replace(self.get_mut(), state)
131    }
132    /// update the current inner state without affecting the _status_ of the state.
133    pub fn set(&mut self, state: Q) {
134        *self.get_mut() = state;
135    }
136}
137
138impl<'a, Q, H> Halt<&'a Q, &'a H>
139where
140    Q: RawState,
141    H: RawState,
142{
143    #[inline]
144    /// consumes the current instance of the halt state to create another with cloned inner
145    /// values.
146    pub fn cloned(self) -> Halt<Q, H>
147    where
148        Q: Clone,
149        H: Clone,
150    {
151        match self {
152            Self::Step(inner) => Halt::Step(inner.clone()),
153            Self::Halt(inner) => Halt::Halt(inner.clone()),
154        }
155    }
156    #[inline]
157    /// consumes the current instance of the halt state to create another with copied inner
158    /// values.
159    ///
160    /// **Note**: This method does not mutate the specified _variant_.
161    pub fn copied(self) -> Halt<Q, H>
162    where
163        Q: Copy,
164        H: Copy,
165    {
166        match self {
167            Self::Step(&inner) => Halt::Step(inner),
168            Self::Halt(&inner) => Halt::Halt(inner),
169        }
170    }
171}
172
173impl<'a, Q, H> Halt<&'a mut Q, &'a mut H>
174where
175    Q: RawState,
176    H: RawState,
177{
178    #[inline]
179    /// consumes the current instance of the halt state to create another with cloned inner
180    /// values.
181    pub fn cloned(self) -> Halt<Q, H>
182    where
183        Q: Clone,
184        H: Clone,
185    {
186        match self {
187            Self::Step(inner) => Halt::Step(inner.clone()),
188            Self::Halt(inner) => Halt::Halt(inner.clone()),
189        }
190    }
191    #[inline]
192    /// consumes the current instance of the halt state to create another with copied inner
193    /// values.
194    pub fn copied(self) -> Halt<Q, H>
195    where
196        Q: Copy,
197        H: Copy,
198    {
199        match self {
200            Self::Step(&mut inner) => Halt::Step(inner),
201            Self::Halt(&mut inner) => Halt::Halt(inner),
202        }
203    }
204}
205
206impl<Q> AsRef<Q> for Halt<Q>
207where
208    Q: RawState,
209{
210    fn as_ref(&self) -> &Q {
211        self.get()
212    }
213}
214
215impl<Q> AsMut<Q> for Halt<Q>
216where
217    Q: RawState,
218{
219    fn as_mut(&mut self) -> &mut Q {
220        self.get_mut()
221    }
222}
223
224impl<Q> core::ops::Deref for Halt<Q>
225where
226    Q: RawState,
227{
228    type Target = Q;
229
230    fn deref(&self) -> &Self::Target {
231        self.get()
232    }
233}
234
235impl<Q> core::ops::DerefMut for Halt<Q>
236where
237    Q: RawState,
238{
239    fn deref_mut(&mut self) -> &mut Self::Target {
240        self.get_mut()
241    }
242}
243
244impl<Q> Default for Halt<Q>
245where
246    Q: Default + RawState,
247{
248    fn default() -> Self {
249        Self::Step(Default::default())
250    }
251}
252
253impl<Q> From<State<Q>> for Halt<Q>
254where
255    Q: RawState,
256{
257    fn from(State(state): State<Q>) -> Self {
258        Self::Step(state)
259    }
260}