1use std::{
6 any::{Any, TypeId},
7 collections::HashMap,
8 hash::BuildHasherDefault,
9 pin::Pin,
10 sync::Mutex,
11};
12
13use crate::{
14 ipc::{CommandArg, CommandItem, InvokeError},
15 Runtime,
16};
17
18pub struct State<'r, T: Send + Sync + 'static>(&'r T);
22
23impl<'r, T: Send + Sync + 'static> State<'r, T> {
24 #[inline(always)]
28 pub fn inner(&self) -> &'r T {
29 self.0
30 }
31}
32
33impl<T: Send + Sync + 'static> std::ops::Deref for State<'_, T> {
34 type Target = T;
35
36 #[inline(always)]
37 fn deref(&self) -> &T {
38 self.0
39 }
40}
41
42impl<T: Send + Sync + 'static> Clone for State<'_, T> {
43 fn clone(&self) -> Self {
44 State(self.0)
45 }
46}
47
48impl<T: Send + Sync + 'static + PartialEq> PartialEq for State<'_, T> {
49 fn eq(&self, other: &Self) -> bool {
50 self.0 == other.0
51 }
52}
53
54impl<T: Send + Sync + std::fmt::Debug> std::fmt::Debug for State<'_, T> {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 f.debug_tuple("State").field(&self.0).finish()
57 }
58}
59
60impl<'r, 'de: 'r, T: Send + Sync + 'static, R: Runtime> CommandArg<'de, R> for State<'r, T> {
61 fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError> {
63 command.message.state_ref().try_get().ok_or_else(|| {
64 InvokeError::from_anyhow(anyhow::anyhow!(
65 "state not managed for field `{}` on command `{}`. You must call `.manage()` before using this command",
66 command.key, command.name
67 ))
68 })
69 }
70}
71
72#[derive(Default)]
78struct IdentHash(u64);
79
80impl std::hash::Hasher for IdentHash {
81 fn finish(&self) -> u64 {
82 self.0
83 }
84
85 fn write(&mut self, bytes: &[u8]) {
86 for byte in bytes {
87 self.write_u8(*byte);
88 }
89 }
90
91 fn write_u8(&mut self, i: u8) {
92 self.0 = (self.0 << 8) | (i as u64);
93 }
94
95 fn write_u64(&mut self, i: u64) {
96 self.0 = i;
97 }
98}
99
100type TypeIdMap = HashMap<TypeId, Pin<Box<dyn Any + Sync + Send>>, BuildHasherDefault<IdentHash>>;
104
105#[derive(Debug)]
107pub struct StateManager {
108 map: Mutex<TypeIdMap>,
109}
110
111impl StateManager {
112 pub(crate) fn new() -> Self {
113 Self {
114 map: Default::default(),
115 }
116 }
117
118 pub(crate) fn set<T: Send + Sync + 'static>(&self, state: T) -> bool {
119 let mut map = self.map.lock().unwrap();
120 let type_id = TypeId::of::<T>();
121 let already_set = map.contains_key(&type_id);
122 if !already_set {
123 let ptr = Box::new(state) as Box<dyn Any + Sync + Send>;
124 let pinned_ptr = Box::into_pin(ptr);
125 map.insert(
126 type_id,
127 pinned_ptr,
130 );
131 }
132 !already_set
133 }
134
135 pub(crate) unsafe fn unmanage<T: Send + Sync + 'static>(&self) -> Option<T> {
138 let mut map = self.map.lock().unwrap();
139 let type_id = TypeId::of::<T>();
140 let pinned_ptr = map.remove(&type_id)?;
141 let ptr = unsafe { Pin::into_inner_unchecked(pinned_ptr) };
143 let value = unsafe {
144 ptr
145 .downcast::<T>()
146 .unwrap_unchecked()
148 };
149 Some(*value)
150 }
151
152 pub fn get<T: Send + Sync + 'static>(&self) -> State<'_, T> {
154 self
155 .try_get()
156 .unwrap_or_else(|| panic!("state not found for type {}", std::any::type_name::<T>()))
157 }
158
159 pub fn try_get<T: Send + Sync + 'static>(&self) -> Option<State<'_, T>> {
161 let map = self.map.lock().unwrap();
162 let type_id = TypeId::of::<T>();
163 let ptr = map.get(&type_id)?;
164 let value = unsafe {
165 ptr
166 .downcast_ref::<T>()
167 .unwrap_unchecked()
169 };
170 let v_ref = unsafe { &*(value as *const T) };
172 Some(State(v_ref))
173 }
174}
175
176#[cfg(test)]
178mod tests {
179 use super::StateManager;
180
181 use std::sync::{Arc, RwLock};
182 use std::thread;
183
184 struct DroppingStruct(Arc<RwLock<bool>>);
186 struct DroppingStructWrap(#[allow(dead_code)] DroppingStruct);
187
188 impl Drop for DroppingStruct {
189 fn drop(&mut self) {
190 *self.0.write().unwrap() = true;
191 }
192 }
193
194 #[test]
195 #[should_panic(expected = "state not found for type core::option::Option<alloc::string::String>")]
196 fn get_panics() {
197 let state = StateManager::new();
198 state.get::<Option<String>>();
199 }
200
201 #[test]
202 fn simple_set_get() {
203 let state = StateManager::new();
204 assert!(state.set(1u32));
205 assert_eq!(*state.get::<u32>(), 1);
206 }
207
208 #[test]
209 fn simple_set_get_unmanage() {
210 let state = StateManager::new();
211 assert!(state.set(1u32));
212 assert_eq!(*state.get::<u32>(), 1);
213 assert!(unsafe { state.unmanage::<u32>() }.is_some());
215 assert!(unsafe { state.unmanage::<u32>() }.is_none());
216 assert_eq!(state.try_get::<u32>(), None);
217 assert!(state.set(2u32));
218 assert_eq!(*state.get::<u32>(), 2);
219 }
220
221 #[test]
222 fn dst_set_get() {
223 let state = StateManager::new();
224 assert!(state.set::<[u32; 4]>([1, 2, 3, 4u32]));
225 assert_eq!(*state.get::<[u32; 4]>(), [1, 2, 3, 4]);
226 }
227
228 #[test]
229 fn set_get_remote() {
230 let state = Arc::new(StateManager::new());
231 let sate_ = Arc::clone(&state);
232 thread::spawn(move || {
233 sate_.set(10isize);
234 })
235 .join()
236 .unwrap();
237
238 assert_eq!(*state.get::<isize>(), 10);
239 }
240
241 #[test]
242 fn two_put_get() {
243 let state = StateManager::new();
244 assert!(state.set("Hello, world!".to_string()));
245
246 let s_old = state.get::<String>();
247 assert_eq!(*s_old, "Hello, world!");
248
249 assert!(!state.set::<String>("Bye bye!".into()));
250 assert_eq!(*state.get::<String>(), "Hello, world!");
251 assert_eq!(state.get::<String>(), s_old);
252 }
253
254 #[test]
255 fn many_puts_only_one_succeeds() {
256 let state = Arc::new(StateManager::new());
257 let mut threads = vec![];
258 for _ in 0..1000 {
259 let state_ = Arc::clone(&state);
260 threads.push(thread::spawn(move || state_.set(10i64)))
261 }
262
263 let results: Vec<bool> = threads.into_iter().map(|t| t.join().unwrap()).collect();
264 assert_eq!(results.into_iter().filter(|&b| b).count(), 1);
265 assert_eq!(*state.get::<i64>(), 10);
266 }
267
268 #[test]
270 fn test_no_drop_on_set() {
271 let state = StateManager::new();
272 let drop_flag = Arc::new(RwLock::new(false));
273 let dropping_struct = DroppingStruct(drop_flag.clone());
274
275 let _drop_flag_ignore = Arc::new(RwLock::new(false));
276 let _dropping_struct_ignore = DroppingStruct(_drop_flag_ignore);
277
278 state.set::<DroppingStruct>(dropping_struct);
279 assert!(!state.set::<DroppingStruct>(_dropping_struct_ignore));
280 assert!(!*drop_flag.read().unwrap());
281 }
282
283 #[test]
285 fn drop_inners_on_drop() {
286 let drop_flag_a = Arc::new(RwLock::new(false));
287 let dropping_struct_a = DroppingStruct(drop_flag_a.clone());
288
289 let drop_flag_b = Arc::new(RwLock::new(false));
290 let dropping_struct_b = DroppingStructWrap(DroppingStruct(drop_flag_b.clone()));
291
292 {
293 let state = StateManager::new();
294 state.set(dropping_struct_a);
295 assert!(!*drop_flag_a.read().unwrap());
296
297 state.set(dropping_struct_b);
298 assert!(!*drop_flag_a.read().unwrap());
299 assert!(!*drop_flag_b.read().unwrap());
300 }
301
302 assert!(*drop_flag_a.read().unwrap());
303 assert!(*drop_flag_b.read().unwrap());
304 }
305}