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
/// Rust doesn't have a `Map` trait, so macros are currently the best way to be
/// generic over `HashMap` and `BTreeMap`.
macro_rules! map {
($map_ty:ident) => {
use core::hash::Hash;
use crate::encoding::*;
/// Generic protobuf map encode function.
pub fn encode<K, V, B, KE, KL, VE, VL>(
key_encode: KE,
key_encoded_len: KL,
val_encode: VE,
val_encoded_len: VL,
tag: u32,
values: &$map_ty<K, V>,
buf: &mut B,
) where
K: Default + Eq + Hash + Ord,
V: Default + PartialEq,
B: BufMut,
KE: Fn(u32, &K, &mut B),
KL: Fn(u32, &K) -> usize,
VE: Fn(u32, &V, &mut B),
VL: Fn(u32, &V) -> usize,
{
encode_with_default(
key_encode,
key_encoded_len,
val_encode,
val_encoded_len,
&V::default(),
tag,
values,
buf,
)
}
/// Generic protobuf map merge function.
pub fn merge<K, V, B, KM, VM>(
key_merge: KM,
val_merge: VM,
values: &mut $map_ty<K, V>,
buf: &mut B,
ctx: DecodeContext,
) -> Result<(), DecodeError>
where
K: Default + Eq + Hash + Ord,
V: Default,
B: Buf,
KM: Fn(WireType, &mut K, &mut B, DecodeContext) -> Result<(), DecodeError>,
VM: Fn(WireType, &mut V, &mut B, DecodeContext) -> Result<(), DecodeError>,
{
merge_with_default(key_merge, val_merge, V::default(), values, buf, ctx)
}
/// Generic protobuf map encode function.
pub fn encoded_len<K, V, KL, VL>(key_encoded_len: KL, val_encoded_len: VL, tag: u32, values: &$map_ty<K, V>) -> usize
where
K: Default + Eq + Hash + Ord,
V: Default + PartialEq,
KL: Fn(u32, &K) -> usize,
VL: Fn(u32, &V) -> usize,
{
encoded_len_with_default(key_encoded_len, val_encoded_len, &V::default(), tag, values)
}
/// Generic protobuf map encode function with an overridden value default.
///
/// This is necessary because enumeration values can have a default value other
/// than 0 in proto2.
#[allow(clippy::too_many_arguments)]
pub fn encode_with_default<K, V, B, KE, KL, VE, VL>(
key_encode: KE,
key_encoded_len: KL,
val_encode: VE,
val_encoded_len: VL,
val_default: &V,
tag: u32,
values: &$map_ty<K, V>,
buf: &mut B,
) where
K: Default + Eq + Hash + Ord,
V: PartialEq,
B: BufMut,
KE: Fn(u32, &K, &mut B),
KL: Fn(u32, &K) -> usize,
VE: Fn(u32, &V, &mut B),
VL: Fn(u32, &V) -> usize,
{
for (key, val) in values.iter() {
let skip_key = key == &K::default();
let skip_val = val == val_default;
let len = (if skip_key { 0 } else { key_encoded_len(1, key) }) + (if skip_val { 0 } else { val_encoded_len(2, val) });
encode_key(tag, WireType::LengthDelimited, buf);
encode_varint(len as u64, buf);
if !skip_key {
key_encode(1, key, buf);
}
if !skip_val {
val_encode(2, val, buf);
}
}
}
/// Generic protobuf map merge function with an overridden value default.
///
/// This is necessary because enumeration values can have a default value other
/// than 0 in proto2.
#[inline]
pub fn merge_with_default<K, V, B, KM, VM>(
key_merge: KM,
val_merge: VM,
val_default: V,
values: &mut $map_ty<K, V>,
buf: &mut B,
ctx: DecodeContext,
) -> Result<(), DecodeError>
where
K: Default + Eq + Hash + Ord,
B: Buf,
KM: Fn(WireType, &mut K, &mut B, DecodeContext) -> Result<(), DecodeError>,
VM: Fn(WireType, &mut V, &mut B, DecodeContext) -> Result<(), DecodeError>,
{
let mut key = K::default();
let mut val = val_default;
// Check recursion limit once at map entry boundary
ctx.limit_reached()?;
// Inline the merge_loop to avoid closure overhead
let len = decode_varint(buf)?;
let remaining = buf.remaining();
if len > remaining as u64 {
return Err(DecodeError::new("buffer underflow"));
}
let limit = remaining - len as usize;
// Don't enter_recursion() for map internals - the key/value are not nested messages
// from a recursion safety perspective (map entry is a single-level wrapper)
while buf.remaining() > limit {
let (tag, wire_type) = decode_key(buf)?;
match tag {
1 => key_merge(wire_type, &mut key, buf, ctx)?,
2 => val_merge(wire_type, &mut val, buf, ctx)?,
_ => skip_field(wire_type, tag, buf, ctx)?,
}
}
if buf.remaining() != limit {
return Err(DecodeError::new("delimited length exceeded"));
}
values.insert(key, val);
Ok(())
}
/// Generic protobuf map encode function with an overridden value default.
///
/// This is necessary because enumeration values can have a default value other
/// than 0 in proto2.
pub fn encoded_len_with_default<K, V, KL, VL>(
key_encoded_len: KL,
val_encoded_len: VL,
val_default: &V,
tag: u32,
values: &$map_ty<K, V>,
) -> usize
where
K: Default + Eq + Hash + Ord,
V: PartialEq,
KL: Fn(u32, &K) -> usize,
VL: Fn(u32, &V) -> usize,
{
key_len(tag) * values.len()
+ values
.iter()
.map(|(key, val)| {
let len = (if key == &K::default() { 0 } else { key_encoded_len(1, key) })
+ (if val == val_default { 0 } else { val_encoded_len(2, val) });
encoded_len_varint(len as u64) + len
})
.sum::<usize>()
}
};
}
#[cfg(feature = "std_legacy")]
pub mod hash_map {
use std::collections::HashMap;
map!(HashMap);
}
pub mod btree_map {
map!(BTreeMap);
}