1 use std::borrow::Borrow;
3 use std::iter::FromIterator;
5 use std::vec::IntoIter;
7 use crate::stable_hasher::{HashStable, StableHasher};
9 /// A map type implemented as a vector of pairs `K` (key) and `V` (value).
10 /// It currently provides a subset of all the map operations, the rest could be added as needed.
11 #[derive(Clone, Encodable, Decodable, Debug)]
12 pub struct VecMap<K, V>(Vec<(K, V)>);
14 impl<K, V> VecMap<K, V>
19 pub fn new() -> Self {
20 VecMap(Default::default())
23 /// Sets the value of the entry, and returns the entry's old value.
24 pub fn insert(&mut self, k: K, v: V) -> Option<V> {
25 if let Some(elem) = self.0.iter_mut().find(|(key, _)| *key == k) {
26 Some(std::mem::replace(&mut elem.1, v))
33 /// Gets a reference to the value in the entry.
34 pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
39 self.0.iter().find(|(key, _)| k == key.borrow()).map(|elem| &elem.1)
42 /// Returns the any value corresponding to the supplied predicate filter.
44 /// The supplied predicate will be applied to each (key, value) pair and it will return a
45 /// reference to the values where the predicate returns `true`.
46 pub fn any_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
47 self.0.iter().find(|kv| predicate(kv)).map(|elem| &elem.1)
50 /// Returns the value corresponding to the supplied predicate filter. It crashes if there's
51 /// more than one matching element.
53 /// The supplied predicate will be applied to each (key, value) pair and it will return a
54 /// reference to the value where the predicate returns `true`.
55 pub fn get_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
56 let mut filter = self.0.iter().filter(|kv| predicate(kv));
57 let (_, value) = filter.next()?;
58 // This should return just one element, otherwise it's a bug
60 filter.next().is_none(),
61 "Collection {:?} should have just one matching element",
67 /// Returns `true` if the map contains a value for the specified key.
69 /// The key may be any borrowed form of the map's key type,
70 /// [`Eq`] on the borrowed form *must* match those for
72 pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
80 /// Returns `true` if the map contains no elements.
81 pub fn is_empty(&self) -> bool {
85 pub fn iter(&self) -> Iter<'_, (K, V)> {
89 pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)> {
93 pub fn retain(&mut self, f: impl Fn(&(K, V)) -> bool) {
98 impl<K, V> Default for VecMap<K, V> {
100 fn default() -> Self {
101 Self(Default::default())
105 impl<K, V> From<Vec<(K, V)>> for VecMap<K, V> {
106 fn from(vec: Vec<(K, V)>) -> Self {
111 impl<K, V> Into<Vec<(K, V)>> for VecMap<K, V> {
112 fn into(self) -> Vec<(K, V)> {
117 impl<K, V> FromIterator<(K, V)> for VecMap<K, V> {
118 fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
119 Self(iter.into_iter().collect())
123 impl<'a, K, V> IntoIterator for &'a VecMap<K, V> {
124 type Item = &'a (K, V);
125 type IntoIter = Iter<'a, (K, V)>;
128 fn into_iter(self) -> Self::IntoIter {
133 impl<'a, K, V> IntoIterator for &'a mut VecMap<K, V> {
134 type Item = (&'a K, &'a mut V);
135 type IntoIter = impl Iterator<Item = Self::Item>;
138 fn into_iter(self) -> Self::IntoIter {
139 self.0.iter_mut().map(|(k, v)| (&*k, v))
143 impl<K, V> IntoIterator for VecMap<K, V> {
145 type IntoIter = IntoIter<(K, V)>;
148 fn into_iter(self) -> Self::IntoIter {
153 impl<K: PartialEq + Debug, V: Debug> Extend<(K, V)> for VecMap<K, V> {
154 fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
160 fn extend_one(&mut self, (k, v): (K, V)) {
164 fn extend_reserve(&mut self, additional: usize) {
165 self.0.extend_reserve(additional);
169 impl<K, V, CTX> HashStable<CTX> for VecMap<K, V>
171 K: HashStable<CTX> + Eq,
174 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
175 self.0.hash_stable(hcx, hasher)