Specialize count for range iterators
This commit is contained in:
parent
e81f85fe9e
commit
08aa6c9b65
@ -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]
|
#[inline]
|
||||||
fn nth(&mut self, n: usize) -> Option<A> {
|
fn nth(&mut self, n: usize) -> Option<A> {
|
||||||
self.spec_nth(n)
|
self.spec_nth(n)
|
||||||
@ -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]
|
#[inline]
|
||||||
fn nth(&mut self, n: usize) -> Option<A> {
|
fn nth(&mut self, n: usize) -> Option<A> {
|
||||||
if self.is_empty() {
|
if self.is_empty() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user