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

Implement ExactSizeIterator for Tuples #761

Merged
merged 6 commits into from
Sep 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Tuples::size_hint: add jswrenn's comments
Co-authored-by: Jack Wrenn <me@jswrenn.com>
Philippe-Cholet and jswrenn authored Sep 27, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 040c794ef4ba0833d03dba789af83e4347ec8809
18 changes: 13 additions & 5 deletions src/tuple_impl.rs
Original file line number Diff line number Diff line change
@@ -107,11 +107,19 @@ where
}

fn size_hint(&self) -> (usize, Option<usize>) {
let buf_len = T::buffer_len(&self.buf);
let (mut low, mut hi) = self.iter.size_hint();
low = add_then_div(low, buf_len, T::num_items()).unwrap_or(usize::MAX);
hi = hi.and_then(|elt| add_then_div(elt, buf_len, T::num_items()));
(low, hi)
// The number of elts we've drawn from the underlying iterator, but have
// not yet produced as a tuple.
let buffered = T::buffer_len(&self.buf);
// To that, we must add the size estimates of the underlying iterator.
let (mut unbuffered_lo, mut unbuffered_hi) = self.iter.size_hint();
// The total low estimate is the sum of the already-buffered elements,
// plus the low estimate of remaining unbuffered elements, divided by
// the tuple size.
let total_lo = add_then_div(unbuffered_lo, buffered, T::num_items()).unwrap_or(usize::MAX);
// And likewise for the total high estimate, but using the high estimate
// of the remaining unbuffered elements.
let total_hi = unbuffered_hi.and_then(|hi| add_then_div(hi, buffered, T::num_items()));
(total_lo, total_hi)
}
}