rstm_state/impls/
impl_halt.rs1use 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 pub const fn from_halt(state: H) -> Self {
22 Self::Halt(state)
23 }
24 pub const fn from_state(state: Q) -> Self {
26 Self::Step(state)
27 }
28 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 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 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 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 pub fn into_inner(self) -> Q {
71 match self {
72 Self::Step(inner) => inner,
73 Self::Halt(inner) => inner,
74 }
75 }
76 #[inline]
77 pub fn halt(self) -> Halt<Q, Q> {
79 match self {
80 Self::Step(inner) => Halt::Halt(inner),
81 _ => self,
82 }
83 }
84 #[inline]
85 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 pub fn into_halt_state(self) -> State<Halt<Q>> {
95 State(self)
96 }
97 pub const fn as_halt_state(&self) -> State<Halt<&Q>> {
99 State(self.view())
100 }
101 pub const fn as_mut_halt_state(&mut self) -> State<Halt<&mut Q>> {
104 State(self.view_mut())
105 }
106 pub const fn as_state(&self) -> State<&Q> {
108 State(self.get())
109 }
110 pub const fn as_mut_state(&mut self) -> State<&mut Q> {
112 State(self.get_mut())
113 }
114 pub const fn get(&self) -> &Q {
116 match self {
117 Self::Step(inner) => inner,
118 Self::Halt(inner) => inner,
119 }
120 }
121 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 pub const fn replace(&mut self, state: Q) -> Q {
130 core::mem::replace(self.get_mut(), state)
131 }
132 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 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 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 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 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}