From fdc96f5586a19cdc3ca9983be2c531737f2c9447 Mon Sep 17 00:00:00 2001 From: L0uisc Date: Thu, 12 Dec 2019 23:15:18 +0200 Subject: [PATCH 1/2] Implement Clone for IntoIter where A: Clone --- lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lib.rs b/lib.rs index e919678..b83c865 100644 --- a/lib.rs +++ b/lib.rs @@ -1504,6 +1504,24 @@ pub struct IntoIter { end: usize, } +/*impl Clone for IntoIter +where + A::Item: Copy, +{ + fn clone(&self) -> IntoIter { + SmallVec::from_slice(self.as_slice()).into_iter() + } +}*/ + +impl Clone for IntoIter +where + A::Item: Clone, +{ + fn clone(&self) -> IntoIter { + SmallVec::from(self.as_slice()).into_iter() + } +} + impl Drop for IntoIter { fn drop(&mut self) { for _ in self {} @@ -2248,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); From 1fa1545e776169c9d4ac39bc67f1d4e0a3a5c3bc Mon Sep 17 00:00:00 2001 From: L0uisc Date: Fri, 13 Dec 2019 20:54:01 +0200 Subject: [PATCH 2/2] Remove specialized impl --- lib.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/lib.rs b/lib.rs index b83c865..9f78c0a 100644 --- a/lib.rs +++ b/lib.rs @@ -1504,15 +1504,6 @@ pub struct IntoIter { end: usize, } -/*impl Clone for IntoIter -where - A::Item: Copy, -{ - fn clone(&self) -> IntoIter { - SmallVec::from_slice(self.as_slice()).into_iter() - } -}*/ - impl Clone for IntoIter where A::Item: Clone,