Skip to content

Commit b712ab0

Browse files
committed
---
yaml --- r: 174521 b: refs/heads/auto c: c3ac929 h: refs/heads/master i: 174519: ee556c7 v: v3
1 parent 71218fa commit b712ab0

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1010
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1111
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1212
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
13-
refs/heads/auto: 2366dee8e9cb41d963900c8d5128f810a87fc6bb
13+
refs/heads/auto: c3ac929ba966666fbe9c72eb97e38d5e37ad11e2
1414
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1515
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1616
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/libcollections/vec_map.rs

Lines changed: 55 additions & 6 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,7 +264,7 @@ impl<V> VecMap<V> {
264264
}
265265
}
266266

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
268268
/// the keys, consuming the original `VecMap`.
269269
/// The iterator's element type is `(uint, &'r V)`.
270270
///
@@ -278,7 +278,6 @@ 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")]);
@@ -293,6 +292,34 @@ impl<V> VecMap<V> {
293292
IntoIter { iter: self.v.into_iter().enumerate().filter_map(filter) }
294293
}
295294

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+
296323
/// Return the number of elements in the map.
297324
///
298325
/// # Examples
@@ -672,6 +699,28 @@ pub struct IntoIter<V> {
672699
fn((uint, Option<V>)) -> Option<(uint, V)>>
673700
}
674701

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+
675724
#[stable]
676725
impl<'a, V> Iterator for Keys<'a, V> {
677726
type Item = uint;

0 commit comments

Comments
 (0)