rstm_core/state/impls/
impl_state.rs1use crate::state::{Halt, RawState, State};
6
7impl<Q> State<Q> {
8 pub fn from_value(state: Q) -> Self {
9 Self(state)
10 }
11 pub fn as_ptr(&self) -> *const Q {
13 core::ptr::addr_of!(self.0)
14 }
15 pub fn as_mut_ptr(&mut self) -> *mut Q {
17 core::ptr::addr_of_mut!(self.0)
18 }
19 pub unsafe fn cast<R>(self) -> State<R> {
25 unsafe { State(core::ptr::read(&self.0 as *const Q as *const R)) }
26 }
27 pub const fn get(&self) -> &Q {
29 &self.0
30 }
31 pub fn get_mut(&mut self) -> &mut Q {
33 self.as_mut()
34 }
35 #[inline]
36 pub fn into_inner(self) -> Q {
38 self.0
39 }
40 pub fn map<R, F>(self, f: F) -> State<R>
43 where
44 F: FnOnce(Q) -> R,
45 {
46 State(f(self.into_inner()))
47 }
48 pub fn map_mut<R, F>(&mut self, mut f: F) -> State<R>
50 where
51 F: FnMut(&mut Q) -> R,
52 {
53 State(f(self.get_mut()))
54 }
55 pub fn replace(&mut self, state: Q) -> Q {
57 core::mem::replace(&mut self.0, state)
58 }
59 pub fn reset(&mut self)
61 where
62 Q: Default,
63 {
64 self.set(Default::default());
65 }
66 pub fn set(&mut self, state: Q) {
68 self.0 = state;
69 }
70 pub fn swap<S>(&mut self, other: &mut S)
72 where
73 S: RawState<Q = Q>,
74 {
75 core::mem::swap(&mut self.0, other.get_mut());
76 }
77 pub fn take(&mut self) -> Q
80 where
81 Q: Default,
82 {
83 core::mem::take(&mut self.0)
84 }
85 pub fn as_halt(&self) -> State<Halt<&Q>> {
87 State(Halt(self))
88 }
89 pub fn into_halt(self) -> State<Halt<Q>> {
91 State(Halt(self.into_inner()))
92 }
93 pub fn halt(self) -> State<Halt<Q>> {
95 State(Halt(self.0))
96 }
97 pub fn is_halt(&self) -> bool
99 where
100 Q: 'static,
101 {
102 core::any::TypeId::of::<Self>() == core::any::TypeId::of::<State<Halt<Q>>>()
103 }
104 pub fn boxed(self) -> State<Box<Q>> {
106 self.map(Box::new)
107 }
108 pub fn as_any(&self) -> State<Box<dyn std::any::Any>>
110 where
111 Q: Clone + 'static,
112 {
113 State(Box::new(self.get().clone()))
114 }
115 pub fn into_any(self) -> State<Box<dyn std::any::Any>>
117 where
118 Q: 'static,
119 {
120 State(Box::new(self.into_inner()))
121 }
122 #[cfg(feature = "std")]
123 pub fn shared(self) -> State<std::sync::Arc<Q>> {
125 self.map(std::sync::Arc::new)
126 }
127 #[cfg(feature = "std")]
128 pub fn to_shared(&self) -> State<std::sync::Arc<Q>>
130 where
131 Q: Clone,
132 {
133 self.clone().shared()
134 }
135 pub fn to_ref(&self) -> State<&Q> {
137 State(self.get())
138 }
139 pub fn to_mut(&mut self) -> State<&mut Q> {
141 State(self.get_mut())
142 }
143 pub fn get_inner_type_name(&self) -> &'static str {
145 core::any::type_name::<Q>()
146 }
147 pub fn get_inner_type_id(&self) -> core::any::TypeId
149 where
150 Q: 'static,
151 {
152 core::any::TypeId::of::<Q>()
153 }
154}
155
156impl<Q> core::convert::AsRef<Q> for State<Q> {
160 fn as_ref(&self) -> &Q {
161 &self.0
162 }
163}
164
165impl<Q> core::convert::AsMut<Q> for State<Q> {
166 fn as_mut(&mut self) -> &mut Q {
167 &mut self.0
168 }
169}
170
171impl<Q> core::borrow::Borrow<Q> for State<Q> {
172 fn borrow(&self) -> &Q {
173 &self.0
174 }
175}
176
177impl<Q> core::borrow::BorrowMut<Q> for State<Q> {
178 fn borrow_mut(&mut self) -> &mut Q {
179 &mut self.0
180 }
181}
182
183impl<Q> core::ops::Deref for State<Q> {
184 type Target = Q;
185
186 fn deref(&self) -> &Self::Target {
187 &self.0
188 }
189}
190
191impl<Q> core::ops::DerefMut for State<Q> {
192 fn deref_mut(&mut self) -> &mut Self::Target {
193 &mut self.0
194 }
195}
196
197impl<Q> core::cmp::PartialEq<Q> for State<Q>
201where
202 Q: core::cmp::PartialEq,
203{
204 fn eq(&self, other: &Q) -> bool {
205 self.get().eq(other)
206 }
207}
208
209impl<Q> core::cmp::PartialOrd<Q> for State<Q>
210where
211 Q: core::cmp::PartialOrd<Q>,
212{
213 fn partial_cmp(&self, other: &Q) -> Option<core::cmp::Ordering> {
214 self.get().partial_cmp(other)
215 }
216}
217
218impl<Q> From<Q> for State<Q> {
222 fn from(state: Q) -> Self {
223 State(state)
224 }
225}
226
227unsafe impl<Q> core::marker::Send for State<Q> where Q: core::marker::Send {}
231
232unsafe impl<Q> core::marker::Sync for State<Q> where Q: core::marker::Sync {}
233
234impl_fmt!(State(
235 Binary, Display, LowerExp, LowerHex, Octal, UpperExp, UpperHex
236));