Skip to content

Commit 08aa6c9

Browse files
committed
Specialize count for range iterators
1 parent e81f85f commit 08aa6c9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

library/core/src/iter/range.rs

+20
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,15 @@ impl<A: Step> Iterator for ops::Range<A> {
765765
}
766766
}
767767

768+
#[inline]
769+
fn count(self) -> usize {
770+
if self.start < self.end {
771+
Step::steps_between(&self.start, &self.end).expect("count overflowed usize")
772+
} else {
773+
0
774+
}
775+
}
776+
768777
#[inline]
769778
fn nth(&mut self, n: usize) -> Option<A> {
770779
self.spec_nth(n)
@@ -1162,6 +1171,17 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
11621171
}
11631172
}
11641173

1174+
#[inline]
1175+
fn count(self) -> usize {
1176+
if self.is_empty() {
1177+
return 0;
1178+
}
1179+
1180+
Step::steps_between(&self.start, &self.end)
1181+
.and_then(|steps| steps.checked_add(1))
1182+
.expect("count overflowed usize")
1183+
}
1184+
11651185
#[inline]
11661186
fn nth(&mut self, n: usize) -> Option<A> {
11671187
if self.is_empty() {

0 commit comments

Comments
 (0)