diff options
| author | ltdk <usr@ltdk.xyz> | 2023-06-02 20:07:26 -0400 |
|---|---|---|
| committer | ltdk <usr@ltdk.xyz> | 2023-09-16 01:33:56 -0400 |
| commit | 08aa6c9b650ba83b58600d0794bcf0bc8eef7a42 (patch) | |
| tree | 6f8e9111a63288f5566da90038ed4ae23862ab7d | |
| parent | e81f85fe9eebf8a5ba21438fb188a59d54095697 (diff) | |
| download | rust-08aa6c9b650ba83b58600d0794bcf0bc8eef7a42.tar.gz rust-08aa6c9b650ba83b58600d0794bcf0bc8eef7a42.zip | |
Specialize count for range iterators
| -rw-r--r-- | library/core/src/iter/range.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/library/core/src/iter/range.rs b/library/core/src/iter/range.rs index 44feb0a5638..391e03636ab 100644 --- a/library/core/src/iter/range.rs +++ b/library/core/src/iter/range.rs @@ -766,6 +766,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) } @@ -1163,6 +1172,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() { return None; |
