rstm_core/state/halt/
impl_halt.rs

1/*
2    Appellation: impl_halting <module>
3    Contrib: @FL03
4*/
5use super::Halt;
6use crate::state::{RawState, State};
7
8impl<Q> Halt<Q> {
9    pub fn new(halt: Q) -> Self {
10        Self(halt)
11    }
12    #[inline]
13    /// Consumes the halted state and returns the inner value.
14    pub fn get(self) -> Q {
15        self.0
16    }
17    /// Returns an immutable reference to the inner value of the halted state.
18    pub const fn get_ref(&self) -> &Q {
19        &self.0
20    }
21    /// Returns a mutable reference to the inner value of the halted state.
22    pub fn get_mut(&mut self) -> &mut Q {
23        &mut self.0
24    }
25    /// Replaces the inner value of the halted state with the given value, returning the
26    /// previous value.
27    pub fn replace(&mut self, halt: Q) -> Q {
28        core::mem::replace(&mut self.0, halt)
29    }
30    /// Resets the inner value of the halted state to the default value of the type.
31    pub fn reset(&mut self)
32    where
33        Q: Default,
34    {
35        self.set(Default::default());
36    }
37    /// Sets the inner value of the halted state to that of the given value.
38    pub fn set(&mut self, halt: Q) {
39        self.0 = halt;
40    }
41    /// Swaps the inner value of the halted state with that of the given state.
42    pub fn swap<S>(&mut self, other: &mut S)
43    where
44        S: RawState<Q = Q>,
45    {
46        core::mem::swap(&mut self.0, other.get_mut());
47    }
48    /// Takes the inner value of the halted state and replaces it with the default value of
49    /// the type.
50    pub fn take(&mut self) -> Q
51    where
52        Q: Default,
53    {
54        core::mem::take(&mut self.0)
55    }
56    /// Converts the halted state into a new [State] with an immutable reference to the inner
57    /// value.
58    pub fn as_state(&self) -> State<Halt<&Q>> {
59        State(Halt(&self.0))
60    }
61    /// Converts the halted state into a new [State] with a mutable reference to the inner
62    /// value.
63    pub fn as_state_mut(&mut self) -> State<Halt<&mut Q>> {
64        State(Halt(&mut self.0))
65    }
66    /// Wraps the halted state and returns a new [State]
67    pub fn into_state(self) -> State<Halt<Q>> {
68        State(self)
69    }
70    /// Returns an instance of [`Halt`] with an immutable reference to the inner value.
71    pub fn view(&self) -> Halt<&Q> {
72        Halt(&self.0)
73    }
74    /// Returns an instance of [`Halt`] with a mutable reference to the inner value.
75    pub fn view_mut(&mut self) -> Halt<&mut Q> {
76        Halt(&mut self.0)
77    }
78}
79
80impl<'a, Q> Halt<&'a Q> {
81    pub fn cloned(&self) -> Halt<Q>
82    where
83        Q: Clone,
84    {
85        Halt(self.0.clone())
86    }
87
88    pub fn copied(&self) -> Halt<Q>
89    where
90        Q: Copy,
91    {
92        Halt(*self.0)
93    }
94}
95
96impl<'a, Q> Halt<&'a mut Q> {
97    pub fn cloned(&self) -> Halt<Q>
98    where
99        Q: Clone,
100    {
101        Halt(self.0.clone())
102    }
103
104    pub fn copied(&self) -> Halt<Q>
105    where
106        Q: Copy,
107    {
108        Halt(*self.0)
109    }
110}
111
112impl<Q> From<State<Q>> for Halt<Q> {
113    fn from(State(state): State<Q>) -> Self {
114        Self(state)
115    }
116}
117
118impl<Q> From<Halt<Q>> for State<Q> {
119    fn from(Halt(state): Halt<Q>) -> Self {
120        Self(state)
121    }
122}