Simplify RangeInclusive::next[_back]

`match`ing on an `Option<Ordering>` seems cause some confusion for LLVM; switching to just using comparison operators removes a few jumps from the simple `for` loops I was trying.
This commit is contained in:
Scott McMurray 2018-02-07 11:11:54 -08:00
parent 4f93357d3b
commit 27d4d51670
2 changed files with 31 additions and 16 deletions

View File

@ -331,19 +331,17 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
#[inline]
fn next(&mut self) -> Option<A> {
use cmp::Ordering::*;
match self.start.partial_cmp(&self.end) {
Some(Less) => {
if self.start <= self.end {
if self.start < self.end {
let n = self.start.add_one();
Some(mem::replace(&mut self.start, n))
},
Some(Equal) => {
} else {
let last = self.start.replace_one();
self.end.replace_zero();
Some(last)
},
_ => None,
}
} else {
None
}
}
@ -425,19 +423,17 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
use cmp::Ordering::*;
match self.start.partial_cmp(&self.end) {
Some(Less) => {
if self.start <= self.end {
if self.start < self.end {
let n = self.end.sub_one();
Some(mem::replace(&mut self.end, n))
},
Some(Equal) => {
} else {
let last = self.end.replace_zero();
self.start.replace_one();
Some(last)
},
_ => None,
}
} else {
None
}
}

View File

@ -1332,6 +1332,18 @@ fn test_range_inclusive_exhaustion() {
assert_eq!(r.next_back(), Some(10));
assert_eq!(r, 1..=0);
let mut r = 10..=12;
assert_eq!(r.next(), Some(10));
assert_eq!(r.next(), Some(11));
assert_eq!(r.next(), Some(12));
assert_eq!(r, 1..=0);
let mut r = 10..=12;
assert_eq!(r.next_back(), Some(12));
assert_eq!(r.next_back(), Some(11));
assert_eq!(r.next_back(), Some(10));
assert_eq!(r, 1..=0);
let mut r = 10..=12;
assert_eq!(r.nth(2), Some(12));
assert_eq!(r, 1..=0);
@ -1340,6 +1352,13 @@ fn test_range_inclusive_exhaustion() {
assert_eq!(r.nth(5), None);
assert_eq!(r, 1..=0);
let mut r = 100..=10;
assert_eq!(r.next(), None);
assert_eq!(r, 100..=10);
let mut r = 100..=10;
assert_eq!(r.next_back(), None);
assert_eq!(r, 100..=10);
}
#[test]