Skip to content

Commit 8d05f6c

Browse files
committed
Rollup merge of rust-lang#21388 - aochagavia:collections, r=Gankro
**Breaking change**: `VecMap::into_iter` now consumes the `VecMap`. To fix it you should pass the VecMap by value instead of by reference. [breaking-change] r? @gankro
2 parents 3d6568f + b4090aa commit 8d05f6c

File tree

1 file changed

+71
-11
lines changed

1 file changed

+71
-11
lines changed

src/libcollections/vec_map.rs

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<V> VecMap<V> {
186186
}
187187
}
188188

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.
190190
/// The iterator's element type is `uint`.
191191
#[stable]
192192
pub fn keys<'r>(&'r self) -> Keys<'r, V> {
@@ -196,7 +196,7 @@ impl<V> VecMap<V> {
196196
Keys { iter: self.iter().map(first) }
197197
}
198198

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.
200200
/// The iterator's element type is `&'r V`.
201201
#[stable]
202202
pub fn values<'r>(&'r self) -> Values<'r, V> {
@@ -206,7 +206,7 @@ impl<V> VecMap<V> {
206206
Values { iter: self.iter().map(second) }
207207
}
208208

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.
210210
/// The iterator's element type is `(uint, &'r V)`.
211211
///
212212
/// # Examples
@@ -233,7 +233,7 @@ impl<V> VecMap<V> {
233233
}
234234
}
235235

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,
237237
/// with mutable references to the values.
238238
/// The iterator's element type is `(uint, &'r mut V)`.
239239
///
@@ -264,8 +264,8 @@ impl<V> VecMap<V> {
264264
}
265265
}
266266

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`.
269269
/// The iterator's element type is `(uint, &'r V)`.
270270
///
271271
/// # Examples
@@ -278,20 +278,46 @@ impl<V> VecMap<V> {
278278
/// map.insert(3, "c");
279279
/// map.insert(2, "b");
280280
///
281-
/// // Not possible with .iter()
282281
/// let vec: Vec<(uint, &str)> = map.into_iter().collect();
283282
///
284283
/// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
285284
/// ```
286285
#[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> {
288315
fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
289316
v.map(|v| (i, v))
290317
}
291318
let filter: fn((uint, Option<V>)) -> Option<(uint, V)> = filter; // coerce to fn ptr
292319

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) }
295321
}
296322

297323
/// Return the number of elements in the map.
@@ -673,6 +699,28 @@ pub struct IntoIter<V> {
673699
fn((uint, Option<V>)) -> Option<(uint, V)>>
674700
}
675701

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+
676724
#[stable]
677725
impl<'a, V> Iterator for Keys<'a, V> {
678726
type Item = uint;
@@ -918,7 +966,19 @@ mod test_map {
918966
assert_eq!(v, box 2i);
919967
}
920968
assert!(called);
921-
m.insert(2, box 1i);
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);
922982
}
923983

924984
#[test]

0 commit comments

Comments
 (0)