@@ -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,7 +264,7 @@ impl<V> VecMap<V> {
264
264
}
265
265
}
266
266
267
- /// Returns an iterator visiting all key-value pairs in ascending order by
267
+ /// Returns an iterator visiting all key-value pairs in ascending order of
268
268
/// the keys, consuming the original `VecMap`.
269
269
/// The iterator's element type is `(uint, &'r V)`.
270
270
///
@@ -278,7 +278,6 @@ 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")]);
@@ -293,6 +292,34 @@ impl<V> VecMap<V> {
293
292
IntoIter { iter : self . v . into_iter ( ) . enumerate ( ) . filter_map ( filter) }
294
293
}
295
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 > {
315
+ fn filter < A > ( ( i, v) : ( uint , Option < A > ) ) -> Option < ( uint , A ) > {
316
+ v. map ( |v| ( i, v) )
317
+ }
318
+ let filter: fn ( ( uint , Option < V > ) ) -> Option < ( uint , V ) > = filter; // coerce to fn ptr
319
+
320
+ Drain { iter : self . v . drain ( ) . enumerate ( ) . filter_map ( filter) }
321
+ }
322
+
296
323
/// Return the number of elements in the map.
297
324
///
298
325
/// # Examples
@@ -672,6 +699,28 @@ pub struct IntoIter<V> {
672
699
fn ( ( uint , Option < V > ) ) -> Option < ( uint , V ) > >
673
700
}
674
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
+
675
724
#[ stable]
676
725
impl < ' a , V > Iterator for Keys < ' a , V > {
677
726
type Item = uint ;
0 commit comments