Skip to content

Commit

Permalink
Add unwrap method to MoveItems
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Orth committed Aug 31, 2014
1 parent db47aa5 commit d34992e
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

9 comments on commit d34992e

@bors
Copy link
Contributor

@bors bors commented on d34992e Sep 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from aturon
at mahkoh@d34992e

@bors
Copy link
Contributor

@bors bors commented on d34992e Sep 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging mahkoh/rust/move_items_unwrap = d34992e into auto

@bors
Copy link
Contributor

@bors bors commented on d34992e Sep 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mahkoh/rust/move_items_unwrap = d34992e merged ok, testing candidate = 079626b4

@bors
Copy link
Contributor

@bors bors commented on d34992e Sep 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from aturon
at mahkoh@d34992e

@bors
Copy link
Contributor

@bors bors commented on d34992e Sep 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging mahkoh/rust/move_items_unwrap = d34992e into auto

@bors
Copy link
Contributor

@bors bors commented on d34992e Sep 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mahkoh/rust/move_items_unwrap = d34992e merged ok, testing candidate = 6f34760

@bors
Copy link
Contributor

@bors bors commented on d34992e Sep 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 6f34760

Please sign in to comment.