dd_sds/scanner/
shared_data.rs1use ahash::AHashMap;
2use std::any::{Any, TypeId};
3
4pub struct SharedData {
5 map: AHashMap<TypeId, Box<dyn Any + Send + Sync>>,
6}
7
8impl SharedData {
9 pub fn new() -> Self {
10 Self {
11 map: AHashMap::new(),
12 }
13 }
14
15 pub fn get_mut_or_default<T: Default + Send + Sync + 'static>(&mut self) -> &mut T {
16 let any = self
17 .map
18 .entry(TypeId::of::<T>())
19 .or_insert_with(|| Box::new(T::default()));
20 any.downcast_mut().unwrap()
21 }
22
23 pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T> {
24 Some(self.map.get(&TypeId::of::<T>())?.downcast_ref().unwrap())
25 }
26
27 pub fn get_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T> {
28 Some(
29 self.map
30 .get_mut(&TypeId::of::<T>())?
31 .downcast_mut()
32 .unwrap(),
33 )
34 }
35
36 pub fn insert<T: Send + Sync + 'static>(&mut self, value: T) {
37 self.map.insert(TypeId::of::<T>(), Box::new(value));
38 }
39
40 pub fn insert_if_not_contains<T: Send + Sync + 'static>(
41 &mut self,
42 get_value: impl FnOnce() -> T,
43 ) {
44 self.map
45 .entry(TypeId::of::<T>())
46 .or_insert_with(|| Box::new(get_value()));
47 }
48}