Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 18, 2018
1 parent d1c2cbf commit 126b71f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 80 deletions.
82 changes: 7 additions & 75 deletions src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,9 @@
use cmp;
use fmt;
use iter_private::TrustedRandomAccess;
use ops::{self, Try};
use ops::Try;
use usize;
use intrinsics;
use mem;

#[stable(feature = "rust1", since = "1.0.0")]
pub use self::iterator::Iterator;
Expand Down Expand Up @@ -673,7 +672,12 @@ impl<I> Iterator for StepBy<I> where I: Iterator {

#[inline]
fn next(&mut self) -> Option<Self::Item> {
<Self as StepBySpecIterator>::spec_next(self)
if self.first_take {
self.first_take = false;
self.iter.next()
} else {
self.iter.nth(self.step)
}
}

#[inline]
Expand Down Expand Up @@ -733,78 +737,6 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
}
}

// hidden trait for specializing iterator methods
// could be generalized but is currently only used for StepBy
trait StepBySpecIterator {
type Item;
fn spec_next(&mut self) -> Option<Self::Item>;
}

impl<I> StepBySpecIterator for StepBy<I>
where
I: Iterator,
{
type Item = I::Item;

#[inline]
default fn spec_next(&mut self) -> Option<I::Item> {
if self.first_take {
self.first_take = false;
self.iter.next()
} else {
self.iter.nth(self.step)
}
}
}

impl<T> StepBySpecIterator for StepBy<ops::Range<T>>
where
T: Step,
{
#[inline]
fn spec_next(&mut self) -> Option<Self::Item> {
self.first_take = false;
if !(self.iter.start < self.iter.end) {
return None;
}
// add 1 to self.step to get original step size back
// it was decremented for the general case on construction
if let Some(n) = self.iter.start.add_usize(self.step+1) {
let next = mem::replace(&mut self.iter.start, n);
Some(next)
} else {
let last = self.iter.start.clone();
self.iter.start = self.iter.end.clone();
Some(last)
}
}
}

impl<T> StepBySpecIterator for StepBy<ops::RangeInclusive<T>>
where
T: Step,
{
#[inline]
fn spec_next(&mut self) -> Option<Self::Item> {
self.first_take = false;
self.iter.compute_is_empty();
if self.iter.is_empty.unwrap_or_default() {
return None;
}
// add 1 to self.step to get original step size back
// it was decremented for the general case on construction
if let Some(n) = self.iter.start.add_usize(self.step+1) {
self.iter.is_empty = Some(!(n <= self.iter.end));
let next = mem::replace(&mut self.iter.start, n);
Some(next)
} else {
let last = self.iter.start.clone();
self.iter.is_empty = Some(true);
Some(last)
}
}
}

// StepBy can only make the iterator shorter, so the len will still fit.
#[stable(feature = "iterator_step_by", since = "1.28.0")]
impl<I> ExactSizeIterator for StepBy<I> where I: ExactSizeIterator {}
Expand Down
7 changes: 2 additions & 5 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1619,11 +1619,8 @@ fn test_range_step() {
}

#[test]
fn test_range_inclusive_step() {
assert_eq!((0..=50).step_by(10).collect::<Vec<_>>(), [0, 10, 20, 30, 40, 50]);
assert_eq!((0..=5).step_by(1).collect::<Vec<_>>(), [0, 1, 2, 3, 4, 5]);
assert_eq!((200..=255u8).step_by(10).collect::<Vec<_>>(), [200, 210, 220, 230, 240, 250]);
assert_eq!((250..=255u8).step_by(1).collect::<Vec<_>>(), [250, 251, 252, 253, 254, 255]);
fn test_step_by_skip() {
assert_eq!((0..640).step_by(128).skip(1).collect::<Vec<_>>(), [128, 256, 384, 512]);
}

#[test]
Expand Down

0 comments on commit 126b71f

Please sign in to comment.