Skip to content

Commit

Permalink
Auto merge of #43012 - scottmcm:delete-range-step-by, r=alexcrichton
Browse files Browse the repository at this point in the history
Delete deprecated & unstable range-specific `step_by`

Using the new one is annoying while this one exists, since the inherent method hides the one on iterator.

Tracking issue: #27741
Replacement: #41439
Deprecation: #42310 for 1.19
Fixes #41477
  • Loading branch information
bors committed Jul 4, 2017
2 parents ed1ef19 + dcd332e commit 7a75d2b
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 251 deletions.
3 changes: 1 addition & 2 deletions src/liballoc/tests/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,7 @@ fn test_from_iter() {
let u: Vec<_> = deq.iter().cloned().collect();
assert_eq!(u, v);

// FIXME #27741: Remove `.skip(0)` when Range::step_by is fully removed
let seq = (0..).skip(0).step_by(2).take(256);
let seq = (0..).step_by(2).take(256);
let deq: VecDeque<_> = seq.collect();
for (i, &x) in deq.iter().enumerate() {
assert_eq!(2 * i, x);
Expand Down
6 changes: 0 additions & 6 deletions src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,6 @@ pub use self::iterator::Iterator;
reason = "likely to be replaced by finer-grained traits",
issue = "42168")]
pub use self::range::Step;
#[unstable(feature = "step_by", reason = "recent addition",
issue = "27741")]
#[rustc_deprecated(since = "1.19.0",
reason = "replaced by `iter::StepBy`")]
#[allow(deprecated)]
pub use self::range::StepBy as DeprecatedStepBy;

#[stable(feature = "rust1", since = "1.0.0")]
pub use self::sources::{Repeat, repeat};
Expand Down
213 changes: 0 additions & 213 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,219 +244,6 @@ step_impl_signed!(i64);
step_impl_no_between!(u64 i64);
step_impl_no_between!(u128 i128);

/// An adapter for stepping range iterators by a custom amount.
///
/// The resulting iterator handles overflow by stopping. The `A`
/// parameter is the type being iterated over, while `R` is the range
/// type (usually one of `std::ops::{Range, RangeFrom, RangeInclusive}`.
#[derive(Clone, Debug)]
#[unstable(feature = "step_by", reason = "recent addition",
issue = "27741")]
#[rustc_deprecated(since = "1.19.0",
reason = "replaced by `iter::StepBy`")]
#[allow(deprecated)]
pub struct StepBy<A, R> {
step_by: A,
range: R,
}

impl<A: Step> ops::RangeFrom<A> {
/// Creates an iterator starting at the same point, but stepping by
/// the given amount at each iteration.
///
/// # Examples
///
/// ```
/// #![feature(step_by)]
/// fn main() {
/// let result: Vec<_> = (0..).step_by(2).take(5).collect();
/// assert_eq!(result, vec![0, 2, 4, 6, 8]);
/// }
/// ```
#[unstable(feature = "step_by", reason = "recent addition",
issue = "27741")]
#[rustc_deprecated(since = "1.19.0",
reason = "replaced by `Iterator::step_by`")]
#[allow(deprecated)]
pub fn step_by(self, by: A) -> StepBy<A, Self> {
StepBy {
step_by: by,
range: self
}
}
}

impl<A: Step> ops::Range<A> {
/// Creates an iterator with the same range, but stepping by the
/// given amount at each iteration.
///
/// The resulting iterator handles overflow by stopping.
///
/// # Examples
///
/// ```
/// #![feature(step_by)]
/// fn main() {
/// let result: Vec<_> = (0..10).step_by(2).collect();
/// assert_eq!(result, vec![0, 2, 4, 6, 8]);
/// }
/// ```
#[unstable(feature = "step_by", reason = "recent addition",
issue = "27741")]
#[rustc_deprecated(since = "1.19.0",
reason = "replaced by `Iterator::step_by`")]
#[allow(deprecated)]
pub fn step_by(self, by: A) -> StepBy<A, Self> {
StepBy {
step_by: by,
range: self
}
}
}

impl<A: Step> ops::RangeInclusive<A> {
/// Creates an iterator with the same range, but stepping by the
/// given amount at each iteration.
///
/// The resulting iterator handles overflow by stopping.
///
/// # Examples
///
/// ```
/// #![feature(step_by, inclusive_range_syntax)]
///
/// let result: Vec<_> = (0...10).step_by(2).collect();
/// assert_eq!(result, vec![0, 2, 4, 6, 8, 10]);
/// ```
#[unstable(feature = "step_by", reason = "recent addition",
issue = "27741")]
#[rustc_deprecated(since = "1.19.0",
reason = "replaced by `Iterator::step_by`")]
#[allow(deprecated)]
pub fn step_by(self, by: A) -> StepBy<A, Self> {
StepBy {
step_by: by,
range: self
}
}
}

#[unstable(feature = "step_by", reason = "recent addition",
issue = "27741")]
#[allow(deprecated)]
impl<A> Iterator for StepBy<A, ops::RangeFrom<A>> where
A: Clone,
for<'a> &'a A: Add<&'a A, Output = A>
{
type Item = A;

#[inline]
fn next(&mut self) -> Option<A> {
let mut n = &self.range.start + &self.step_by;
mem::swap(&mut n, &mut self.range.start);
Some(n)
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(usize::MAX, None) // Too bad we can't specify an infinite lower bound
}
}

#[unstable(feature = "fused", issue = "35602")]
#[allow(deprecated)]
impl<A> FusedIterator for StepBy<A, ops::RangeFrom<A>>
where A: Clone, for<'a> &'a A: Add<&'a A, Output = A> {}

#[unstable(feature = "step_by", reason = "recent addition",
issue = "27741")]
#[allow(deprecated)]
impl<A: Step + Clone> Iterator for StepBy<A, ops::Range<A>> {
type Item = A;

#[inline]
fn next(&mut self) -> Option<A> {
let rev = self.step_by.is_negative();
if (rev && self.range.start > self.range.end) ||
(!rev && self.range.start < self.range.end)
{
match self.range.start.step(&self.step_by) {
Some(mut n) => {
mem::swap(&mut self.range.start, &mut n);
Some(n)
},
None => {
let mut n = self.range.end.clone();
mem::swap(&mut self.range.start, &mut n);
Some(n)
}
}
} else {
None
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match Step::steps_between(&self.range.start,
&self.range.end,
&self.step_by) {
Some(hint) => (hint, Some(hint)),
None => (0, None)
}
}
}

#[unstable(feature = "fused", issue = "35602")]
#[allow(deprecated)]
impl<A: Step + Clone> FusedIterator for StepBy<A, ops::Range<A>> {}

#[unstable(feature = "inclusive_range",
reason = "recently added, follows RFC",
issue = "28237")]
#[allow(deprecated)]
impl<A: Step + Clone> Iterator for StepBy<A, ops::RangeInclusive<A>> {
type Item = A;

#[inline]
fn next(&mut self) -> Option<A> {
let rev = self.step_by.is_negative();

if (rev && self.range.start >= self.range.end) ||
(!rev && self.range.start <= self.range.end)
{
match self.range.start.step(&self.step_by) {
Some(n) => {
Some(mem::replace(&mut self.range.start, n))
},
None => {
let last = self.range.start.replace_one();
self.range.end.replace_zero();
self.step_by.replace_one();
Some(last)
},
}
}
else {
None
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match Step::steps_between(&self.range.start,
&self.range.end,
&self.step_by) {
Some(hint) => (hint.saturating_add(1), hint.checked_add(1)),
None => (0, None)
}
}
}

#[unstable(feature = "fused", issue = "35602")]
#[allow(deprecated)]
impl<A: Step + Clone> FusedIterator for StepBy<A, ops::RangeInclusive<A>> {}

macro_rules! range_exact_iter_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
42 changes: 16 additions & 26 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ use core::iter::*;
use core::{i8, i16, isize};
use core::usize;

// FIXME #27741: This is here to simplify calling Iterator::step_by. Remove
// once Range::step_by is completely gone (not just deprecated).
trait IterEx: Sized {
fn iter_step_by(self, n: usize) -> StepBy<Self>;
}
impl<I:Iterator> IterEx for I {
fn iter_step_by(self, n: usize) -> StepBy<Self> { self.step_by(n) }
}

#[test]
fn test_lt() {
let empty: [isize; 0] = [];
Expand Down Expand Up @@ -76,7 +67,7 @@ fn test_multi_iter() {

#[test]
fn test_counter_from_iter() {
let it = (0..).iter_step_by(5).take(10);
let it = (0..).step_by(5).take(10);
let xs: Vec<isize> = FromIterator::from_iter(it);
assert_eq!(xs, [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
}
Expand All @@ -94,7 +85,7 @@ fn test_iterator_chain() {
}
assert_eq!(i, expected.len());

let ys = (30..).iter_step_by(10).take(4);
let ys = (30..).step_by(10).take(4);
let it = xs.iter().cloned().chain(ys);
let mut i = 0;
for x in it {
Expand Down Expand Up @@ -156,13 +147,13 @@ fn test_iterator_chain_find() {
#[test]
fn test_iterator_step_by() {
// Identity
let mut it = (0..).iter_step_by(1).take(3);
let mut it = (0..).step_by(1).take(3);
assert_eq!(it.next(), Some(0));
assert_eq!(it.next(), Some(1));
assert_eq!(it.next(), Some(2));
assert_eq!(it.next(), None);

let mut it = (0..).iter_step_by(3).take(4);
let mut it = (0..).step_by(3).take(4);
assert_eq!(it.next(), Some(0));
assert_eq!(it.next(), Some(3));
assert_eq!(it.next(), Some(6));
Expand All @@ -173,7 +164,7 @@ fn test_iterator_step_by() {
#[test]
#[should_panic]
fn test_iterator_step_by_zero() {
let mut it = (0..).iter_step_by(0);
let mut it = (0..).step_by(0);
it.next();
}

Expand Down Expand Up @@ -252,7 +243,7 @@ fn test_iterator_step_by_size_hint() {

#[test]
fn test_filter_map() {
let it = (0..).iter_step_by(1).take(10)
let it = (0..).step_by(1).take(10)
.filter_map(|x| if x % 2 == 0 { Some(x*x) } else { None });
assert_eq!(it.collect::<Vec<usize>>(), [0*0, 2*2, 4*4, 6*6, 8*8]);
}
Expand Down Expand Up @@ -654,7 +645,7 @@ fn test_iterator_scan() {
fn test_iterator_flat_map() {
let xs = [0, 3, 6];
let ys = [0, 1, 2, 3, 4, 5, 6, 7, 8];
let it = xs.iter().flat_map(|&x| (x..).iter_step_by(1).take(3));
let it = xs.iter().flat_map(|&x| (x..).step_by(1).take(3));
let mut i = 0;
for x in it {
assert_eq!(x, ys[i]);
Expand All @@ -680,13 +671,13 @@ fn test_inspect() {
#[test]
fn test_cycle() {
let cycle_len = 3;
let it = (0..).iter_step_by(1).take(cycle_len).cycle();
let it = (0..).step_by(1).take(cycle_len).cycle();
assert_eq!(it.size_hint(), (usize::MAX, None));
for (i, x) in it.take(100).enumerate() {
assert_eq!(i % cycle_len, x);
}

let mut it = (0..).iter_step_by(1).take(0).cycle();
let mut it = (0..).step_by(1).take(0).cycle();
assert_eq!(it.size_hint(), (0, Some(0)));
assert_eq!(it.next(), None);
}
Expand Down Expand Up @@ -765,7 +756,7 @@ fn test_iterator_min() {

#[test]
fn test_iterator_size_hint() {
let c = (0..).iter_step_by(1);
let c = (0..).step_by(1);
let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let v2 = &[10, 11, 12];
let vi = v.iter();
Expand Down Expand Up @@ -1090,22 +1081,21 @@ fn test_range_step() {
#![allow(deprecated)]

assert_eq!((0..20).step_by(5).collect::<Vec<isize>>(), [0, 5, 10, 15]);
assert_eq!((20..0).step_by(-5).collect::<Vec<isize>>(), [20, 15, 10, 5]);
assert_eq!((20..0).step_by(-6).collect::<Vec<isize>>(), [20, 14, 8, 2]);
assert_eq!((1..21).rev().step_by(5).collect::<Vec<isize>>(), [20, 15, 10, 5]);
assert_eq!((1..21).rev().step_by(6).collect::<Vec<isize>>(), [20, 14, 8, 2]);
assert_eq!((200..255).step_by(50).collect::<Vec<u8>>(), [200, 250]);
assert_eq!((200..-5).step_by(1).collect::<Vec<isize>>(), []);
assert_eq!((200..200).step_by(1).collect::<Vec<isize>>(), []);

assert_eq!((0..20).step_by(1).size_hint(), (20, Some(20)));
assert_eq!((0..20).step_by(21).size_hint(), (1, Some(1)));
assert_eq!((0..20).step_by(5).size_hint(), (4, Some(4)));
assert_eq!((20..0).step_by(-5).size_hint(), (4, Some(4)));
assert_eq!((20..0).step_by(-6).size_hint(), (4, Some(4)));
assert_eq!((1..21).rev().step_by(5).size_hint(), (4, Some(4)));
assert_eq!((1..21).rev().step_by(6).size_hint(), (4, Some(4)));
assert_eq!((20..-5).step_by(1).size_hint(), (0, Some(0)));
assert_eq!((20..20).step_by(1).size_hint(), (0, Some(0)));
assert_eq!((0..1).step_by(0).size_hint(), (0, None));
assert_eq!((i8::MAX..i8::MIN).step_by(i8::MIN).size_hint(), (2, Some(2)));
assert_eq!((i16::MIN..i16::MAX).step_by(i16::MAX).size_hint(), (3, Some(3)));
assert_eq!((i8::MIN..i8::MAX).step_by(-(i8::MIN as i32) as usize).size_hint(), (2, Some(2)));
assert_eq!((i16::MIN..i16::MAX).step_by(i16::MAX as usize).size_hint(), (3, Some(3)));
assert_eq!((isize::MIN..isize::MAX).step_by(1).size_hint(), (usize::MAX, Some(usize::MAX)));
}

Expand Down
1 change: 0 additions & 1 deletion src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#![feature(slice_rotate)]
#![feature(sort_internals)]
#![feature(specialization)]
#![feature(step_by)]
#![feature(step_trait)]
#![feature(test)]
#![feature(trusted_len)]
Expand Down
2 changes: 1 addition & 1 deletion src/librand/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
issue = "27703")]
#![feature(core_intrinsics)]
#![feature(staged_api)]
#![feature(step_by)]
#![feature(iterator_step_by)]
#![feature(custom_attribute)]
#![feature(specialization)]
#![allow(unused_attributes)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/range_inclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// Test inclusive range syntax.

#![feature(inclusive_range_syntax, inclusive_range, step_by)]
#![feature(inclusive_range_syntax, inclusive_range, iterator_step_by)]

use std::ops::{RangeInclusive, RangeToInclusive};

Expand Down
Loading

0 comments on commit 7a75d2b

Please sign in to comment.