1 use std::collections::hash_map;
2 use std::collections::HashMap;
3 use std::hash::Hash;
4 
5 use crate::reflect::map::ReflectMap;
6 use crate::reflect::map::ReflectMapIter;
7 use crate::reflect::map::ReflectMapIterTrait;
8 use crate::reflect::runtime_types::RuntimeTypeHashable;
9 use crate::reflect::runtime_types::RuntimeTypeTrait;
10 use crate::reflect::ProtobufValue;
11 use crate::reflect::ReflectValueBox;
12 use crate::reflect::ReflectValueRef;
13 use crate::reflect::RuntimeType;
14 
15 impl<K, V> ReflectMap for HashMap<K, V>
16 where
17     K: ProtobufValue + Eq + Hash,
18     V: ProtobufValue,
19     K::RuntimeType: RuntimeTypeHashable,
20 {
reflect_iter<'a>(&'a self) -> ReflectMapIter<'a>21     fn reflect_iter<'a>(&'a self) -> ReflectMapIter<'a> {
22         ReflectMapIter::new(GeneratedMapIterImpl::<'a, K, V> { iter: self.iter() })
23     }
24 
len(&self) -> usize25     fn len(&self) -> usize {
26         HashMap::len(self)
27     }
28 
is_empty(&self) -> bool29     fn is_empty(&self) -> bool {
30         self.is_empty()
31     }
32 
get<'a>(&'a self, key: ReflectValueRef) -> Option<ReflectValueRef<'a>>33     fn get<'a>(&'a self, key: ReflectValueRef) -> Option<ReflectValueRef<'a>> {
34         <K::RuntimeType as RuntimeTypeHashable>::hash_map_get(self, key).map(V::RuntimeType::as_ref)
35     }
36 
insert(&mut self, key: ReflectValueBox, value: ReflectValueBox)37     fn insert(&mut self, key: ReflectValueBox, value: ReflectValueBox) {
38         let key: K = key.downcast().expect("wrong key type");
39         let value: V = value.downcast().expect("wrong value type");
40         self.insert(key, value);
41     }
42 
clear(&mut self)43     fn clear(&mut self) {
44         self.clear();
45     }
46 
key_type(&self) -> RuntimeType47     fn key_type(&self) -> RuntimeType {
48         K::RuntimeType::runtime_type_box()
49     }
50 
value_type(&self) -> RuntimeType51     fn value_type(&self) -> RuntimeType {
52         V::RuntimeType::runtime_type_box()
53     }
54 }
55 
56 struct GeneratedMapIterImpl<'a, K: Eq + Hash + 'static, V: 'static> {
57     iter: hash_map::Iter<'a, K, V>,
58 }
59 
60 impl<'a, K: ProtobufValue + Eq + Hash, V: ProtobufValue> ReflectMapIterTrait<'a>
61     for GeneratedMapIterImpl<'a, K, V>
62 {
next(&mut self) -> Option<(ReflectValueRef<'a>, ReflectValueRef<'a>)>63     fn next(&mut self) -> Option<(ReflectValueRef<'a>, ReflectValueRef<'a>)> {
64         match self.iter.next() {
65             Some((k, v)) => Some((K::RuntimeType::as_ref(k), V::RuntimeType::as_ref(v))),
66             None => None,
67         }
68     }
69 
key_type(&self) -> RuntimeType70     fn key_type(&self) -> RuntimeType {
71         K::RuntimeType::runtime_type_box()
72     }
73 
value_type(&self) -> RuntimeType74     fn value_type(&self) -> RuntimeType {
75         V::RuntimeType::runtime_type_box()
76     }
77 }
78