1 use std::borrow::Borrow;
4 use std::vec::IntoIter;
6 use crate::stable_hasher::{HashStable, StableHasher};
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.
10 #[derive(Clone, Encodable, Decodable, Debug)]
11 pub struct VecMap<K, V>(Vec<(K, V)>);
13 impl<K, V> VecMap<K, V>
18 pub fn new() -> Self {
19 VecMap(Default::default())
22 /// Sets the value of the entry, and returns the entry's old value.
23 pub fn insert(&mut self, k: K, v: V) -> Option<V> {
24 if let Some(elem) = self.0.iter_mut().find(|(key, _)| *key == k) {
25 Some(std::mem::replace(&mut elem.1, v))
32 /// Removes the entry from the map and returns the removed value
33 pub fn remove(&mut self, k: &K) -> Option<V> {
34 self.0.iter().position(|(k2, _)| k2 == k).map(|pos| self.0.remove(pos).1)
37 /// Gets a reference to the value in the entry.
38 pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
43 self.0.iter().find(|(key, _)| k == key.borrow()).map(|elem| &elem.1)
46 /// Gets a mutable reference to the value in the entry.
47 pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
52 self.0.iter_mut().find(|(key, _)| k == key.borrow()).map(|elem| &mut elem.1)
55 /// Returns the any value corresponding to the supplied predicate filter.
57 /// The supplied predicate will be applied to each (key, value) pair and it will return a
58 /// reference to the values where the predicate returns `true`.
59 pub fn any_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
60 self.0.iter().find(|kv| predicate(kv)).map(|elem| &elem.1)
63 /// Returns the value corresponding to the supplied predicate filter. It crashes if there's
64 /// more than one matching element.
66 /// The supplied predicate will be applied to each (key, value) pair and it will return a
67 /// reference to the value where the predicate returns `true`.
68 pub fn get_value_matching(&self, mut predicate: impl FnMut(&(K, V)) -> bool) -> Option<&V> {
69 let mut filter = self.0.iter().filter(|kv| predicate(kv));
70 let (_, value) = filter.next()?;
71 // This should return just one element, otherwise it's a bug
73 filter.next().is_none(),
74 "Collection {:#?} should have just one matching element",
80 /// Returns `true` if the map contains a value for the specified key.
82 /// The key may be any borrowed form of the map's key type,
83 /// [`Eq`] on the borrowed form *must* match those for
85 pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
93 /// Returns `true` if the map contains no elements.
94 pub fn is_empty(&self) -> bool {
98 pub fn iter(&self) -> Iter<'_, (K, V)> {
102 pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)> {
106 pub fn retain(&mut self, f: impl Fn(&(K, V)) -> bool) {
111 impl<K, V> Default for VecMap<K, V> {
113 fn default() -> Self {
114 Self(Default::default())
118 impl<K, V> From<Vec<(K, V)>> for VecMap<K, V> {
119 fn from(vec: Vec<(K, V)>) -> Self {
124 impl<K, V> Into<Vec<(K, V)>> for VecMap<K, V> {
125 fn into(self) -> Vec<(K, V)> {
130 impl<K, V> FromIterator<(K, V)> for VecMap<K, V> {
131 fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
132 Self(iter.into_iter().collect())
136 impl<'a, K, V> IntoIterator for &'a VecMap<K, V> {
137 type Item = &'a (K, V);
138 type IntoIter = Iter<'a, (K, V)>;
141 fn into_iter(self) -> Self::IntoIter {
146 impl<'a, K: 'a, V: 'a> IntoIterator for &'a mut VecMap<K, V> {
147 type Item = (&'a K, &'a mut V);
148 type IntoIter = impl Iterator<Item = Self::Item>;
151 fn into_iter(self) -> Self::IntoIter {
152 self.0.iter_mut().map(|(k, v)| (&*k, v))
156 impl<K, V> IntoIterator for VecMap<K, V> {
158 type IntoIter = IntoIter<(K, V)>;
161 fn into_iter(self) -> Self::IntoIter {
166 impl<K: PartialEq + Debug, V: Debug> Extend<(K, V)> for VecMap<K, V> {
167 fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
173 fn extend_one(&mut self, (k, v): (K, V)) {
177 fn extend_reserve(&mut self, additional: usize) {
178 self.0.extend_reserve(additional);
182 impl<K, V, CTX> HashStable<CTX> for VecMap<K, V>
184 K: HashStable<CTX> + Eq,
187 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
188 self.0.hash_stable(hcx, hasher)