Repository: rust-itertools/itertools (language: rust)

Issue:
Clonable Chunks iterator?
It seems that `Chunks` doesn't implement Clone, so there's no way to reuse the iterator if you want multiple copies of the chunks
Clonable Chunks iterator?
It seems that `Chunks` doesn't implement Clone, so there's no way to reuse the iterator if you want multiple copies of the chunks


Discussion / hints:

Fix patch:
diff --git a/src/groupbylazy.rs b/src/groupbylazy.rs
index a5a321df4..80c6f09f3 100644
--- a/src/groupbylazy.rs
+++ b/src/groupbylazy.rs
@@ -19,7 +19,7 @@ impl<A, K, F: ?Sized> KeyFunction<A> for F
 
 
 /// `ChunkIndex` acts like the grouping key function for `IntoChunks`
-#[derive(Debug)]
+#[derive(Debug, Clone)]
 struct ChunkIndex {
     size: usize,
     index: usize,
@@ -50,7 +50,7 @@ impl<A> KeyFunction<A> for ChunkIndex {
     }
 }
 
-
+#[derive(Clone)]
 struct GroupInner<K, I, F>
     where I: Iterator
 {
@@ -471,6 +471,13 @@ pub struct IntoChunks<I>
     index: Cell<usize>,
 }
 
+impl<I> Clone for IntoChunks<I>
+    where I: Clone + Iterator,
+          I::Item: Clone,
+{
+    clone_fields!(inner, index);
+}
+
 
 impl<I> IntoChunks<I>
     where I: Iterator,
@@ -507,6 +514,7 @@ impl<'a, I> IntoIterator for &'a IntoChunks<I>
 ///
 /// See [`.chunks()`](crate::Itertools::chunks) for more information.
 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
+#[derive(Clone)]
 pub struct Chunks<'a, I: 'a>
     where I: Iterator,
           I::Item: 'a,


Test patch:
diff --git a/tests/quick.rs b/tests/quick.rs
index 0adcf1ad7..7af0ef602 100644
--- a/tests/quick.rs
+++ b/tests/quick.rs
@@ -993,6 +993,17 @@ quickcheck! {
     }
 }
 
+quickcheck! {
+    fn chunk_clone_equal(a: Vec<u8>, size: u8) -> () {
+        let mut size = size;
+        if size == 0 {
+            size += 1;
+        }
+        let it = a.chunks(size as usize);
+        itertools::assert_equal(it.clone(), it);
+    }
+}
+
 quickcheck! {
     fn equal_chunks_lazy(a: Vec<u8>, size: u8) -> bool {
         let mut size = size;