Skip to content

Commit

Permalink
auto merge of #16903 : mahkoh/rust/move_items_unwrap, r=aturon
Browse files Browse the repository at this point in the history
Closes #16879
  • Loading branch information
bors committed Sep 8, 2014
2 parents 5c39879 + d34992e commit 6f34760
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/libcollections/vec.rs
Expand Up @@ -1618,6 +1618,19 @@ pub struct MoveItems<T> {
iter: Items<'static, T>
}

impl<T> MoveItems<T> {
#[inline]
/// Drops all items that have not yet been moved and returns the empty vector.
pub fn unwrap(mut self) -> Vec<T> {
unsafe {
for _x in self { }
let MoveItems { allocation, cap, iter: _iter } = self;
mem::forget(self);
Vec { ptr: allocation, cap: cap, len: 0 }
}
}
}

impl<T> Iterator<T> for MoveItems<T> {
#[inline]
fn next<'a>(&'a mut self) -> Option<T> {
Expand Down Expand Up @@ -2016,6 +2029,18 @@ mod tests {
assert_eq!(vec.swap_remove(0), None);
}

#[test]
fn test_move_iter_unwrap() {
let mut vec: Vec<uint> = Vec::with_capacity(7);
vec.push(1);
vec.push(2);
let ptr = vec.as_ptr();
vec = vec.move_iter().unwrap();
assert_eq!(vec.as_ptr(), ptr);
assert_eq!(vec.capacity(), 7);
assert_eq!(vec.len(), 0);
}

#[bench]
fn bench_new(b: &mut Bencher) {
b.iter(|| {
Expand Down

0 comments on commit 6f34760

Please sign in to comment.