Skip to content

Commit

Permalink
Implement Clone for std::vec::IntoIter
Browse files Browse the repository at this point in the history
  • Loading branch information
tbu- committed Feb 17, 2016
1 parent b54770c commit 8fd7469
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/libcollections/vec.rs
Expand Up @@ -59,9 +59,10 @@

#![stable(feature = "rust1", since = "1.0.0")]

use alloc::raw_vec::RawVec;
use alloc::boxed::Box;
use alloc::heap::EMPTY;
use alloc::raw_vec::RawVec;
use borrow::ToOwned;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{self, Hash};
Expand Down Expand Up @@ -1633,6 +1634,15 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ExactSizeIterator for IntoIter<T> {}

#[stable(feature = "vec_into_iter_clone", since = "1.8.0")]
impl<T: Clone> Clone for IntoIter<T> {
fn clone(&self) -> IntoIter<T> {
unsafe {
slice::from_raw_parts(self.ptr, self.len()).to_owned().into_iter()
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Drop for IntoIter<T> {
#[unsafe_destructor_blind_to_params]
Expand Down
18 changes: 18 additions & 0 deletions src/libcollectionstest/vec.rs
Expand Up @@ -467,6 +467,24 @@ fn test_into_iter_count() {
assert_eq!(vec![1, 2, 3].into_iter().count(), 3);
}

#[test]
fn test_into_iter_clone() {
fn iter_equal<I: Iterator<Item=i32>>(it: I, slice: &[i32]) {
let v: Vec<i32> = it.collect();
assert_eq!(&v[..], slice);
}
let mut it = vec![1, 2, 3].into_iter();
iter_equal(it.clone(), &[1, 2, 3]);
assert_eq!(it.next(), Some(1));
let mut it = it.rev();
iter_equal(it.clone(), &[3, 2]);
assert_eq!(it.next(), Some(3));
iter_equal(it.clone(), &[2]);
assert_eq!(it.next(), Some(2));
iter_equal(it.clone(), &[]);
assert_eq!(it.next(), None);
}

#[test]
fn test_cow_from() {
let borrowed: &[_] = &["borrowed", "(slice)"];
Expand Down

0 comments on commit 8fd7469

Please sign in to comment.