@@ -18,13 +18,25 @@ pub struct ValueIterator<'a, V> {
1818 next : Option < & ' a Record < V > > ,
1919}
2020
21+ // An iterator over all values associated with a translated key, allowing for mutation.
22+ pub struct MutableValueIterator < ' a , V > {
23+ next : Option < & ' a mut Record < V > > ,
24+ }
25+
2126impl < V > ValueIterator < ' _ , V > {
2227 /// Create a `ValueIterator` that returns no items.
2328 fn empty ( ) -> Self {
2429 ValueIterator { next : None }
2530 }
2631}
2732
33+ impl < V > MutableValueIterator < ' _ , V > {
34+ /// Create a `MutableValueIterator` that returns no items.
35+ fn empty ( ) -> Self {
36+ MutableValueIterator { next : None }
37+ }
38+ }
39+
2840impl < ' a , V > Iterator for ValueIterator < ' a , V > {
2941 type Item = & ' a V ;
3042
@@ -40,10 +52,29 @@ impl<'a, V> Iterator for ValueIterator<'a, V> {
4052 }
4153}
4254
55+ impl < ' a , V > Iterator for MutableValueIterator < ' a , V > {
56+ type Item = & ' a mut V ;
57+
58+ fn next ( & mut self ) -> Option < Self :: Item > {
59+ match self . next . take ( ) {
60+ Some ( next) => {
61+ let loc = & mut next. value ;
62+ self . next = next. next . as_deref_mut ( ) ;
63+ Some ( loc)
64+ }
65+ None => None ,
66+ }
67+ }
68+ }
69+
4370impl < V > Record < V > {
4471 fn iter ( & self ) -> ValueIterator < V > {
4572 ValueIterator { next : Some ( self ) }
4673 }
74+
75+ fn iter_mut ( & mut self ) -> MutableValueIterator < V > {
76+ MutableValueIterator { next : Some ( self ) }
77+ }
4778}
4879
4980/// An index that maps translated keys to values.
@@ -113,6 +144,15 @@ impl<T: Translator, V> Index<T, V> {
113144 }
114145 }
115146
147+ /// Retrieve all values associated with a translated key, allowing for mutation.
148+ pub fn get_mut ( & mut self , key : & [ u8 ] ) -> MutableValueIterator < V > {
149+ let translated_key = self . translator . transform ( key) ;
150+ match self . map . get_mut ( & translated_key) {
151+ Some ( head) => head. iter_mut ( ) ,
152+ None => MutableValueIterator :: empty ( ) ,
153+ }
154+ }
155+
116156 /// Remove values associated with the key that match the `prune` predicate.
117157 ///
118158 /// If this function is never called, the amount of memory used by old values will grow
0 commit comments