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 /// Removes the entry from the map and returns the removed value
34 pub fn remove(&mut self, k: &K) -> Option<V> {
35 self.0.iter().position(|(k2, _)| k2 == k).map(|pos| self.0.remove(pos).1)
38 /// Gets a reference to the value in the entry.
39 pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
44 self.0.iter().find(|(key, _)| k == key.borrow()).map(|elem| &elem.1)
47 /// Gets a mutable reference to the value in the entry.
48 pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
53 self.0.iter_mut().find(|(key, _)| k == key.borrow()).map(|elem| &mut elem.1)
56 /// Returns the any value corresponding to the supplied predicate filter.
58 /// The supplied predicate will be applied to each (key, value) pair and it will return a
59 /// reference to the values where the predicate returns `true`.
60 pub fn any_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
61 self.0.iter().find(|kv| predicate(kv)).map(|elem| &elem.1)
64 /// Returns the value corresponding to the supplied predicate filter. It crashes if there's
65 /// more than one matching element.
67 /// The supplied predicate will be applied to each (key, value) pair and it will return a
68 /// reference to the value where the predicate returns `true`.
69 pub fn get_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
70 let mut filter = self.0.iter().filter(|kv| predicate(kv));
71 let (_, value) = filter.next()?;
72 // This should return just one element, otherwise it's a bug
74 filter.next().is_none(),
75 "Collection {:#?} should have just one matching element",
81 /// Returns `true` if the map contains a value for the specified key.
83 /// The key may be any borrowed form of the map's key type,
84 /// [`Eq`] on the borrowed form *must* match those for
86 pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
94 /// Returns `true` if the map contains no elements.
95 pub fn is_empty(&self) -> bool {
99 pub fn iter(&self) -> Iter<'_, (K, V)> {
103 pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)> {
107 pub fn retain(&mut self, f: impl Fn(&(K, V)) -> bool) {
112 impl<K, V> Default for VecMap<K, V> {
114 fn default() -> Self {
115 Self(Default::default())
119 impl<K, V> From<Vec<(K, V)>> for VecMap<K, V> {
120 fn from(vec: Vec<(K, V)>) -> Self {
125 impl<K, V> Into<Vec<(K, V)>> for VecMap<K, V> {
126 fn into(self) -> Vec<(K, V)> {
131 impl<K, V> FromIterator<(K, V)> for VecMap<K, V> {
132 fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
133 Self(iter.into_iter().collect())
137 impl<'a, K, V> IntoIterator for &'a VecMap<K, V> {
138 type Item = &'a (K, V);
139 type IntoIter = Iter<'a, (K, V)>;
142 fn into_iter(self) -> Self::IntoIter {
147 impl<'a, K: 'a, V: 'a> IntoIterator for &'a mut VecMap<K, V> {
148 type Item = (&'a K, &'a mut V);
149 type IntoIter = impl Iterator<Item = Self::Item>;
152 fn into_iter(self) -> Self::IntoIter {
153 self.0.iter_mut().map(|(k, v)| (&*k, v))
157 impl<K, V> IntoIterator for VecMap<K, V> {
159 type IntoIter = IntoIter<(K, V)>;
162 fn into_iter(self) -> Self::IntoIter {
167 impl<K: PartialEq + Debug, V: Debug> Extend<(K, V)> for VecMap<K, V> {
168 fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
174 fn extend_one(&mut self, (k, v): (K, V)) {
178 fn extend_reserve(&mut self, additional: usize) {
179 self.0.extend_reserve(additional);
183 impl<K, V, CTX> HashStable<CTX> for VecMap<K, V>
185 K: HashStable<CTX> + Eq,
188 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
189 self.0.hash_stable(hcx, hasher)