mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-24 13:43:04 +00:00
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:
parent
4f93357d3b
commit
27d4d51670
@ -331,19 +331,17 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn next(&mut self) -> Option<A> {
|
fn next(&mut self) -> Option<A> {
|
||||||
use cmp::Ordering::*;
|
if self.start <= self.end {
|
||||||
|
if self.start < self.end {
|
||||||
match self.start.partial_cmp(&self.end) {
|
|
||||||
Some(Less) => {
|
|
||||||
let n = self.start.add_one();
|
let n = self.start.add_one();
|
||||||
Some(mem::replace(&mut self.start, n))
|
Some(mem::replace(&mut self.start, n))
|
||||||
},
|
} else {
|
||||||
Some(Equal) => {
|
|
||||||
let last = self.start.replace_one();
|
let last = self.start.replace_one();
|
||||||
self.end.replace_zero();
|
self.end.replace_zero();
|
||||||
Some(last)
|
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> {
|
impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn next_back(&mut self) -> Option<A> {
|
fn next_back(&mut self) -> Option<A> {
|
||||||
use cmp::Ordering::*;
|
if self.start <= self.end {
|
||||||
|
if self.start < self.end {
|
||||||
match self.start.partial_cmp(&self.end) {
|
|
||||||
Some(Less) => {
|
|
||||||
let n = self.end.sub_one();
|
let n = self.end.sub_one();
|
||||||
Some(mem::replace(&mut self.end, n))
|
Some(mem::replace(&mut self.end, n))
|
||||||
},
|
} else {
|
||||||
Some(Equal) => {
|
|
||||||
let last = self.end.replace_zero();
|
let last = self.end.replace_zero();
|
||||||
self.start.replace_one();
|
self.start.replace_one();
|
||||||
Some(last)
|
Some(last)
|
||||||
},
|
}
|
||||||
_ => None,
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1332,6 +1332,18 @@ fn test_range_inclusive_exhaustion() {
|
|||||||
assert_eq!(r.next_back(), Some(10));
|
assert_eq!(r.next_back(), Some(10));
|
||||||
assert_eq!(r, 1..=0);
|
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;
|
let mut r = 10..=12;
|
||||||
assert_eq!(r.nth(2), Some(12));
|
assert_eq!(r.nth(2), Some(12));
|
||||||
assert_eq!(r, 1..=0);
|
assert_eq!(r, 1..=0);
|
||||||
@ -1340,6 +1352,13 @@ fn test_range_inclusive_exhaustion() {
|
|||||||
assert_eq!(r.nth(5), None);
|
assert_eq!(r.nth(5), None);
|
||||||
assert_eq!(r, 1..=0);
|
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]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user