Skip to content

Commit 941d6aa

Browse files
author
bors-servo
authored
Auto merge of #192 - L0uisc:clone-into-iter, r=mbrubeck
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.
2 parents 731c0e9 + 1fa1545 commit 941d6aa

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

lib.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,15 @@ pub struct IntoIter<A: Array> {
15131513
end: usize,
15141514
}
15151515

1516+
impl<A: Array + Clone> Clone for IntoIter<A>
1517+
where
1518+
A::Item: Clone,
1519+
{
1520+
fn clone(&self) -> IntoIter<A> {
1521+
SmallVec::from(self.as_slice()).into_iter()
1522+
}
1523+
}
1524+
15161525
impl<A: Array> Drop for IntoIter<A> {
15171526
fn drop(&mut self) {
15181527
for _ in self {}
@@ -2257,6 +2266,37 @@ mod tests {
22572266
assert_eq!(iter.as_mut_slice(), &[2]);
22582267
}
22592268

2269+
#[test]
2270+
fn test_into_iter_clone() {
2271+
// Test that the cloned iterator yields identical elements and that it owns its own copy
2272+
// (i.e. no use after move errors).
2273+
let mut iter = SmallVec::<[u8; 2]>::from_iter(0..3).into_iter();
2274+
let mut clone_iter = iter.clone();
2275+
while let Some(x) = iter.next() {
2276+
assert_eq!(x, clone_iter.next().unwrap());
2277+
}
2278+
assert_eq!(clone_iter.next(), None);
2279+
}
2280+
2281+
#[test]
2282+
fn test_into_iter_clone_partially_consumed_iterator() {
2283+
// Test that the cloned iterator only contains the remaining elements of the original iterator.
2284+
let mut iter = SmallVec::<[u8; 2]>::from_iter(0..3).into_iter().skip(1);
2285+
let mut clone_iter = iter.clone();
2286+
while let Some(x) = iter.next() {
2287+
assert_eq!(x, clone_iter.next().unwrap());
2288+
}
2289+
assert_eq!(clone_iter.next(), None);
2290+
}
2291+
2292+
#[test]
2293+
fn test_into_iter_clone_empty_smallvec() {
2294+
let mut iter = SmallVec::<[u8; 2]>::new().into_iter();
2295+
let mut clone_iter = iter.clone();
2296+
assert_eq!(iter.next(), None);
2297+
assert_eq!(clone_iter.next(), None);
2298+
}
2299+
22602300
#[test]
22612301
fn shrink_to_fit_unspill() {
22622302
let mut vec = SmallVec::<[u8; 2]>::from_iter(0..3);

0 commit comments

Comments
 (0)