rstm_core/state/impls/
impl_repr.rs1use crate::error::Error;
6use crate::state::{Halt, State};
7use core::mem::MaybeUninit;
8
9impl<'a, Q> State<&'a Q> {
10 pub fn cloned(&self) -> State<Q>
12 where
13 Q: Clone,
14 {
15 State(self.0.clone())
16 }
17 pub fn copied(&self) -> State<Q>
19 where
20 Q: Copy,
21 {
22 State(*self.0)
23 }
24}
25
26impl<'a, Q> State<&'a mut Q> {
27 pub fn cloned(&self) -> State<Q>
29 where
30 Q: Clone,
31 {
32 State(self.0.clone())
33 }
34 pub fn copied(&self) -> State<Q>
36 where
37 Q: Copy,
38 {
39 State(*self.0)
40 }
41}
42
43impl<Q> State<*const Q> {
44 pub fn from_ptr(ptr: *const Q) -> Self {
46 Self(ptr)
47 }
48}
49
50impl<Q> State<*mut Q> {
51 pub fn from_mut_ptr(ptr: *mut Q) -> Self {
53 Self(ptr)
54 }
55}
56
57impl<Q> State<MaybeUninit<Q>> {
58 pub fn init(value: Q) -> Self {
60 Self(MaybeUninit::new(value))
61 }
62 pub const fn uninit() -> Self {
64 Self(MaybeUninit::uninit())
65 }
66 pub unsafe fn assume_init(self) -> State<Q> {
68 State(unsafe { self.into_inner().assume_init() })
69 }
70 pub fn is_null(&self) -> bool {
72 self.get().as_ptr().is_null()
73 }
74 pub fn write(&mut self, value: Q) -> &mut Q {
76 self.get_mut().write(value)
77 }
78}
79
80impl State<()> {
81 pub fn empty() -> Self {
83 Self(())
84 }
85}
86
87impl State<bool> {
88 pub fn from_true() -> Self {
89 Self(true)
90 }
91
92 pub fn from_false() -> Self {
93 Self(false)
94 }
95
96 pub fn is_true(&self) -> bool {
97 self.into_inner()
98 }
99
100 pub fn is_false(&self) -> bool {
101 !self.into_inner()
102 }
103}
104
105impl State<Box<dyn core::any::Any>> {
106 pub fn downcast<Q>(self) -> Result<State<Box<Q>>, Error>
109 where
110 Q: core::any::Any,
111 {
112 self.into_inner()
113 .downcast()
114 .map(State)
115 .map_err(|_| Error::TypeError("Failed to downcast state".to_string()))
116 }
117 pub fn downcast_ref<Q>(&self) -> Option<State<&Q>>
120 where
121 Q: core::any::Any,
122 {
123 self.get().downcast_ref().map(State)
124 }
125
126 pub fn downcast_mut<Q>(&mut self) -> Option<State<&mut Q>>
129 where
130 Q: core::any::Any,
131 {
132 self.get_mut().downcast_mut().map(State)
133 }
134}
135
136impl<Q> State<Option<Q>> {
137 pub fn none() -> Self {
139 Self(None)
140 }
141 pub fn some(value: Q) -> Self {
143 Self(Some(value))
144 }
145}
146
147impl<Q> State<Halt<Q>> {
148 pub fn halted(Halt(inner): Halt<Q>) -> Self {
150 Self(Halt(inner))
151 }
152 pub fn unhalt(self) -> State<Q> {
154 State(self.into_inner().get())
155 }
156}