eryon_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, RawState, State};
7#[cfg(feature = "alloc")]
8use alloc::boxed::Box;
9use core::mem::MaybeUninit;
10
11impl<Q> State<&Q>
12where
13    Q: RawState,
14{
15    /// Clones the internal state and returning a new instance of [State]
16    pub fn cloned(&self) -> State<Q>
17    where
18        Q: Clone,
19    {
20        State(self.0.clone())
21    }
22    /// Copies the internal state and returning a new instance of [State]
23    pub fn copied(&self) -> State<Q>
24    where
25        Q: Copy,
26    {
27        State(*self.0)
28    }
29}
30
31impl<Q> State<&mut Q>
32where
33    Q: RawState,
34{
35    /// Clones the internal state and returning a new instance of [State]
36    pub fn cloned(&self) -> State<Q>
37    where
38        Q: Clone,
39    {
40        State(self.0.clone())
41    }
42    /// Copies the internal state and returning a new instance of [State]
43    pub fn copied(&self) -> State<Q>
44    where
45        Q: Copy,
46    {
47        State(*self.0)
48    }
49}
50
51impl<Q> State<*const Q>
52where
53    Q: RawState,
54{
55    /// Creates a new instance of state with a raw pointer to the inner value.
56    pub fn from_ptr(ptr: *const Q) -> Self {
57        Self(ptr)
58    }
59}
60
61impl<Q> State<*mut Q>
62where
63    Q: RawState,
64{
65    /// Creates a new instance of state with a mutable raw pointer to the inner value.
66    pub fn from_mut_ptr(ptr: *mut Q) -> Self {
67        Self(ptr)
68    }
69}
70
71impl<Q> State<MaybeUninit<Q>>
72where
73    Q: RawState,
74{
75    /// Creates a new instance of state with an initialized inner value.
76    pub fn init(value: Q) -> Self {
77        Self(MaybeUninit::new(value))
78    }
79    /// Creates a new instance of state with an uninitialized inner value.
80    pub const fn uninit() -> Self {
81        Self(MaybeUninit::uninit())
82    }
83    #[allow(clippy::missing_safety_doc)]
84    /// Converts the state into a new instance of [State] with an initialized state.
85    ///
86    /// # Safety
87    ///
88    /// This method is unsafe because it is up to the caller to ensure that the inner value
89    /// is indeed initialized.
90    pub unsafe fn assume_init(self) -> State<Q> {
91        State(unsafe { self.value().assume_init() })
92    }
93    /// determines if the inner state is null; returns false if the inner state is not null.
94    pub fn is_null(&self) -> bool {
95        self.get().as_ptr().is_null()
96    }
97    /// Writes a value to the inner state.
98    pub fn write(&mut self, value: Q) -> &mut Q {
99        self.get_mut().write(value)
100    }
101}
102
103impl State<()> {
104    /// Creates a new instance of [State] with an empty state.
105    pub const fn empty() -> Self {
106        Self(())
107    }
108}
109
110impl State<bool> {
111    /// Creates a new instance of [State] with an inner state of `true`.
112    pub const fn from_true() -> Self {
113        Self(true)
114    }
115    /// returns a new instance of [`State`] with an inner state of `false`.
116    pub const fn from_false() -> Self {
117        Self(false)
118    }
119    /// returns true if the inner state is true, false otherwise.
120    pub fn is_true(&self) -> bool {
121        self.value()
122    }
123    /// returns true if the inner state is false, false otherwise.
124    pub fn is_false(&self) -> bool {
125        !self.value()
126    }
127}
128#[cfg(feature = "alloc")]
129impl State<Box<dyn core::any::Any>> {
130    /// Attempts to downcast the state to a concrete type `Q`; returns an error if the state
131    /// is not of type `Q`.
132    pub fn downcast<Q>(self) -> Result<State<Box<Q>>, Error>
133    where
134        Q: core::any::Any,
135    {
136        self.value()
137            .downcast()
138            .map(State)
139            .map_err(|_| Error::Unknown("Failed to downcast state".to_string()))
140    }
141    /// Returns an immutable reference to the state if it is of type `Q`; returns `None`
142    /// otherwise.
143    pub fn downcast_ref<Q>(&self) -> Option<State<&Q>>
144    where
145        Q: core::any::Any,
146    {
147        self.get().downcast_ref().map(State)
148    }
149
150    /// Returns a mutable reference to the state if it is of type `Q`; returns `None`
151    /// otherwise.
152    pub fn downcast_mut<Q>(&mut self) -> Option<State<&mut Q>>
153    where
154        Q: core::any::Any,
155    {
156        self.get_mut().downcast_mut().map(State)
157    }
158}
159
160impl<Q> State<Option<Q>>
161where
162    Q: RawState,
163{
164    /// Creates a new instance of state whose inner state is [Option::None].
165    pub const fn none() -> Self {
166        Self(None)
167    }
168    /// Creates a new instance of state whose inner state is [Option::Some].
169    pub const fn some(value: Q) -> Self {
170        Self(Some(value))
171    }
172}
173
174impl<Q> State<Halt<Q>>
175where
176    Q: RawState,
177{
178    /// Creates a new instance of [State] from a [Halt] state.
179    pub fn halted(Halt(inner): Halt<Q>) -> Self {
180        Self(Halt(inner))
181    }
182    /// Converts the halted state into an unhalted state.
183    pub fn unhalt(self) -> State<Q>
184    where
185        Q: RawState,
186    {
187        State(self.value().get())
188    }
189}