rstm_state/impls/
impl_state_repr.rs

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