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).
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 Iterator<Item = &(K, V)>where
K: PartialEq,
pub fn iter_unique(&self) -> impl Iterator<Item = &(K, V)>where
K: PartialEq,
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 implementation is allocation-free and O(n²) — for
each entry, scan the remaining entries for a later occurrence of the
same key. Duplicate map keys are invalid per the protobuf encoding
spec and only arise in adversarial or conformance-test wire data, so
n is effectively always small.
Sourcepub fn len_unique(&self) -> usizewhere
K: PartialEq,
pub fn len_unique(&self) -> usizewhere
K: PartialEq,
Count of distinct keys (iter_unique().count()).
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.