Skip to content

Commit

Permalink
Auto merge of rust-lang#112229 - clarfonthey:range-iter-count, r=dtolnay
Browse files Browse the repository at this point in the history
Specialize count for range iterators

Since `size_hint` is already specialized, it feels apt to specialize `count` as well. Without any specialized version of `ExactSizeIterator::len` or `Step::steps_between`, this feels like a more reliable way of accessing this without having to rely on knowing that `size_hint` is correct.

In my case, this is particularly useful to access the `steps_between` implementation for `char` from the standard library without having to compute it manually.

I didn't think it was worth modifying the `Step` trait to add a version of `steps_between` that used native overflow checks since this is just doing one subtraction in most cases anyway, and so I decided to make the inclusive version use `checked_add` so it didn't have this lopsided overflow-checks-but-only-sometimes logic.
  • Loading branch information
bors committed Sep 16, 2023
2 parents 409e7f6 + 08aa6c9 commit 4514fb9
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions library/core/src/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,15 @@ impl<A: Step> Iterator for ops::Range<A> {
}
}

#[inline]
fn count(self) -> usize {
if self.start < self.end {
Step::steps_between(&self.start, &self.end).expect("count overflowed usize")
} else {
0
}
}

#[inline]
fn nth(&mut self, n: usize) -> Option<A> {
self.spec_nth(n)
Expand Down Expand Up @@ -1162,6 +1171,17 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
}
}

#[inline]
fn count(self) -> usize {
if self.is_empty() {
return 0;
}

Step::steps_between(&self.start, &self.end)
.and_then(|steps| steps.checked_add(1))
.expect("count overflowed usize")
}

#[inline]
fn nth(&mut self, n: usize) -> Option<A> {
if self.is_empty() {
Expand Down

0 comments on commit 4514fb9

Please sign in to comment.