1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
use std::marker::PhantomData; use crate::typedkey::TypedKey; use crate::typedvalue::TypedMapValue; use crate::TypedMapKey; pub struct OccupiedEntry<'a, K, Marker> where K: 'static + TypedMapKey<Marker>, { base: std::collections::hash_map::OccupiedEntry<'a, TypedKey, TypedMapValue>, _phantom: std::marker::PhantomData<Marker>, _phantom_key: std::marker::PhantomData<K>, } impl<'a, K, Marker> OccupiedEntry<'a, K, Marker> where K: 'static + TypedMapKey<Marker>, { #[inline] pub fn into_mut(self) -> &'a mut K::Value { self.base .into_mut() .downcast_mut::<K::Value>() .expect("Broken TypedMap entry") } #[inline] pub fn key(&self) -> &K { self.base .key() .downcast_ref::<K>() .expect("Broken TypedMap entry") } #[inline] pub fn remove_entry(self) -> (K, K::Value) { let (k, v) = self.base.remove_entry(); let v = v .downcast::<K::Value>() .ok() .expect("Broken TypedMap entry"); let k = k.downcast::<K>().ok().expect("Broken TypedMap entry"); (k, v) } #[inline] pub fn get(&self) -> &K::Value { self.base .get() .downcast_ref::<K::Value>() .expect("Broken TypedMap entry") } #[inline] pub fn get_mut(&mut self) -> &mut K::Value { self.base .get_mut() .downcast_mut::<K::Value>() .expect("Broken TypedMap entry") } #[inline] pub fn insert(&mut self, value: K::Value) -> K::Value { let value = TypedMapValue::from_value(value); self.base .insert(value) .downcast::<K::Value>() .ok() .expect("Broken TypedMap entry") } #[inline] pub fn remove(self) -> K::Value { self.base .remove() .downcast::<K::Value>() .ok() .expect("Broken TypedMap entry") } } pub struct VacantEntry<'a, K, Marker> where K: 'static + TypedMapKey<Marker>, { base: std::collections::hash_map::VacantEntry<'a, TypedKey, TypedMapValue>, _phantom: std::marker::PhantomData<Marker>, _phantom_key: std::marker::PhantomData<K>, } impl<'a, K, Marker> VacantEntry<'a, K, Marker> where K: 'static + TypedMapKey<Marker>, { #[inline] pub fn key(&self) -> &K { self.base .key() .downcast_ref::<K>() .expect("Broken TypedMap entry") } #[inline] pub fn into_key(self) -> K { self.base .into_key() .downcast::<K>() .ok() .expect("Broken TypedMap entry") } #[inline] pub fn insert(self, value: K::Value) -> &'a mut K::Value { let value = TypedMapValue::from_value(value); self.base .insert(value) .downcast_mut::<K::Value>() .expect("Broken TypedMap entry") } } pub enum Entry<'a, K, Marker> where K: 'static + TypedMapKey<Marker>, { Occupied(OccupiedEntry<'a, K, Marker>), Vacant(VacantEntry<'a, K, Marker>), } pub(crate) fn map_entry<Marker, K: 'static + TypedMapKey<Marker>>( entry: std::collections::hash_map::Entry<'_, TypedKey, TypedMapValue>, ) -> Entry<'_, K, Marker> { match entry { std::collections::hash_map::Entry::Occupied(base) => Entry::Occupied(OccupiedEntry { base, _phantom: PhantomData, _phantom_key: PhantomData, }), std::collections::hash_map::Entry::Vacant(base) => Entry::Vacant(VacantEntry { base, _phantom: PhantomData, _phantom_key: PhantomData, }), } } impl<'a, Marker, K: 'static + TypedMapKey<Marker>> Entry<'a, K, Marker> { #[inline] pub fn or_insert(self, default: K::Value) -> &'a mut K::Value { match self { Self::Occupied(entry) => entry.into_mut(), Self::Vacant(entry) => entry.insert(default), } } #[inline] pub fn or_insert_with<F: FnOnce() -> K::Value>(self, default: F) -> &'a mut K::Value { match self { Self::Occupied(entry) => entry.into_mut(), Self::Vacant(entry) => entry.insert(default()), } } pub fn key(&self) -> &K { match *self { Self::Occupied(ref entry) => entry.key(), Self::Vacant(ref entry) => entry.key(), } } pub fn and_modify<F>(self, f: F) -> Self where F: FnOnce(&mut K::Value), { match self { Self::Occupied(mut entry) => { f(entry.get_mut()); Self::Occupied(entry) } Self::Vacant(entry) => Self::Vacant(entry), } } } impl<'a, Marker, K: 'static + TypedMapKey<Marker>> Entry<'a, K, Marker> where K::Value: Default, { pub fn or_default(self) -> &'a mut K::Value { match self { Self::Occupied(entry) => entry.into_mut(), Self::Vacant(entry) => entry.insert(Default::default()), } } }