Skip to content

Commit fdc96f5

Browse files
committed
Implement Clone for IntoIter<A> where A: Clone
1 parent c1870ea commit fdc96f5

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,24 @@ pub struct IntoIter<A: Array> {
15041504
end: usize,
15051505
}
15061506

1507+
/*impl<A: Array + Clone> Clone for IntoIter<A>
1508+
where
1509+
A::Item: Copy,
1510+
{
1511+
fn clone(&self) -> IntoIter<A> {
1512+
SmallVec::from_slice(self.as_slice()).into_iter()
1513+
}
1514+
}*/
1515+
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+
15071525
impl<A: Array> Drop for IntoIter<A> {
15081526
fn drop(&mut self) {
15091527
for _ in self {}
@@ -2248,6 +2266,37 @@ mod tests {
22482266
assert_eq!(iter.as_mut_slice(), &[2]);
22492267
}
22502268

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+
22512300
#[test]
22522301
fn shrink_to_fit_unspill() {
22532302
let mut vec = SmallVec::<[u8; 2]>::from_iter(0..3);

0 commit comments

Comments
 (0)