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.
Trait Implementations§
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.