Skip to content

Commit 7b1e1c7

Browse files
committed
add VecMap docs
1 parent cad762b commit 7b1e1c7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

compiler/rustc_data_structures/src/vec_map.rs

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::vec::IntoIter;
55

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

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

@@ -16,6 +18,7 @@ where
1618
VecMap(Default::default())
1719
}
1820

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

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

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

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

61+
/// Returns `true` if the map contains no elements.
4862
pub fn is_empty(&self) -> bool {
4963
self.0.is_empty()
5064
}

0 commit comments

Comments
 (0)