Skip to content

Commit 41b58ee

Browse files
committed
Move generated map support code into a separate file
1 parent ea9da7a commit 41b58ee

File tree

2 files changed

+78
-72
lines changed

2 files changed

+78
-72
lines changed

protobuf/src/reflect/map/generated.rs

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

protobuf/src/reflect/map.rs renamed to protobuf/src/reflect/map/mod.rs

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
use std::collections::hash_map;
2-
use std::collections::HashMap;
3-
use std::hash::Hash;
4-
51
use crate::reflect::reflect_eq::ReflectEq;
62
use crate::reflect::reflect_eq::ReflectEqMode;
7-
use crate::reflect::runtime_types::RuntimeTypeHashable;
8-
use crate::reflect::ProtobufValue;
93
use crate::reflect::ReflectValueBox;
104
use crate::reflect::ReflectValueRef;
115
use crate::reflect::RuntimeTypeBox;
126

7+
mod generated;
8+
139
/// Implemented for `HashMap` with appropriate keys and values
1410
pub(crate) trait ReflectMap: Send + Sync + 'static {
1511
fn reflect_iter(&self) -> ReflectMapIter;
@@ -29,78 +25,12 @@ pub(crate) trait ReflectMap: Send + Sync + 'static {
2925
fn value_type(&self) -> RuntimeTypeBox;
3026
}
3127

32-
impl<K, V> ReflectMap for HashMap<K, V>
33-
where
34-
K: ProtobufValue + Eq + Hash,
35-
V: ProtobufValue,
36-
K::RuntimeType: RuntimeTypeHashable,
37-
{
38-
fn reflect_iter<'a>(&'a self) -> ReflectMapIter<'a> {
39-
ReflectMapIter {
40-
imp: Box::new(ReflectMapIterImpl::<'a, K, V> { iter: self.iter() }),
41-
}
42-
}
43-
44-
fn len(&self) -> usize {
45-
HashMap::len(self)
46-
}
47-
48-
fn is_empty(&self) -> bool {
49-
self.is_empty()
50-
}
51-
52-
fn get<'a>(&'a self, key: ReflectValueRef) -> Option<ReflectValueRef<'a>> {
53-
<K::RuntimeType as RuntimeTypeHashable>::hash_map_get(self, key).map(V::as_ref)
54-
}
55-
56-
fn insert(&mut self, key: ReflectValueBox, value: ReflectValueBox) {
57-
let key: K = key.downcast().expect("wrong key type");
58-
let value: V = value.downcast().expect("wrong value type");
59-
self.insert(key, value);
60-
}
61-
62-
fn clear(&mut self) {
63-
self.clear();
64-
}
65-
66-
fn key_type(&self) -> RuntimeTypeBox {
67-
K::runtime_type_box()
68-
}
69-
70-
fn value_type(&self) -> RuntimeTypeBox {
71-
V::runtime_type_box()
72-
}
73-
}
74-
7528
trait ReflectMapIterTrait<'a> {
7629
fn next(&mut self) -> Option<(ReflectValueRef<'a>, ReflectValueRef<'a>)>;
7730
fn key_type(&self) -> RuntimeTypeBox;
7831
fn value_type(&self) -> RuntimeTypeBox;
7932
}
8033

81-
struct ReflectMapIterImpl<'a, K: Eq + Hash + 'static, V: 'static> {
82-
iter: hash_map::Iter<'a, K, V>,
83-
}
84-
85-
impl<'a, K: ProtobufValue + Eq + Hash, V: ProtobufValue> ReflectMapIterTrait<'a>
86-
for ReflectMapIterImpl<'a, K, V>
87-
{
88-
fn next(&mut self) -> Option<(ReflectValueRef<'a>, ReflectValueRef<'a>)> {
89-
match self.iter.next() {
90-
Some((k, v)) => Some((K::as_ref(k), V::as_ref(v))),
91-
None => None,
92-
}
93-
}
94-
95-
fn key_type(&self) -> RuntimeTypeBox {
96-
K::runtime_type_box()
97-
}
98-
99-
fn value_type(&self) -> RuntimeTypeBox {
100-
V::runtime_type_box()
101-
}
102-
}
103-
10434
pub struct ReflectMapIter<'a> {
10535
imp: Box<dyn ReflectMapIterTrait<'a> + 'a>,
10636
}

0 commit comments

Comments
 (0)