Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #657 #683

Merged
merged 3 commits into from Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/groupbylazy.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -50,7 +50,7 @@ impl<A> KeyFunction<A> for ChunkIndex {
}
}


#[derive(Clone)]
struct GroupInner<K, I, F>
where I: Iterator
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_std.rs
Expand Up @@ -842,6 +842,7 @@ fn group_by_lazy_3() {
fn chunks() {
let data = vec![0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3];
let grouper = data.iter().chunks(3);
let grouper_clone = grouper.clone();
for (i, chunk) in grouper.into_iter().enumerate() {
match i {
0 => it::assert_equal(chunk, &[0, 0, 0]),
Expand All @@ -851,6 +852,15 @@ fn chunks() {
_ => unreachable!(),
}
}
for (i, chunk) in grouper_clone.into_iter().enumerate() {
match i {
0 => it::assert_equal(chunk, &[0, 0, 0]),
1 => it::assert_equal(chunk, &[1, 1, 0]),
2 => it::assert_equal(chunk, &[0, 2, 2]),
3 => it::assert_equal(chunk, &[3, 3]),
_ => unreachable!(),
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid the duplication here with the above code block, and make it more explicit that you're testing that these produce the same output.

You can achieve this by doing something like:

for (i, (chunk, cloned_chunk)) in grouper.into_iter(grouper_clone).zip().enumerate() {
    assert_eq!(chunk, cloned_chunk);
    match i { /* the match branches */ }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d608283

}

#[test]
Expand Down