eryon_core/state/impls/
impl_repr.rs1use 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 pub fn cloned(&self) -> State<Q>
17 where
18 Q: Clone,
19 {
20 State(self.0.clone())
21 }
22 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 pub fn cloned(&self) -> State<Q>
37 where
38 Q: Clone,
39 {
40 State(self.0.clone())
41 }
42 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 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 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 pub fn init(value: Q) -> Self {
77 Self(MaybeUninit::new(value))
78 }
79 pub const fn uninit() -> Self {
81 Self(MaybeUninit::uninit())
82 }
83 #[allow(clippy::missing_safety_doc)]
84 pub unsafe fn assume_init(self) -> State<Q> {
91 State(unsafe { self.value().assume_init() })
92 }
93 pub fn is_null(&self) -> bool {
95 self.get().as_ptr().is_null()
96 }
97 pub fn write(&mut self, value: Q) -> &mut Q {
99 self.get_mut().write(value)
100 }
101}
102
103impl State<()> {
104 pub const fn empty() -> Self {
106 Self(())
107 }
108}
109
110impl State<bool> {
111 pub const fn from_true() -> Self {
113 Self(true)
114 }
115 pub const fn from_false() -> Self {
117 Self(false)
118 }
119 pub fn is_true(&self) -> bool {
121 self.value()
122 }
123 pub fn is_false(&self) -> bool {
125 !self.value()
126 }
127}
128#[cfg(feature = "alloc")]
129impl State<Box<dyn core::any::Any>> {
130 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 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 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 pub const fn none() -> Self {
166 Self(None)
167 }
168 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 pub fn halted(Halt(inner): Halt<Q>) -> Self {
180 Self(Halt(inner))
181 }
182 pub fn unhalt(self) -> State<Q>
184 where
185 Q: RawState,
186 {
187 State(self.value().get())
188 }
189}