rstm_state/impls/
impl_state_repr.rs1use 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 pub fn cloned(&self) -> State<Q>
20 where
21 Q: Clone,
22 {
23 State(self.0.clone())
24 }
25 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 pub fn cloned(&self) -> State<Q>
40 where
41 Q: Clone,
42 {
43 State(self.0.clone())
44 }
45 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 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 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 pub fn init(value: Q) -> Self {
80 Self(MaybeUninit::new(value))
81 }
82 pub const fn uninit() -> Self {
84 Self(MaybeUninit::uninit())
85 }
86 #[allow(clippy::missing_safety_doc)]
87 pub unsafe fn assume_init(self) -> State<Q> {
94 State(unsafe { self.value().assume_init() })
95 }
96 pub fn is_null(&self) -> bool {
98 self.get().as_ptr().is_null()
99 }
100 pub fn write(&mut self, value: Q) -> &mut Q {
102 self.get_mut().write(value)
103 }
104}
105
106impl State<()> {
107 pub const fn empty() -> Self {
109 Self(())
110 }
111}
112
113impl State<bool> {
114 pub const fn from_true() -> Self {
116 Self(true)
117 }
118 pub const fn from_false() -> Self {
120 Self(false)
121 }
122 pub fn is_true(&self) -> bool {
124 self.value()
125 }
126 pub fn is_false(&self) -> bool {
128 !self.value()
129 }
130}
131#[cfg(feature = "alloc")]
132impl State<Box<dyn core::any::Any>> {
133 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 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 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 pub const fn none() -> Self {
169 Self(None)
170 }
171 pub const fn some(value: Q) -> Self {
173 Self(Some(value))
174 }
175}