Skip to content

Untangle InterleaveShortest::size_hint and fix #1068#1112

Open
phimuemue wants to merge 13 commits into
rust-itertools:masterfrom
phimuemue:untangle_interleaveshortest_size_hint
Open

Untangle InterleaveShortest::size_hint and fix #1068#1112
phimuemue wants to merge 13 commits into
rust-itertools:masterfrom
phimuemue:untangle_interleaveshortest_size_hint

Conversation

@phimuemue

@phimuemue phimuemue commented Jun 30, 2026

Copy link
Copy Markdown
Member

While reviewing #1109, I realized that InterleaveShortest::size_hint is hard too understand, mainly because we compare lower/upper bounds separately and with the magic size_hint::min methods.

I kept my commits so that you can follow step-by-step how I untangled it. At the end, we do - both for the size_hint's lower and upper bound - this: Compare "current" vs "next" iterator's estimated length, and act accordingly.

Thanks @patrickwehbe, @c-tonneslan and @ronnodas for your ideas on this.

phimuemue and others added 13 commits June 30, 2026 20:48
Simplifies subsequent diffs.
The function was not used anywhere else, so I cut-pasted it.
…int_min

Simplifies subsequent diffs.
The function is in other places, so I copy-pasted it.
Before this commit, we repeated the comparison between `curr_lower` and
`next_lower`, because we did it explicitly, and implicitly through
`cmp::min`.

Now, because we rely on one comparison.

This simplifies the logic:
If `curr_lower > next_lower`, then the "next" iterator is the limitting
factor, and there's one additional element at the end coming from the
"current" iterator.
Otherwise, the "current" iterator is the limitting factor, and for each
element of the "current" iterator, there's one from the "next" iterator,
so we do not have to add 1.
This avoids constructing an `Option`, just to later inspect it and map
it via `and_then`.

It's a bit more code, but I'd argue it's clearer.
We now do the `checked_add(1)` inline instead of computing `extra_elem`
and then looking back to see if we shall add 1.

Now, the computation of `lower` and `upper` is (at least somewhat) in
line: Check if curr or next is the limitting factor and behave
accordingly.
If "next" is the limitting factor, there may be one element from "curr"
at the end. But if this element leads to more than `usize::MAX`
elements, a lower bound of `usize::MAX` is still good enough.

Fixes rust-itertools#1068
@phimuemue phimuemue requested a review from jswrenn June 30, 2026 19:02
@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.98%. Comparing base (6814180) to head (881101f).
⚠️ Report is 210 commits behind head on master.

Files with missing lines Patch % Lines
src/adaptors/mod.rs 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1112      +/-   ##
==========================================
- Coverage   94.38%   93.98%   -0.40%     
==========================================
  Files          48       52       +4     
  Lines        6665     6671       +6     
==========================================
- Hits         6291     6270      -21     
- Misses        374      401      +27     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

interleave_shortest's size_hint panics with large iterators.

1 participant