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
This commit is contained in:
bors
2015-04-08 07:02:06 +00:00
2 changed files with 24 additions and 24 deletions

View File

@@ -60,7 +60,7 @@ use self::MinMaxResult::*;
use clone::Clone; use clone::Clone;
use cmp; use cmp;
use cmp::Ord; use cmp::{Ord, PartialOrd, PartialEq};
use default::Default; use default::Default;
use marker; use marker;
use mem; use mem;
@@ -2382,7 +2382,7 @@ impl<A, St, F> Iterator for Unfold<St, F> where F: FnMut(&mut St) -> Option<A> {
/// two `Step` objects. /// two `Step` objects.
#[unstable(feature = "step_trait", #[unstable(feature = "step_trait",
reason = "likely to be replaced by finer-grained traits")] reason = "likely to be replaced by finer-grained traits")]
pub trait Step: Ord { pub trait Step: PartialOrd {
/// Steps `self` if possible. /// Steps `self` if possible.
fn step(&self, by: &Self) -> Option<Self>; fn step(&self, by: &Self) -> Option<Self>;
@@ -2549,7 +2549,10 @@ pub fn range_inclusive<A>(start: A, stop: A) -> RangeInclusive<A>
#[unstable(feature = "core", #[unstable(feature = "core",
reason = "likely to be replaced by range notation and adapters")] reason = "likely to be replaced by range notation and adapters")]
impl<A: Step + One + Clone> Iterator for RangeInclusive<A> { impl<A> Iterator for RangeInclusive<A> where
A: PartialEq + Step + One + Clone,
for<'a> &'a A: Add<&'a A, Output = A>
{
type Item = A; type Item = A;
#[inline] #[inline]
@@ -2579,9 +2582,10 @@ impl<A: Step + One + Clone> Iterator for RangeInclusive<A> {
#[unstable(feature = "core", #[unstable(feature = "core",
reason = "likely to be replaced by range notation and adapters")] reason = "likely to be replaced by range notation and adapters")]
impl<A> DoubleEndedIterator for RangeInclusive<A> impl<A> DoubleEndedIterator for RangeInclusive<A> where
where A: Step + One + Clone, A: PartialEq + Step + One + Clone,
for<'a> &'a A: Sub<Output=A> for<'a> &'a A: Add<&'a A, Output = A>,
for<'a> &'a A: Sub<Output=A>
{ {
#[inline] #[inline]
fn next_back(&mut self) -> Option<A> { fn next_back(&mut self) -> Option<A> {
@@ -2709,24 +2713,17 @@ macro_rules! range_exact_iter_impl {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[allow(deprecated)] #[allow(deprecated)]
impl<A: Step + One + Clone> Iterator for ops::Range<A> { impl<A: Step + One> Iterator for ops::Range<A> where
for<'a> &'a A: Add<&'a A, Output = A>
{
type Item = A; type Item = A;
#[inline] #[inline]
fn next(&mut self) -> Option<A> { fn next(&mut self) -> Option<A> {
if self.start < self.end { if self.start < self.end {
match self.start.step(&A::one()) { let mut n = &self.start + &A::one();
Some(mut n) => { mem::swap(&mut n, &mut self.start);
mem::swap(&mut n, &mut self.start); Some(n)
Some(n)
},
None => {
let mut n = self.end.clone();
mem::swap(&mut n, &mut self.start);
Some(n)
}
}
} else { } else {
None None
} }
@@ -2748,6 +2745,7 @@ range_exact_iter_impl!(usize u8 u16 u32 isize i8 i16 i32);
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[allow(deprecated)] #[allow(deprecated)]
impl<A: Step + One + Clone> DoubleEndedIterator for ops::Range<A> where impl<A: Step + One + Clone> DoubleEndedIterator for ops::Range<A> where
for<'a> &'a A: Add<&'a A, Output = A>,
for<'a> &'a A: Sub<&'a A, Output = A> for<'a> &'a A: Sub<&'a A, Output = A>
{ {
#[inline] #[inline]
@@ -2763,15 +2761,16 @@ impl<A: Step + One + Clone> DoubleEndedIterator for ops::Range<A> where
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[allow(deprecated)] #[allow(deprecated)]
impl<A: Step + One> Iterator for ops::RangeFrom<A> { impl<A: Step + One> Iterator for ops::RangeFrom<A> where
for<'a> &'a A: Add<&'a A, Output = A>
{
type Item = A; type Item = A;
#[inline] #[inline]
fn next(&mut self) -> Option<A> { fn next(&mut self) -> Option<A> {
self.start.step(&A::one()).map(|mut n| { let mut n = &self.start + &A::one();
mem::swap(&mut n, &mut self.start); mem::swap(&mut n, &mut self.start);
n Some(n)
})
} }
} }

View File

@@ -19,6 +19,7 @@ pub fn main() {
for i in false..true {} for i in false..true {}
//~^ ERROR the trait //~^ ERROR the trait
//~^^ ERROR the trait //~^^ ERROR the trait
//~^^^ ERROR the trait
// Unsized type. // Unsized type.
let arr: &[_] = &[1, 2, 3]; let arr: &[_] = &[1, 2, 3];