Skip to main content

rstm_state/impls/
impl_state_repr.rs

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