rstm_core/state/impls/
impl_state.rs

1/*
2    Appellation: impl_state <module>
3    Contrib: @FL03
4*/
5use crate::state::{Halt, RawState, State};
6
7impl<Q> State<Q> {
8    pub fn from_value(state: Q) -> Self {
9        Self(state)
10    }
11    /// Returns a new instance of state with a raw pointer to the inner value.
12    pub fn as_ptr(&self) -> *const Q {
13        core::ptr::addr_of!(self.0)
14    }
15    /// Returns a new instance of state with a mutable raw pointer to the inner value.
16    pub fn as_mut_ptr(&mut self) -> *mut Q {
17        core::ptr::addr_of_mut!(self.0)
18    }
19    /// Casts the state to a new type, returning a new instance of [State].
20    ///
21    /// # Saftey
22    ///
23    /// This method is unsafe because it is up to the caller to ensure that the cast is valid.
24    pub unsafe fn cast<R>(self) -> State<R> {
25        unsafe { State(core::ptr::read(&self.0 as *const Q as *const R)) }
26    }
27    /// Returns an immutable reference to the inner value of the state.
28    pub const fn get(&self) -> &Q {
29        &self.0
30    }
31    /// Returns a mutable reference to the inner value of the state.
32    pub fn get_mut(&mut self) -> &mut Q {
33        self.as_mut()
34    }
35    #[inline]
36    /// Consumes and returns the inner value of the state.
37    pub fn into_inner(self) -> Q {
38        self.0
39    }
40    /// [State::map] applies the given function onto the inner value of the state, returning a
41    /// new state with the result.
42    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    ///
49    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    /// Replaces the state with a new value, returning the old value.
56    pub fn replace(&mut self, state: Q) -> Q {
57        core::mem::replace(&mut self.0, state)
58    }
59    /// Clears the state, setting it to its default value.
60    pub fn reset(&mut self)
61    where
62        Q: Default,
63    {
64        self.set(Default::default());
65    }
66    /// Sets the state to a new value.
67    pub fn set(&mut self, state: Q) {
68        self.0 = state;
69    }
70    /// Swaps the inner value of the state with that of the given state.
71    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    /// Takes the inner value of the state, replacing it with the default value and returning
78    /// the previous value.
79    pub fn take(&mut self) -> Q
80    where
81        Q: Default,
82    {
83        core::mem::take(&mut self.0)
84    }
85    /// Returns a halted state with an immutable reference to the state.
86    pub fn as_halt(&self) -> State<Halt<&Q>> {
87        State(Halt(self))
88    }
89    /// Consumes the state and returns a halted state.
90    pub fn into_halt(self) -> State<Halt<Q>> {
91        State(Halt(self.into_inner()))
92    }
93    /// Wraps the inner value with a [Halt] state, returning a new instance of [State].
94    pub fn halt(self) -> State<Halt<Q>> {
95        State(Halt(self.0))
96    }
97    /// Returns `true` if the state is a [Halt] state.
98    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    /// Returns a new state with a boxed inner value.
105    pub fn boxed(self) -> State<Box<Q>> {
106        self.map(Box::new)
107    }
108    /// Converts the inner type into a boxed "any" state, returning a new instance of state
109    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    /// Converts the inner type into a boxed "any" state, returning a new instance of state
116    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    /// Wraps the inner value of the state with an [`Arc`] and returns a new instance of [State]
124    pub fn shared(self) -> State<std::sync::Arc<Q>> {
125        self.map(std::sync::Arc::new)
126    }
127    #[cfg(feature = "std")]
128    /// Returns a shared reference to the state.
129    pub fn to_shared(&self) -> State<std::sync::Arc<Q>>
130    where
131        Q: Clone,
132    {
133        self.clone().shared()
134    }
135    /// Returns a state with an owned inner value.
136    pub fn to_ref(&self) -> State<&Q> {
137        State(self.get())
138    }
139    /// Returns a state with a mutable reference to the inner value.
140    pub fn to_mut(&mut self) -> State<&mut Q> {
141        State(self.get_mut())
142    }
143    /// Returns the `name` of the generic inner type, `Q`.
144    pub fn get_inner_type_name(&self) -> &'static str {
145        core::any::type_name::<Q>()
146    }
147    /// Returns the `type id` of the generic inner type, `Q`.
148    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
156/*
157 ************* References *************
158*/
159impl<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
197/*
198 ************* Comparisons *************
199*/
200impl<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
218/*
219 ************* Conversions *************
220*/
221impl<Q> From<Q> for State<Q> {
222    fn from(state: Q) -> Self {
223        State(state)
224    }
225}
226
227/*
228 ************* Markers *************
229*/
230unsafe 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));