|
| 1 | +use crate::any::element::{AnyMapElement, AnyMapTrait}; |
| 2 | +use std::any::TypeId; |
| 3 | +use std::collections::HashMap; |
| 4 | +use std::hash::Hash; |
| 5 | + |
| 6 | +/// Stores any object by `Key`. |
| 7 | +#[derive(Clone, Debug)] |
| 8 | +pub struct AnyMap<Key: Hash + Eq>(HashMap<Key, AnyMapElement>); |
| 9 | + |
| 10 | +impl<Key: Hash + Eq> Default for AnyMap<Key> { |
| 11 | + fn default() -> Self { |
| 12 | + AnyMap(HashMap::new()) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | + |
| 18 | +impl<Key: Hash + Eq> AnyMap<Key> { |
| 19 | + pub fn get<T: AnyMapTrait>(&mut self, key: &Key) -> Option<&T> { |
| 20 | + self.get_mut(key).map(|x| &*x) |
| 21 | + } |
| 22 | + |
| 23 | + pub fn get_mut<T: AnyMapTrait>(&mut self, key: &Key) -> Option<&mut T> { |
| 24 | + self.0.get_mut(key)?.get_mut() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl<Key: Hash + Eq> AnyMap<Key> { |
| 29 | + pub fn get_or_insert_with<T: AnyMapTrait>( |
| 30 | + &mut self, |
| 31 | + key: Key, |
| 32 | + or_insert_with: impl FnOnce() -> T, |
| 33 | + ) -> &T { |
| 34 | + &*self.get_mut_or_insert_with(key, or_insert_with) |
| 35 | + } |
| 36 | + |
| 37 | + pub fn get_or_default<T: AnyMapTrait + Default>(&mut self, key: Key) -> &T { |
| 38 | + self.get_or_insert_with(key, Default::default) |
| 39 | + } |
| 40 | + |
| 41 | + pub fn get_mut_or_insert_with<T: AnyMapTrait>( |
| 42 | + &mut self, |
| 43 | + key: Key, |
| 44 | + or_insert_with: impl FnOnce() -> T, |
| 45 | + ) -> &mut T { |
| 46 | + use std::collections::hash_map::Entry; |
| 47 | + match self.0.entry(key) { |
| 48 | + Entry::Vacant(vacant) => vacant |
| 49 | + .insert(AnyMapElement::new(or_insert_with())) |
| 50 | + .get_mut() |
| 51 | + .unwrap(), // this unwrap will never panic, because we insert correct type right now |
| 52 | + Entry::Occupied(occupied) => occupied.into_mut().get_mut_or_set_with(or_insert_with), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn get_mut_or_default<T: AnyMapTrait + Default>(&mut self, key: Key) -> &mut T { |
| 57 | + self.get_mut_or_insert_with(key, Default::default) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl<Key: Hash + Eq> AnyMap<Key> { |
| 62 | + pub fn insert<T: AnyMapTrait>(&mut self, key: Key, element: T) { |
| 63 | + self.0.insert(key, AnyMapElement::new(element)); |
| 64 | + } |
| 65 | + |
| 66 | + pub fn remove(&mut self, key: &Key) { |
| 67 | + self.0.remove(key); |
| 68 | + } |
| 69 | + |
| 70 | + pub fn remove_by_type<T: AnyMapTrait>(&mut self) { |
| 71 | + let key = TypeId::of::<T>(); |
| 72 | + self.0.retain(|_, v| v.type_id() != key); |
| 73 | + } |
| 74 | + |
| 75 | + pub fn clear(&mut self) { |
| 76 | + self.0.clear(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl<Key: Hash + Eq> AnyMap<Key> { |
| 81 | + /// You could use this function to find is there some leak or misusage. |
| 82 | + pub fn count<T: AnyMapTrait>(&mut self) -> usize { |
| 83 | + let key = TypeId::of::<T>(); |
| 84 | + self.0.iter().filter(|(_, v)| v.type_id() == key).count() |
| 85 | + } |
| 86 | + |
| 87 | + pub fn count_all(&mut self) -> usize { |
| 88 | + self.0.len() |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// ---------------------------------------------------------------------------- |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +#[test] |
| 96 | +fn basic_usage() { |
| 97 | + #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 98 | + struct State { |
| 99 | + a: i32, |
| 100 | + } |
| 101 | + |
| 102 | + let mut map: AnyMap<i32> = Default::default(); |
| 103 | + |
| 104 | + assert!(map.get::<State>(&0).is_none()); |
| 105 | + map.insert(0, State { a: 42 }); |
| 106 | + |
| 107 | + assert_eq!(*map.get::<State>(&0).unwrap(), State { a: 42 }); |
| 108 | + assert!(map.get::<State>(&1).is_none()); |
| 109 | + map.get_mut::<State>(&0).unwrap().a = 43; |
| 110 | + assert_eq!(*map.get::<State>(&0).unwrap(), State { a: 43 }); |
| 111 | + |
| 112 | + map.remove(&0); |
| 113 | + assert!(map.get::<State>(&0).is_none()); |
| 114 | + |
| 115 | + assert_eq!( |
| 116 | + *map.get_or_insert_with(0, || State { a: 55 }), |
| 117 | + State { a: 55 } |
| 118 | + ); |
| 119 | + map.remove(&0); |
| 120 | + assert_eq!( |
| 121 | + *map.get_mut_or_insert_with(0, || State { a: 56 }), |
| 122 | + State { a: 56 } |
| 123 | + ); |
| 124 | + map.remove(&0); |
| 125 | + assert_eq!(*map.get_or_default::<State>(0), State { a: 0 }); |
| 126 | + map.remove(&0); |
| 127 | + assert_eq!(*map.get_mut_or_default::<State>(0), State { a: 0 }); |
| 128 | +} |
| 129 | + |
| 130 | +#[cfg(test)] |
| 131 | +#[test] |
| 132 | +fn different_type_same_id() { |
| 133 | + #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 134 | + struct State { |
| 135 | + a: i32, |
| 136 | + } |
| 137 | + |
| 138 | + let mut map: AnyMap<i32> = Default::default(); |
| 139 | + |
| 140 | + map.insert(0, State { a: 42 }); |
| 141 | + |
| 142 | + assert_eq!(*map.get::<State>(&0).unwrap(), State { a: 42 }); |
| 143 | + assert!(map.get::<i32>(&0).is_none()); |
| 144 | + |
| 145 | + map.insert(0, 255i32); |
| 146 | + |
| 147 | + assert_eq!(*map.get::<i32>(&0).unwrap(), 255); |
| 148 | + assert!(map.get::<State>(&0).is_none()); |
| 149 | +} |
| 150 | + |
| 151 | +#[cfg(test)] |
| 152 | +#[test] |
| 153 | +fn cloning() { |
| 154 | + #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 155 | + struct State { |
| 156 | + a: i32, |
| 157 | + } |
| 158 | + |
| 159 | + let mut map: AnyMap<i32> = Default::default(); |
| 160 | + |
| 161 | + map.insert(0, State::default()); |
| 162 | + map.insert(10, 10i32); |
| 163 | + map.insert(11, 11i32); |
| 164 | + |
| 165 | + let mut cloned_map = map.clone(); |
| 166 | + |
| 167 | + map.insert(12, 12i32); |
| 168 | + map.insert(1, State { a: 10 }); |
| 169 | + |
| 170 | + assert_eq!(*cloned_map.get::<State>(&0).unwrap(), State { a: 0 }); |
| 171 | + assert!(cloned_map.get::<State>(&1).is_none()); |
| 172 | + assert_eq!(*cloned_map.get::<i32>(&10).unwrap(), 10i32); |
| 173 | + assert_eq!(*cloned_map.get::<i32>(&11).unwrap(), 11i32); |
| 174 | + assert!(cloned_map.get::<i32>(&12).is_none()); |
| 175 | +} |
| 176 | + |
| 177 | +#[cfg(test)] |
| 178 | +#[test] |
| 179 | +fn counting() { |
| 180 | + #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 181 | + struct State { |
| 182 | + a: i32, |
| 183 | + } |
| 184 | + |
| 185 | + let mut map: AnyMap<i32> = Default::default(); |
| 186 | + |
| 187 | + map.insert(0, State::default()); |
| 188 | + map.insert(1, State { a: 10 }); |
| 189 | + map.insert(10, 10i32); |
| 190 | + map.insert(11, 11i32); |
| 191 | + map.insert(12, 12i32); |
| 192 | + |
| 193 | + assert_eq!(map.count::<State>(), 2); |
| 194 | + assert_eq!(map.count::<i32>(), 3); |
| 195 | + |
| 196 | + map.remove_by_type::<State>(); |
| 197 | + |
| 198 | + assert_eq!(map.count::<State>(), 0); |
| 199 | + assert_eq!(map.count::<i32>(), 3); |
| 200 | + |
| 201 | + map.clear(); |
| 202 | + |
| 203 | + assert_eq!(map.count::<State>(), 0); |
| 204 | + assert_eq!(map.count::<i32>(), 0); |
| 205 | +} |
0 commit comments