1 use std::fmt; 2 use std::fmt::Debug; 3 use std::fmt::Formatter; 4 5 use crate::reflect::map::empty::DynamicEmptyMap; 6 use crate::reflect::reflect_eq::ReflectEq; 7 use crate::reflect::reflect_eq::ReflectEqMode; 8 use crate::reflect::ReflectValueBox; 9 use crate::reflect::ReflectValueRef; 10 use crate::reflect::RuntimeType; 11 12 mod empty; 13 mod generated; 14 15 /// Implemented for `HashMap` with appropriate keys and values 16 pub(crate) trait ReflectMap: Debug + Send + Sync + 'static { reflect_iter(&self) -> ReflectMapIter17 fn reflect_iter(&self) -> ReflectMapIter; 18 len(&self) -> usize19 fn len(&self) -> usize; 20 is_empty(&self) -> bool21 fn is_empty(&self) -> bool; 22 get<'a>(&'a self, key: ReflectValueRef) -> Option<ReflectValueRef<'a>>23 fn get<'a>(&'a self, key: ReflectValueRef) -> Option<ReflectValueRef<'a>>; 24 insert(&mut self, key: ReflectValueBox, value: ReflectValueBox)25 fn insert(&mut self, key: ReflectValueBox, value: ReflectValueBox); 26 clear(&mut self)27 fn clear(&mut self); 28 key_type(&self) -> RuntimeType29 fn key_type(&self) -> RuntimeType; 30 value_type(&self) -> RuntimeType31 fn value_type(&self) -> RuntimeType; 32 } 33 34 pub(crate) trait ReflectMapIterTrait<'a> { next(&mut self) -> Option<(ReflectValueRef<'a>, ReflectValueRef<'a>)>35 fn next(&mut self) -> Option<(ReflectValueRef<'a>, ReflectValueRef<'a>)>; key_type(&self) -> RuntimeType36 fn key_type(&self) -> RuntimeType; value_type(&self) -> RuntimeType37 fn value_type(&self) -> RuntimeType; 38 } 39 40 pub struct ReflectMapIter<'a> { 41 imp: Box<dyn ReflectMapIterTrait<'a> + 'a>, 42 } 43 44 impl<'a> ReflectMapIter<'a> { new<I: ReflectMapIterTrait<'a> + 'a>(imp: I) -> ReflectMapIter<'a>45 pub(crate) fn new<I: ReflectMapIterTrait<'a> + 'a>(imp: I) -> ReflectMapIter<'a> { 46 ReflectMapIter { imp: Box::new(imp) } 47 } 48 } 49 50 impl<'a> Iterator for ReflectMapIter<'a> { 51 type Item = (ReflectValueRef<'a>, ReflectValueRef<'a>); 52 next(&mut self) -> Option<(ReflectValueRef<'a>, ReflectValueRef<'a>)>53 fn next(&mut self) -> Option<(ReflectValueRef<'a>, ReflectValueRef<'a>)> { 54 self.imp.next() 55 } 56 } 57 58 impl<'a> IntoIterator for &'a dyn ReflectMap { 59 type Item = (ReflectValueRef<'a>, ReflectValueRef<'a>); 60 type IntoIter = ReflectMapIter<'a>; 61 into_iter(self) -> Self::IntoIter62 fn into_iter(self) -> Self::IntoIter { 63 self.reflect_iter() 64 } 65 } 66 67 #[derive(Clone)] 68 enum ReflectMapRefImpl<'a> { 69 Generated(&'a dyn ReflectMap), 70 DynamicEmpty(DynamicEmptyMap), 71 } 72 73 impl<'a> fmt::Debug for ReflectMapRefImpl<'a> { fmt(&self, f: &mut Formatter) -> fmt::Result74 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 75 match self { 76 ReflectMapRefImpl::Generated(m) => fmt::Debug::fmt(m, f), 77 ReflectMapRefImpl::DynamicEmpty(m) => fmt::Debug::fmt(m, f), 78 } 79 } 80 } 81 82 /// Dynamic reference to `map` field 83 #[derive(Clone)] 84 pub struct ReflectMapRef<'a> { 85 imp: ReflectMapRefImpl<'a>, 86 } 87 88 /// Dynamic mutable reference to `map` field 89 pub struct ReflectMapMut<'a> { 90 map: &'a mut dyn ReflectMap, 91 } 92 93 impl<'a> fmt::Debug for ReflectMapRef<'a> { fmt(&self, f: &mut Formatter<'_>) -> fmt::Result94 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 95 fmt::Debug::fmt(&self.imp, f) 96 } 97 } 98 99 impl<'a> fmt::Debug for ReflectMapMut<'a> { fmt(&self, f: &mut Formatter<'_>) -> fmt::Result100 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 101 fmt::Debug::fmt(&self.map, f) 102 } 103 } 104 105 impl<'a> ReflectMapRef<'a> { new(map: &'a dyn ReflectMap) -> ReflectMapRef<'a>106 pub(crate) fn new(map: &'a dyn ReflectMap) -> ReflectMapRef<'a> { 107 ReflectMapRef { 108 imp: ReflectMapRefImpl::Generated(map), 109 } 110 } 111 new_empty(key: RuntimeType, value: RuntimeType) -> ReflectMapRef<'a>112 pub(crate) fn new_empty(key: RuntimeType, value: RuntimeType) -> ReflectMapRef<'a> { 113 ReflectMapRef { 114 imp: ReflectMapRefImpl::DynamicEmpty(DynamicEmptyMap::new(key, value)), 115 } 116 } 117 118 /// Size of the map len(&self) -> usize119 pub fn len(&self) -> usize { 120 match &self.imp { 121 ReflectMapRefImpl::Generated(map) => map.len(), 122 ReflectMapRefImpl::DynamicEmpty(map) => map.len(), 123 } 124 } 125 126 /// Is map empty? is_empty(&self) -> bool127 pub fn is_empty(&self) -> bool { 128 match &self.imp { 129 ReflectMapRefImpl::Generated(map) => map.is_empty(), 130 ReflectMapRefImpl::DynamicEmpty(map) => map.is_empty(), 131 } 132 } 133 134 /// Find a value by given key. get(&self, key: ReflectValueRef) -> Option<ReflectValueRef>135 pub fn get(&self, key: ReflectValueRef) -> Option<ReflectValueRef> { 136 match &self.imp { 137 ReflectMapRefImpl::Generated(map) => map.get(key), 138 ReflectMapRefImpl::DynamicEmpty(map) => map.get(key), 139 } 140 } 141 142 /// Map key type key_type(&self) -> RuntimeType143 pub fn key_type(&self) -> RuntimeType { 144 match &self.imp { 145 ReflectMapRefImpl::Generated(map) => map.key_type(), 146 ReflectMapRefImpl::DynamicEmpty(map) => map.key_type(), 147 } 148 } 149 150 /// Map value type value_type(&self) -> RuntimeType151 pub fn value_type(&self) -> RuntimeType { 152 match &self.imp { 153 ReflectMapRefImpl::Generated(map) => map.value_type(), 154 ReflectMapRefImpl::DynamicEmpty(map) => map.value_type(), 155 } 156 } 157 } 158 159 impl<'a> PartialEq for ReflectMapRef<'a> { eq(&self, other: &Self) -> bool160 fn eq(&self, other: &Self) -> bool { 161 self.reflect_eq(other, &ReflectEqMode::default()) 162 } 163 } 164 165 impl<'a> ReflectEq for ReflectMapRef<'a> { reflect_eq(&self, that: &Self, mode: &ReflectEqMode) -> bool166 fn reflect_eq(&self, that: &Self, mode: &ReflectEqMode) -> bool { 167 let len = self.len(); 168 169 if len != that.len() { 170 return false; 171 } 172 173 if self.key_type() != that.key_type() { 174 return false; 175 } 176 if self.value_type() != that.value_type() { 177 return false; 178 } 179 180 for (k, va) in self { 181 let vb = match that.get(k) { 182 Some(v) => v, 183 None => return false, 184 }; 185 186 if !va.reflect_eq(&vb, mode) { 187 return false; 188 } 189 } 190 191 true 192 } 193 } 194 195 impl<'a> ReflectMapMut<'a> { new(map: &'a mut dyn ReflectMap) -> ReflectMapMut<'a>196 pub(crate) fn new(map: &'a mut dyn ReflectMap) -> ReflectMapMut<'a> { 197 ReflectMapMut { map } 198 } 199 as_ref(&'a self) -> ReflectMapRef<'a>200 fn as_ref(&'a self) -> ReflectMapRef<'a> { 201 ReflectMapRef::new(self.map) 202 } 203 204 /// Map key type key_type(&self) -> RuntimeType205 pub fn key_type(&self) -> RuntimeType { 206 self.map.key_type() 207 } 208 209 /// Map value type value_type(&self) -> RuntimeType210 pub fn value_type(&self) -> RuntimeType { 211 self.map.value_type() 212 } 213 214 /// Number of map entries len(&self) -> usize215 pub fn len(&self) -> usize { 216 self.as_ref().len() 217 } 218 219 /// Is this map empty? is_empty(&self) -> bool220 pub fn is_empty(&self) -> bool { 221 self.as_ref().is_empty() 222 } 223 224 /// Find a value for given key get(&self, key: ReflectValueRef) -> Option<ReflectValueRef>225 pub fn get(&self, key: ReflectValueRef) -> Option<ReflectValueRef> { 226 self.map.get(key) 227 } 228 229 /// Insert a value into the map. 230 /// 231 /// # Panics 232 /// 233 /// If given key has an incompatible key type. insert(&mut self, key: ReflectValueBox, value: ReflectValueBox)234 pub fn insert(&mut self, key: ReflectValueBox, value: ReflectValueBox) { 235 self.map.insert(key, value) 236 } 237 238 /// Clear clear(&mut self)239 pub fn clear(&mut self) { 240 self.map.clear(); 241 } 242 } 243 244 /// Iterator over map 245 pub struct ReflectMapRefIter<'a> { 246 iter: ReflectMapIter<'a>, 247 } 248 249 impl<'a> ReflectMapRefIter<'a> { _key_type(&self) -> RuntimeType250 fn _key_type(&self) -> RuntimeType { 251 self.iter.imp.key_type() 252 } 253 _value_type(&self) -> RuntimeType254 fn _value_type(&self) -> RuntimeType { 255 self.iter.imp.value_type() 256 } 257 } 258 259 impl<'a> Iterator for ReflectMapRefIter<'a> { 260 type Item = (ReflectValueRef<'a>, ReflectValueRef<'a>); 261 next(&mut self) -> Option<(ReflectValueRef<'a>, ReflectValueRef<'a>)>262 fn next(&mut self) -> Option<(ReflectValueRef<'a>, ReflectValueRef<'a>)> { 263 self.iter.next() 264 } 265 } 266 267 impl<'a, 'b: 'a> IntoIterator for &'b ReflectMapRef<'a> { 268 type Item = (ReflectValueRef<'a>, ReflectValueRef<'a>); 269 type IntoIter = ReflectMapRefIter<'a>; 270 into_iter(self) -> ReflectMapRefIter<'a>271 fn into_iter(self) -> ReflectMapRefIter<'a> { 272 match &self.imp { 273 ReflectMapRefImpl::Generated(map) => ReflectMapRefIter { 274 iter: map.reflect_iter(), 275 }, 276 ReflectMapRefImpl::DynamicEmpty(map) => ReflectMapRefIter { 277 iter: map.reflect_iter(), 278 }, 279 } 280 } 281 } 282