Skip to content

Commit 6f34760

Browse files
committed
auto merge of #16903 : mahkoh/rust/move_items_unwrap, r=aturon
Closes #16879
2 parents 5c39879 + d34992e commit 6f34760

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/libcollections/vec.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,19 @@ pub struct MoveItems<T> {
16181618
iter: Items<'static, T>
16191619
}
16201620

1621+
impl<T> MoveItems<T> {
1622+
#[inline]
1623+
/// Drops all items that have not yet been moved and returns the empty vector.
1624+
pub fn unwrap(mut self) -> Vec<T> {
1625+
unsafe {
1626+
for _x in self { }
1627+
let MoveItems { allocation, cap, iter: _iter } = self;
1628+
mem::forget(self);
1629+
Vec { ptr: allocation, cap: cap, len: 0 }
1630+
}
1631+
}
1632+
}
1633+
16211634
impl<T> Iterator<T> for MoveItems<T> {
16221635
#[inline]
16231636
fn next<'a>(&'a mut self) -> Option<T> {
@@ -2016,6 +2029,18 @@ mod tests {
20162029
assert_eq!(vec.swap_remove(0), None);
20172030
}
20182031

2032+
#[test]
2033+
fn test_move_iter_unwrap() {
2034+
let mut vec: Vec<uint> = Vec::with_capacity(7);
2035+
vec.push(1);
2036+
vec.push(2);
2037+
let ptr = vec.as_ptr();
2038+
vec = vec.move_iter().unwrap();
2039+
assert_eq!(vec.as_ptr(), ptr);
2040+
assert_eq!(vec.capacity(), 7);
2041+
assert_eq!(vec.len(), 0);
2042+
}
2043+
20192044
#[bench]
20202045
fn bench_new(b: &mut Bencher) {
20212046
b.iter(|| {

0 commit comments

Comments
 (0)