pub struct MapView<'a, K, V> { /* private fields */ }Expand description
A borrowed view of a map field.
Protobuf map<K, V> fields are encoded as repeated sub-messages, each
containing a key (field 1) and value (field 2). This type stores the
decoded entries in a Vec<(K, V)>, borrowing string and bytes keys/values
directly from the input buffer.
Lookup is O(n) linear scan, which is appropriate for the typically small
maps found in protobuf messages (metadata labels, headers, etc.).
If duplicate keys appear on the wire, get returns the
last occurrence (last-write-wins, per the protobuf spec). That rule is
about whole entries; within one entry, a repeated scalar key or value
also last-wins, while a repeated message value merges — the same as the
owned decoder.
For larger maps where O(1) lookup matters, collect into a HashMap:
use std::collections::HashMap;
let index: HashMap<&str, &str> = view.labels.into_iter().collect();Duplicate keys resolve last-write-wins in the collected map (matching
proto map semantics), since HashMap::from_iter keeps the last value.
§Allocation
Like RepeatedView, the Vec backing store requires allocation.
The individual keys and values borrow from the input buffer where possible
(string keys as &'a str, bytes values as &'a [u8]).
Implementations§
Source§impl<'a, K, V> MapView<'a, K, V>
impl<'a, K, V> MapView<'a, K, V>
Sourcepub fn new(entries: Vec<(K, V)>) -> Self
pub fn new(entries: Vec<(K, V)>) -> Self
Construct from a Vec of entries, for ViewEncode use.
Duplicate keys are kept and all encoded — valid protobuf wire data
(decoders apply last-write-wins). Mirrors RepeatedView::new.
Sourcepub fn iter(&self) -> Iter<'_, (K, V)> ⓘ
pub fn iter(&self) -> Iter<'_, (K, V)> ⓘ
Iterate over all entries in wire order.
If duplicate keys exist, all occurrences are yielded.
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Look up a value by key, returning the last occurrence (last-write-wins).
Accepts any type that K can borrow as, so map.get("key") works
when K is &str. O(n) scan.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Returns true if an entry with the given key exists.
Sourcepub fn iter_unique(&self) -> impl ExactSizeIterator<Item = &(K, V)>where
K: Ord,
pub fn iter_unique(&self) -> impl ExactSizeIterator<Item = &(K, V)>where
K: Ord,
Iterate over key-value pairs with duplicate keys removed.
Each distinct key is yielded exactly once, at the position of its
last wire occurrence, carrying that occurrence’s value — i.e.
last-write-wins, mirroring the merge semantics that an owned
HashMap decode applies. Callers that need first-occurrence position
should use iter and filter themselves.
Used by the generated view Serialize impl: a JSON object cannot
hold duplicate keys, but MapView preserves all wire entries
(including malformed duplicates), so the JSON encode path must
deduplicate.
The work happens when this is called, not lazily: it sorts an index
vector, O(n log n) over the wire-entry count regardless of how many
entries are duplicates, allocating one Vec<usize>. Survivors are
yielded in wire order. A map with at most one entry skips the sort.
§Panics
Only if K’s Ord is not a total order, in which case the
underlying sort_unstable_by may panic.
Sourcepub fn len_unique(&self) -> usizewhere
K: Ord,
pub fn len_unique(&self) -> usizewhere
K: Ord,
Count of distinct keys (iter_unique().len()). Same cost as
iter_unique; a caller that needs both should
call iter_unique once and take its len().
§Panics
As iter_unique.
Trait Implementations§
impl<'a, K: Eq, V: Eq> Eq for MapView<'a, K, V>
Source§impl<'a, K, V> FromIterator<(K, V)> for MapView<'a, K, V>
Duplicate keys are kept and all encoded; see MapView::new.
impl<'a, K, V> FromIterator<(K, V)> for MapView<'a, K, V>
Duplicate keys are kept and all encoded; see MapView::new.