Skip to content

Commit

Permalink
add VecMap docs
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Jun 8, 2021
1 parent cad762b commit 7b1e1c7
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions compiler/rustc_data_structures/src/vec_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::vec::IntoIter;

use crate::stable_hasher::{HashStable, StableHasher};

/// A map type implemented as a vector of pairs `K` (key) and `V` (value).
/// It currently provides a subset of all the map operations, the rest could be added as needed.
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct VecMap<K, V>(Vec<(K, V)>);

Expand All @@ -16,6 +18,7 @@ where
VecMap(Default::default())
}

/// Sets the value of the entry, and returns the entry's old value.
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
if let Some(elem) = self.0.iter_mut().find(|(key, _)| *key == k) {
Some(std::mem::replace(&mut elem.1, v))
Expand All @@ -25,6 +28,7 @@ where
}
}

/// Gets a reference to the value in the entry.
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
where
K: Borrow<Q>,
Expand All @@ -33,10 +37,19 @@ where
self.0.iter().find(|(key, _)| k == key.borrow()).map(|elem| &elem.1)
}

/// Returns the value corresponding to the supplied predicate filter.
///
/// The supplied predicate will be applied to each (key, value) pair and it will return a
/// reference to the values where the predicate returns `true`.
pub fn get_by(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
self.0.iter().find(|kv| predicate(kv)).map(|elem| &elem.1)
}

/// Returns `true` if the map contains a value for the specified key.
///
/// The key may be any borrowed form of the map's key type,
/// [`Eq`] on the borrowed form *must* match those for
/// the key type.
pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
where
K: Borrow<Q>,
Expand All @@ -45,6 +58,7 @@ where
self.get(k).is_some()
}

/// Returns `true` if the map contains no elements.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
Expand Down

0 comments on commit 7b1e1c7

Please sign in to comment.