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 {self:#?} should have just one matching element"
79 /// Returns `true` if the map contains a value for the specified key.
81 /// The key may be any borrowed form of the map's key type,
82 /// [`Eq`] on the borrowed form *must* match those for
84 pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
92 /// Returns `true` if the map contains no elements.
93 pub fn is_empty(&self) -> bool {
97 pub fn iter(&self) -> Iter<'_, (K, V)> {
101 pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)> {
105 pub fn retain(&mut self, f: impl Fn(&(K, V)) -> bool) {
110 impl<K, V> Default for VecMap<K, V> {
112 fn default() -> Self {
113 Self(Default::default())
117 impl<K, V> From<Vec<(K, V)>> for VecMap<K, V> {
118 fn from(vec: Vec<(K, V)>) -> Self {
123 impl<K, V> Into<Vec<(K, V)>> for VecMap<K, V> {
124 fn into(self) -> Vec<(K, V)> {
129 impl<K, V> FromIterator<(K, V)> for VecMap<K, V> {
130 fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
131 Self(iter.into_iter().collect())
135 impl<'a, K, V> IntoIterator for &'a VecMap<K, V> {
136 type Item = &'a (K, V);
137 type IntoIter = Iter<'a, (K, V)>;
140 fn into_iter(self) -> Self::IntoIter {
145 impl<'a, K: 'a, V: 'a> IntoIterator for &'a mut VecMap<K, V> {
146 type Item = (&'a K, &'a mut V);
147 type IntoIter = impl Iterator<Item = Self::Item>;
150 fn into_iter(self) -> Self::IntoIter {
151 self.0.iter_mut().map(|(k, v)| (&*k, v))
155 impl<K, V> IntoIterator for VecMap<K, V> {
157 type IntoIter = IntoIter<(K, V)>;
160 fn into_iter(self) -> Self::IntoIter {
165 impl<K: PartialEq + Debug, V: Debug> Extend<(K, V)> for VecMap<K, V> {
166 fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
172 fn extend_one(&mut self, (k, v): (K, V)) {
176 fn extend_reserve(&mut self, additional: usize) {
177 self.0.extend_reserve(additional);
181 impl<K, V, CTX> HashStable<CTX> for VecMap<K, V>
183 K: HashStable<CTX> + Eq,
186 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
187 self.0.hash_stable(hcx, hasher)