@@ -186,7 +186,7 @@ impl<V> VecMap<V> {
186
186
}
187
187
}
188
188
189
- /// Returns an iterator visiting all keys in ascending order by the keys.
189
+ /// Returns an iterator visiting all keys in ascending order of the keys.
190
190
/// The iterator's element type is `uint`.
191
191
#[ stable]
192
192
pub fn keys < ' r > ( & ' r self ) -> Keys < ' r , V > {
@@ -196,7 +196,7 @@ impl<V> VecMap<V> {
196
196
Keys { iter : self . iter ( ) . map ( first) }
197
197
}
198
198
199
- /// Returns an iterator visiting all values in ascending order by the keys.
199
+ /// Returns an iterator visiting all values in ascending order of the keys.
200
200
/// The iterator's element type is `&'r V`.
201
201
#[ stable]
202
202
pub fn values < ' r > ( & ' r self ) -> Values < ' r , V > {
@@ -206,7 +206,7 @@ impl<V> VecMap<V> {
206
206
Values { iter : self . iter ( ) . map ( second) }
207
207
}
208
208
209
- /// Returns an iterator visiting all key-value pairs in ascending order by the keys.
209
+ /// Returns an iterator visiting all key-value pairs in ascending order of the keys.
210
210
/// The iterator's element type is `(uint, &'r V)`.
211
211
///
212
212
/// # Examples
@@ -233,7 +233,7 @@ impl<V> VecMap<V> {
233
233
}
234
234
}
235
235
236
- /// Returns an iterator visiting all key-value pairs in ascending order by the keys,
236
+ /// Returns an iterator visiting all key-value pairs in ascending order of the keys,
237
237
/// with mutable references to the values.
238
238
/// The iterator's element type is `(uint, &'r mut V)`.
239
239
///
@@ -264,8 +264,8 @@ impl<V> VecMap<V> {
264
264
}
265
265
}
266
266
267
- /// Returns an iterator visiting all key-value pairs in ascending order by
268
- /// the keys, emptying (but not consuming) the original `VecMap`.
267
+ /// Returns an iterator visiting all key-value pairs in ascending order of
268
+ /// the keys, consuming the original `VecMap`.
269
269
/// The iterator's element type is `(uint, &'r V)`.
270
270
///
271
271
/// # Examples
@@ -278,20 +278,46 @@ impl<V> VecMap<V> {
278
278
/// map.insert(3, "c");
279
279
/// map.insert(2, "b");
280
280
///
281
- /// // Not possible with .iter()
282
281
/// let vec: Vec<(uint, &str)> = map.into_iter().collect();
283
282
///
284
283
/// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
285
284
/// ```
286
285
#[ stable]
287
- pub fn into_iter ( & mut self ) -> IntoIter < V > {
286
+ pub fn into_iter ( self ) -> IntoIter < V > {
287
+ fn filter < A > ( ( i, v) : ( uint , Option < A > ) ) -> Option < ( uint , A ) > {
288
+ v. map ( |v| ( i, v) )
289
+ }
290
+ let filter: fn ( ( uint , Option < V > ) ) -> Option < ( uint , V ) > = filter; // coerce to fn ptr
291
+
292
+ IntoIter { iter : self . v . into_iter ( ) . enumerate ( ) . filter_map ( filter) }
293
+ }
294
+
295
+ /// Returns an iterator visiting all key-value pairs in ascending order of
296
+ /// the keys, emptying (but not consuming) the original `VecMap`.
297
+ /// The iterator's element type is `(uint, &'r V)`. Keeps the allocated memory for reuse.
298
+ ///
299
+ /// # Examples
300
+ ///
301
+ /// ```
302
+ /// use std::collections::VecMap;
303
+ ///
304
+ /// let mut map = VecMap::new();
305
+ /// map.insert(1, "a");
306
+ /// map.insert(3, "c");
307
+ /// map.insert(2, "b");
308
+ ///
309
+ /// let vec: Vec<(uint, &str)> = map.drain().collect();
310
+ ///
311
+ /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
312
+ /// ```
313
+ #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
314
+ pub fn drain < ' a > ( & ' a mut self ) -> Drain < ' a , V > {
288
315
fn filter < A > ( ( i, v) : ( uint , Option < A > ) ) -> Option < ( uint , A ) > {
289
316
v. map ( |v| ( i, v) )
290
317
}
291
318
let filter: fn ( ( uint , Option < V > ) ) -> Option < ( uint , V ) > = filter; // coerce to fn ptr
292
319
293
- let values = replace ( & mut self . v , vec ! ( ) ) ;
294
- IntoIter { iter : values. into_iter ( ) . enumerate ( ) . filter_map ( filter) }
320
+ Drain { iter : self . v . drain ( ) . enumerate ( ) . filter_map ( filter) }
295
321
}
296
322
297
323
/// Return the number of elements in the map.
@@ -673,6 +699,28 @@ pub struct IntoIter<V> {
673
699
fn ( ( uint , Option < V > ) ) -> Option < ( uint , V ) > >
674
700
}
675
701
702
+ #[ unstable]
703
+ pub struct Drain < ' a , V > {
704
+ iter : FilterMap <
705
+ ( uint , Option < V > ) ,
706
+ ( uint , V ) ,
707
+ Enumerate < vec:: Drain < ' a , Option < V > > > ,
708
+ fn ( ( uint , Option < V > ) ) -> Option < ( uint , V ) > >
709
+ }
710
+
711
+ #[ unstable]
712
+ impl < ' a , V > Iterator for Drain < ' a , V > {
713
+ type Item = ( uint , V ) ;
714
+
715
+ fn next ( & mut self ) -> Option < ( uint , V ) > { self . iter . next ( ) }
716
+ fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
717
+ }
718
+
719
+ #[ unstable]
720
+ impl < ' a , V > DoubleEndedIterator for Drain < ' a , V > {
721
+ fn next_back ( & mut self ) -> Option < ( uint , V ) > { self . iter . next_back ( ) }
722
+ }
723
+
676
724
#[ stable]
677
725
impl < ' a , V > Iterator for Keys < ' a , V > {
678
726
type Item = uint ;
@@ -918,7 +966,19 @@ mod test_map {
918
966
assert_eq ! ( v, box 2 i) ;
919
967
}
920
968
assert ! ( called) ;
921
- m. insert ( 2 , box 1 i) ;
969
+ }
970
+
971
+ #[ test]
972
+ fn test_drain_iterator ( ) {
973
+ let mut map = VecMap :: new ( ) ;
974
+ map. insert ( 1 , "a" ) ;
975
+ map. insert ( 3 , "c" ) ;
976
+ map. insert ( 2 , "b" ) ;
977
+
978
+ let vec: Vec < ( usize , & str ) > = map. drain ( ) . collect ( ) ;
979
+
980
+ assert_eq ! ( vec, vec![ ( 1 , "a" ) , ( 2 , "b" ) , ( 3 , "c" ) ] ) ;
981
+ assert_eq ! ( map. len( ) , 0 ) ;
922
982
}
923
983
924
984
#[ test]
0 commit comments