Skip to content

Commit 3a66c7f

Browse files
committed
Auto merge of #24120 - aturon:range-perf, r=alexcrichton
A recent change to the implementation of range iterators meant that, even when stepping by 1, the iterators *always* involved checked arithmetic. This commit reverts to the earlier behavior (while retaining the refactoring into traits). Fixes #24095 Closes #24119 cc #24014 r? @alexcrichton
2 parents ce97c19 + dddcbcf commit 3a66c7f

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/libcore/iter.rs

+23-24
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use self::MinMaxResult::*;
6060

6161
use clone::Clone;
6262
use cmp;
63-
use cmp::Ord;
63+
use cmp::{Ord, PartialOrd, PartialEq};
6464
use default::Default;
6565
use marker;
6666
use mem;
@@ -2382,7 +2382,7 @@ impl<A, St, F> Iterator for Unfold<St, F> where F: FnMut(&mut St) -> Option<A> {
23822382
/// two `Step` objects.
23832383
#[unstable(feature = "step_trait",
23842384
reason = "likely to be replaced by finer-grained traits")]
2385-
pub trait Step: Ord {
2385+
pub trait Step: PartialOrd {
23862386
/// Steps `self` if possible.
23872387
fn step(&self, by: &Self) -> Option<Self>;
23882388

@@ -2549,7 +2549,10 @@ pub fn range_inclusive<A>(start: A, stop: A) -> RangeInclusive<A>
25492549

25502550
#[unstable(feature = "core",
25512551
reason = "likely to be replaced by range notation and adapters")]
2552-
impl<A: Step + One + Clone> Iterator for RangeInclusive<A> {
2552+
impl<A> Iterator for RangeInclusive<A> where
2553+
A: PartialEq + Step + One + Clone,
2554+
for<'a> &'a A: Add<&'a A, Output = A>
2555+
{
25532556
type Item = A;
25542557

25552558
#[inline]
@@ -2579,9 +2582,10 @@ impl<A: Step + One + Clone> Iterator for RangeInclusive<A> {
25792582

25802583
#[unstable(feature = "core",
25812584
reason = "likely to be replaced by range notation and adapters")]
2582-
impl<A> DoubleEndedIterator for RangeInclusive<A>
2583-
where A: Step + One + Clone,
2584-
for<'a> &'a A: Sub<Output=A>
2585+
impl<A> DoubleEndedIterator for RangeInclusive<A> where
2586+
A: PartialEq + Step + One + Clone,
2587+
for<'a> &'a A: Add<&'a A, Output = A>,
2588+
for<'a> &'a A: Sub<Output=A>
25852589
{
25862590
#[inline]
25872591
fn next_back(&mut self) -> Option<A> {
@@ -2709,24 +2713,17 @@ macro_rules! range_exact_iter_impl {
27092713

27102714
#[stable(feature = "rust1", since = "1.0.0")]
27112715
#[allow(deprecated)]
2712-
impl<A: Step + One + Clone> Iterator for ops::Range<A> {
2716+
impl<A: Step + One> Iterator for ops::Range<A> where
2717+
for<'a> &'a A: Add<&'a A, Output = A>
2718+
{
27132719
type Item = A;
27142720

27152721
#[inline]
27162722
fn next(&mut self) -> Option<A> {
27172723
if self.start < self.end {
2718-
match self.start.step(&A::one()) {
2719-
Some(mut n) => {
2720-
mem::swap(&mut n, &mut self.start);
2721-
Some(n)
2722-
},
2723-
None => {
2724-
let mut n = self.end.clone();
2725-
mem::swap(&mut n, &mut self.start);
2726-
Some(n)
2727-
2728-
}
2729-
}
2724+
let mut n = &self.start + &A::one();
2725+
mem::swap(&mut n, &mut self.start);
2726+
Some(n)
27302727
} else {
27312728
None
27322729
}
@@ -2748,6 +2745,7 @@ range_exact_iter_impl!(usize u8 u16 u32 isize i8 i16 i32);
27482745
#[stable(feature = "rust1", since = "1.0.0")]
27492746
#[allow(deprecated)]
27502747
impl<A: Step + One + Clone> DoubleEndedIterator for ops::Range<A> where
2748+
for<'a> &'a A: Add<&'a A, Output = A>,
27512749
for<'a> &'a A: Sub<&'a A, Output = A>
27522750
{
27532751
#[inline]
@@ -2763,15 +2761,16 @@ impl<A: Step + One + Clone> DoubleEndedIterator for ops::Range<A> where
27632761

27642762
#[stable(feature = "rust1", since = "1.0.0")]
27652763
#[allow(deprecated)]
2766-
impl<A: Step + One> Iterator for ops::RangeFrom<A> {
2764+
impl<A: Step + One> Iterator for ops::RangeFrom<A> where
2765+
for<'a> &'a A: Add<&'a A, Output = A>
2766+
{
27672767
type Item = A;
27682768

27692769
#[inline]
27702770
fn next(&mut self) -> Option<A> {
2771-
self.start.step(&A::one()).map(|mut n| {
2772-
mem::swap(&mut n, &mut self.start);
2773-
n
2774-
})
2771+
let mut n = &self.start + &A::one();
2772+
mem::swap(&mut n, &mut self.start);
2773+
Some(n)
27752774
}
27762775
}
27772776

src/test/compile-fail/range-1.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn main() {
1919
for i in false..true {}
2020
//~^ ERROR the trait
2121
//~^^ ERROR the trait
22+
//~^^^ ERROR the trait
2223

2324
// Unsized type.
2425
let arr: &[_] = &[1, 2, 3];

0 commit comments

Comments
 (0)