@@ -5,6 +5,8 @@ use std::vec::IntoIter;
5
5
6
6
use crate :: stable_hasher:: { HashStable , StableHasher } ;
7
7
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.
8
10
#[ derive( Clone , Encodable , Decodable , Debug ) ]
9
11
pub struct VecMap < K , V > ( Vec < ( K , V ) > ) ;
10
12
16
18
VecMap ( Default :: default ( ) )
17
19
}
18
20
21
+ /// Sets the value of the entry, and returns the entry's old value.
19
22
pub fn insert ( & mut self , k : K , v : V ) -> Option < V > {
20
23
if let Some ( elem) = self . 0 . iter_mut ( ) . find ( |( key, _) | * key == k) {
21
24
Some ( std:: mem:: replace ( & mut elem. 1 , v) )
25
28
}
26
29
}
27
30
31
+ /// Gets a reference to the value in the entry.
28
32
pub fn get < Q : ?Sized > ( & self , k : & Q ) -> Option < & V >
29
33
where
30
34
K : Borrow < Q > ,
@@ -33,10 +37,19 @@ where
33
37
self . 0 . iter ( ) . find ( |( key, _) | k == key. borrow ( ) ) . map ( |elem| & elem. 1 )
34
38
}
35
39
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`.
36
44
pub fn get_by ( & self , mut predicate : impl FnMut ( & ( K , V ) ) -> bool ) -> Option < & V > {
37
45
self . 0 . iter ( ) . find ( |kv| predicate ( kv) ) . map ( |elem| & elem. 1 )
38
46
}
39
47
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.
40
53
pub fn contains_key < Q : ?Sized > ( & self , k : & Q ) -> bool
41
54
where
42
55
K : Borrow < Q > ,
45
58
self . get ( k) . is_some ( )
46
59
}
47
60
61
+ /// Returns `true` if the map contains no elements.
48
62
pub fn is_empty ( & self ) -> bool {
49
63
self . 0 . is_empty ( )
50
64
}
0 commit comments