Skip to main content

VecMap

Struct VecMap 

Source
pub struct VecMap<K, V> { /* private fields */ }
Expand description

A Vec-backed map that provides HashMap-like lookup by key.

§Duplicates

Duplicates are tolerated: VecMap::insert always appends, and VecMap::get/VecMap::get_mut return the last matching entry so that later writes shadow earlier ones. This optimizes for fast insertion and construction (that might happen on the client’s application hot path), avoiding a linear scan on each insert, or a potential full re-hashing with a hashmap. Additionally, while overriding a metric or a meta definitively happens, it’s assumed to be rare enough so such that the size penalty of duplication is expected to be reasonable.

Important: note that only VecMap::get and VecMap::get_mut are duplicate-aware, so to speak. VecMap::len, VecMap::iter, and others just delegates to the underlying Vec, and won’t deduplicate.

Explicit deduplication is currently being done on-demand by VecMap::dedup. An internal flag is used to avoid undue deduplication (see VecMap::dedup). VecMap is automatically deduped before serialization.

In the future, we could trigger deduplication on other events, for example at insertion if the size is bigger than a threshold (and we haven’t deduped for x operations).

§Ordering

As this is a map, iteration order is not defined nor guaranteed. In practice, iteration follows insertion order, but Self::dedup will reverse the underlying vector.

Implementations§

Source§

impl<K, V> VecMap<K, V>

Source

pub fn new() -> Self

Source

pub fn with_capacity(capacity: usize) -> Self

Source

pub fn insert(&mut self, key: K, value: V)

Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: ?Sized + PartialEq,

Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: ?Sized + PartialEq,

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: ?Sized + PartialEq,

Source

pub fn slow_compare(&self, other: &Self) -> bool
where K: Eq + Hash, V: PartialEq,

Compares two maps for equality, ignoring insertion order and duplicate entries (the last value for a given key wins on both sides). This allocates two intermediate HashMaps, so it’s exposed as a named method rather than PartialEq/Eq, to keep that cost visible at the call site instead of hiding it behind ==.

Source

pub fn remove_slow<Q>(&mut self, key: &Q)
where K: Borrow<Q>, Q: ?Sized + PartialEq,

Remove all entries matching this key from the map. This method uses Vec::retain, and is thus potentially costly (like any removal in a vector-like datastructure).

Source

pub fn iter(&self) -> Iter<'_, (K, V)>

Iterate over the element, including duplicate entries.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, (K, V)>

Iterate mutably over the elements, including duplicate entries.

Source

pub fn len(&self) -> usize

Return the length of the underlying vector, thus including duplicate entries.

Source

pub fn is_empty(&self) -> bool

Source

pub fn is_deduped(&self) -> bool

Return true if the map hasn’t been extended since the last call to Self::dedup, guaranteeing that the underlying vector doesn’t have any duplicate key.

If is_deduped returns false, the map may have duplicate keys.

Source

pub fn mark_deduped(&mut self)

Assert, without scanning, that this map holds no duplicate keys, setting the deduped flag.

For builders whose source guarantees key uniqueness (e.g. msgpack decoding, where the wire format is a map), to skip the Self::dedup pass. A later mutation re-dirties the flag.

Caution: if the source can actually contain duplicate keys, prefer Self::dedup.

Source

pub fn clear(&mut self)

Source

pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> Drain<'_, (K, V)>

Source§

impl<K: Eq + Hash, V> VecMap<K, V>

Source

pub fn as_deduped_map(&self) -> DedupedVecMap<'_, K, V>

Returns a deduped map, that either borrows from self without performing any work if the map is already deduped, or dedup the entries in a new separate vec otherwise. As opposed to Self::dedup, as_deduped_map takes an immutable reference to self but might allocate. Prefer Self::dedup when applicable.

Source

pub fn defensive_dedup(&self) -> DedupedVecMap<'_, K, V>

This is a convenience wrapper around Self::as_deduped_map used in the msgpack encoder, where we expect the map to be deduped, but call as_deduped_map as a defensive measure. If the latter had to deduplicate and allocate a new vec, we log a warning (at most once).

Source

pub fn dedup(&mut self)

Remove entries with a duplicate key, only keeping the last one. After this, a flag is set internally, such that as long as the map isn’t extended or mutably iterated, the next Self::dedup doesn’t perform the work again.

Trait Implementations§

Source§

impl<K: Clone, V: Clone> Clone for VecMap<K, V>

Source§

fn clone(&self) -> VecMap<K, V>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug> Debug for VecMap<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V> Default for VecMap<K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K, V> Extend<(K, V)> for VecMap<K, V>

Source§

fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V> From<Vec<(K, V)>> for VecMap<K, V>

Source§

fn from(data: Vec<(K, V)>) -> Self

Converts to this type from the input type.
Source§

impl<K, V> From<VecMap<K, V>> for Vec<(K, V)>

Source§

fn from(value: VecMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<K, V> FromIterator<(K, V)> for VecMap<K, V>

Source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<K, V> IntoIterator for VecMap<K, V>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<(K, V)>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V> IntoIterator for &'a VecMap<K, V>

Source§

type Item = &'a (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, (K, V)>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V> IntoIterator for &'a mut VecMap<K, V>

Source§

type Item = &'a mut (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, (K, V)>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: Serialize + Eq + Hash, V: Serialize> Serialize for VecMap<K, V>

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for VecMap<K, V>
where Vec<(K, V)>: Freeze,

§

impl<K, V> RefUnwindSafe for VecMap<K, V>

§

impl<K, V> Send for VecMap<K, V>
where Vec<(K, V)>: Send,

§

impl<K, V> Sync for VecMap<K, V>
where Vec<(K, V)>: Sync,

§

impl<K, V> Unpin for VecMap<K, V>
where Vec<(K, V)>: Unpin,

§

impl<K, V> UnsafeUnpin for VecMap<K, V>

§

impl<K, V> UnwindSafe for VecMap<K, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSend for T
where T: Send,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more