protobuf/reflect/map/
generated.rs

1use std::collections::btree_map;
2use std::collections::hash_map;
3use std::collections::BTreeMap;
4use std::collections::HashMap;
5use std::hash::Hash;
6
7use crate::reflect::map::ReflectMap;
8use crate::reflect::map::ReflectMapIter;
9use crate::reflect::map::ReflectMapIterTrait;
10use crate::reflect::runtime_types::RuntimeTypeMapKey;
11use crate::reflect::runtime_types::RuntimeTypeTrait;
12use crate::reflect::ProtobufValue;
13use crate::reflect::ReflectValueBox;
14use crate::reflect::ReflectValueRef;
15use crate::reflect::RuntimeType;
16
17impl<K, V> ReflectMap for HashMap<K, V>
18where
19    K: ProtobufValue + Eq + Hash,
20    V: ProtobufValue,
21    K::RuntimeType: RuntimeTypeMapKey,
22{
23    fn reflect_iter<'a>(&'a self) -> ReflectMapIter<'a> {
24        ReflectMapIter::new(GeneratedMapIterImpl::<'a, K, V, hash_map::Iter<'a, K, V>> {
25            iter: self.iter(),
26        })
27    }
28
29    fn len(&self) -> usize {
30        HashMap::len(self)
31    }
32
33    fn is_empty(&self) -> bool {
34        self.is_empty()
35    }
36
37    fn get<'a>(&'a self, key: ReflectValueRef) -> Option<ReflectValueRef<'a>> {
38        <K::RuntimeType as RuntimeTypeMapKey>::hash_map_get(self, key).map(V::RuntimeType::as_ref)
39    }
40
41    fn insert(&mut self, key: ReflectValueBox, value: ReflectValueBox) {
42        let key: K = key.downcast().expect("wrong key type");
43        let value: V = value.downcast().expect("wrong value type");
44        self.insert(key, value);
45    }
46
47    fn clear(&mut self) {
48        self.clear();
49    }
50
51    fn key_type(&self) -> RuntimeType {
52        K::RuntimeType::runtime_type_box()
53    }
54
55    fn value_type(&self) -> RuntimeType {
56        V::RuntimeType::runtime_type_box()
57    }
58}
59
60impl<K, V> ReflectMap for BTreeMap<K, V>
61where
62    K: ProtobufValue + Ord,
63    V: ProtobufValue,
64    K::RuntimeType: RuntimeTypeMapKey,
65{
66    fn reflect_iter<'a>(&'a self) -> ReflectMapIter<'a> {
67        ReflectMapIter::new(
68            GeneratedMapIterImpl::<'a, K, V, btree_map::Iter<'a, K, V>> { iter: self.iter() },
69        )
70    }
71
72    fn len(&self) -> usize {
73        BTreeMap::len(self)
74    }
75
76    fn is_empty(&self) -> bool {
77        self.is_empty()
78    }
79
80    fn get<'a>(&'a self, key: ReflectValueRef) -> Option<ReflectValueRef<'a>> {
81        <K::RuntimeType as RuntimeTypeMapKey>::btree_map_get(self, key).map(V::RuntimeType::as_ref)
82    }
83
84    fn insert(&mut self, key: ReflectValueBox, value: ReflectValueBox) {
85        let key: K = key.downcast().expect("wrong key type");
86        let value: V = value.downcast().expect("wrong value type");
87        self.insert(key, value);
88    }
89
90    fn clear(&mut self) {
91        self.clear();
92    }
93
94    fn key_type(&self) -> RuntimeType {
95        K::RuntimeType::runtime_type_box()
96    }
97
98    fn value_type(&self) -> RuntimeType {
99        V::RuntimeType::runtime_type_box()
100    }
101}
102
103struct GeneratedMapIterImpl<'a, K: 'static, V: 'static, I: Iterator<Item = (&'a K, &'a V)>> {
104    iter: I,
105}
106
107impl<'a, K: ProtobufValue, V: ProtobufValue, I: Iterator<Item = (&'a K, &'a V)>>
108    ReflectMapIterTrait<'a> for GeneratedMapIterImpl<'a, K, V, I>
109{
110    fn next(&mut self) -> Option<(ReflectValueRef<'a>, ReflectValueRef<'a>)> {
111        match self.iter.next() {
112            Some((k, v)) => Some((K::RuntimeType::as_ref(k), V::RuntimeType::as_ref(v))),
113            None => None,
114        }
115    }
116
117    fn _key_type(&self) -> RuntimeType {
118        K::RuntimeType::runtime_type_box()
119    }
120
121    fn _value_type(&self) -> RuntimeType {
122        V::RuntimeType::runtime_type_box()
123    }
124}