rstm_core/state/impls/
impl_repr.rs

1/*
2    Appellation: impl_repr <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::error::Error;
6use crate::state::{Halt, State};
7use core::mem::MaybeUninit;
8
9impl<'a, Q> State<&'a Q> {
10    /// Clones the internal state and returning a new instance of [State]
11    pub fn cloned(&self) -> State<Q>
12    where
13        Q: Clone,
14    {
15        State(self.0.clone())
16    }
17    /// Copies the internal state and returning a new instance of [State]
18    pub fn copied(&self) -> State<Q>
19    where
20        Q: Copy,
21    {
22        State(*self.0)
23    }
24}
25
26impl<'a, Q> State<&'a mut Q> {
27    /// Clones the internal state and returning a new instance of [State]
28    pub fn cloned(&self) -> State<Q>
29    where
30        Q: Clone,
31    {
32        State(self.0.clone())
33    }
34    /// Copies the internal state and returning a new instance of [State]
35    pub fn copied(&self) -> State<Q>
36    where
37        Q: Copy,
38    {
39        State(*self.0)
40    }
41}
42
43impl<Q> State<*const Q> {
44    /// Creates a new instance of state with a raw pointer to the inner value.
45    pub fn from_ptr(ptr: *const Q) -> Self {
46        Self(ptr)
47    }
48}
49
50impl<Q> State<*mut Q> {
51    /// Creates a new instance of state with a mutable raw pointer to the inner value.
52    pub fn from_mut_ptr(ptr: *mut Q) -> Self {
53        Self(ptr)
54    }
55}
56
57impl<Q> State<MaybeUninit<Q>> {
58    /// Creates a new instance of state with an initialized inner value.
59    pub fn init(value: Q) -> Self {
60        Self(MaybeUninit::new(value))
61    }
62    /// Creates a new instance of state with an uninitialized inner value.
63    pub const fn uninit() -> Self {
64        Self(MaybeUninit::uninit())
65    }
66    /// Converts the state into a new instance of [State] with an initialized state.
67    pub unsafe fn assume_init(self) -> State<Q> {
68        State(unsafe { self.into_inner().assume_init() })
69    }
70    /// determines if the inner state is null; returns false if the inner state is not null.
71    pub fn is_null(&self) -> bool {
72        self.get().as_ptr().is_null()
73    }
74    /// Writes a value to the inner state.
75    pub fn write(&mut self, value: Q) -> &mut Q {
76        self.get_mut().write(value)
77    }
78}
79
80impl State<()> {
81    /// Creates a new instance of [State] with an empty state.
82    pub fn empty() -> Self {
83        Self(())
84    }
85}
86
87impl State<bool> {
88    pub fn from_true() -> Self {
89        Self(true)
90    }
91
92    pub fn from_false() -> Self {
93        Self(false)
94    }
95
96    pub fn is_true(&self) -> bool {
97        self.into_inner()
98    }
99
100    pub fn is_false(&self) -> bool {
101        !self.into_inner()
102    }
103}
104
105impl State<Box<dyn core::any::Any>> {
106    /// Attempts to downcast the state to a concrete type `Q`; returns an error if the state
107    /// is not of type `Q`.
108    pub fn downcast<Q>(self) -> Result<State<Box<Q>>, Error>
109    where
110        Q: core::any::Any,
111    {
112        self.into_inner()
113            .downcast()
114            .map(State)
115            .map_err(|_| Error::TypeError("Failed to downcast state".to_string()))
116    }
117    /// Returns an immutable reference to the state if it is of type `Q`; returns `None`
118    /// otherwise.
119    pub fn downcast_ref<Q>(&self) -> Option<State<&Q>>
120    where
121        Q: core::any::Any,
122    {
123        self.get().downcast_ref().map(State)
124    }
125
126    /// Returns a mutable reference to the state if it is of type `Q`; returns `None`
127    /// otherwise.
128    pub fn downcast_mut<Q>(&mut self) -> Option<State<&mut Q>>
129    where
130        Q: core::any::Any,
131    {
132        self.get_mut().downcast_mut().map(State)
133    }
134}
135
136impl<Q> State<Option<Q>> {
137    /// Creates a new instance of state whose inner state is [Option::None].
138    pub fn none() -> Self {
139        Self(None)
140    }
141    /// Creates a new instance of state whose inner state is [Option::Some].
142    pub fn some(value: Q) -> Self {
143        Self(Some(value))
144    }
145}
146
147impl<Q> State<Halt<Q>> {
148    /// Creates a new instance of [State] from a [Halt] state.
149    pub fn halted(Halt(inner): Halt<Q>) -> Self {
150        Self(Halt(inner))
151    }
152    /// Converts the halted state into an unhalted state.
153    pub fn unhalt(self) -> State<Q> {
154        State(self.into_inner().get())
155    }
156}