Skip to content

Commit

Permalink
Auto merge of #192 - L0uisc:clone-into-iter, r=mbrubeck
Browse files Browse the repository at this point in the history
Implement Clone for IntoIter<A> where A: Clone

Implemented code to fix #178. The commented impl can be uncommented once specialization is possible. Both impls were tested and passed the three added tests.
  • Loading branch information
bors-servo committed Dec 13, 2019
2 parents 731c0e9 + 1fa1545 commit 941d6aa
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib.rs
Expand Up @@ -1513,6 +1513,15 @@ pub struct IntoIter<A: Array> {
end: usize,
}

impl<A: Array + Clone> Clone for IntoIter<A>
where
A::Item: Clone,
{
fn clone(&self) -> IntoIter<A> {
SmallVec::from(self.as_slice()).into_iter()
}
}

impl<A: Array> Drop for IntoIter<A> {
fn drop(&mut self) {
for _ in self {}
Expand Down Expand Up @@ -2257,6 +2266,37 @@ mod tests {
assert_eq!(iter.as_mut_slice(), &[2]);
}

#[test]
fn test_into_iter_clone() {
// Test that the cloned iterator yields identical elements and that it owns its own copy
// (i.e. no use after move errors).
let mut iter = SmallVec::<[u8; 2]>::from_iter(0..3).into_iter();
let mut clone_iter = iter.clone();
while let Some(x) = iter.next() {
assert_eq!(x, clone_iter.next().unwrap());
}
assert_eq!(clone_iter.next(), None);
}

#[test]
fn test_into_iter_clone_partially_consumed_iterator() {
// Test that the cloned iterator only contains the remaining elements of the original iterator.
let mut iter = SmallVec::<[u8; 2]>::from_iter(0..3).into_iter().skip(1);
let mut clone_iter = iter.clone();
while let Some(x) = iter.next() {
assert_eq!(x, clone_iter.next().unwrap());
}
assert_eq!(clone_iter.next(), None);
}

#[test]
fn test_into_iter_clone_empty_smallvec() {
let mut iter = SmallVec::<[u8; 2]>::new().into_iter();
let mut clone_iter = iter.clone();
assert_eq!(iter.next(), None);
assert_eq!(clone_iter.next(), None);
}

#[test]
fn shrink_to_fit_unspill() {
let mut vec = SmallVec::<[u8; 2]>::from_iter(0..3);
Expand Down

0 comments on commit 941d6aa

Please sign in to comment.