Skip to content

Commit 23b8562

Browse files
committed
Specialize count for range iterators
1 parent e29821f commit 23b8562

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
@@ -722,6 +722,15 @@ impl<A: Step> Iterator for ops::Range<A> {
722722
}
723723
}
724724

725+
#[inline]
726+
fn count(self) -> usize {
727+
if self.start < self.end {
728+
Step::steps_between(&self.start, &self.end).expect("count overflowed usize")
729+
} else {
730+
0
731+
}
732+
}
733+
725734
#[inline]
726735
fn nth(&mut self, n: usize) -> Option<A> {
727736
self.spec_nth(n)
@@ -1119,6 +1128,17 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
11191128
}
11201129
}
11211130

1131+
#[inline]
1132+
fn count(self) -> usize {
1133+
if self.is_empty() {
1134+
return 0;
1135+
}
1136+
1137+
Step::steps_between(&self.start, &self.end)
1138+
.and_then(|steps| steps.checked_add(1))
1139+
.expect("count overflowed usize")
1140+
}
1141+
11221142
#[inline]
11231143
fn nth(&mut self, n: usize) -> Option<A> {
11241144
if self.is_empty() {

0 commit comments

Comments
 (0)