Skip to content

Commit

Permalink
Remove deprecated Range::step_by (use Iterator::step_by instead)
Browse files Browse the repository at this point in the history
Follow-up to rust-lang#41439 (comment)

While doing so, remove the now-unused `step`, `steps_between`, and `is_negative` methods from `Step`.

Mostly simple, but needed two interesting changes:
* Override `Iterator::size_hint` for `iter::StepBy` (so hints aren't lost)
* Override `Iterator::size_hint` for `ops::RangeFrom` (so `(0..).size_hint()` returns what `(0..).step_by(1).size_hint()` used to)
(It turns out that `(0..).step_by(d)` is used in a bunch of tests, from `cycle` to `vec_deque`.)

Incidentally fixes rust-lang#41477
  • Loading branch information
scottmcm committed May 20, 2017
1 parent 0bd9e1f commit 9347756
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 350 deletions.
1 change: 0 additions & 1 deletion src/doc/unstable-book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@
- [sort_internals](library-features/sort-internals.md)
- [sort_unstable](library-features/sort-unstable.md)
- [splice](library-features/splice.md)
- [step_by](library-features/step-by.md)
- [step_trait](library-features/step-trait.md)
- [str_checked_slicing](library-features/str-checked-slicing.md)
- [str_escape](library-features/str-escape.md)
Expand Down
7 changes: 0 additions & 7 deletions src/doc/unstable-book/src/library-features/step-by.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/libcollections/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#![feature(placement_in_syntax)]
#![feature(rand)]
#![feature(splice)]
#![feature(step_by)]
#![feature(iterator_step_by)]
#![feature(str_escape)]
#![feature(test)]
#![feature(unboxed_closures)]
Expand Down
3 changes: 2 additions & 1 deletion src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ pub trait Iterator {
///
/// ```
/// // an infinite iterator has no upper bound
/// // and the maximum possible lower bound
/// let iter = 0..;
///
/// assert_eq!((0, None), iter.size_hint());
/// assert_eq!((usize::max_value(), None), iter.size_hint());
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
16 changes: 13 additions & 3 deletions src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,6 @@ pub use self::iterator::Iterator;
reason = "likely to be replaced by finer-grained traits",
issue = "27741")]
pub use self::range::Step;
#[unstable(feature = "step_by", reason = "recent addition",
issue = "27741")]
pub use self::range::StepBy as DeprecatedStepBy;

#[stable(feature = "rust1", since = "1.0.0")]
pub use self::sources::{Repeat, repeat};
Expand Down Expand Up @@ -553,6 +550,19 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
self.iter.nth(self.step)
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let inner_hint = self.iter.size_hint();

if self.first_take {
let f = |n| if n == 0 { 0 } else { 1 + (n-1)/(self.step+1) };
(f(inner_hint.0), inner_hint.1.map(f))
} else {
let f = |n| n / (self.step+1);
(f(inner_hint.0), inner_hint.1.map(f))
}
}
}

/// An iterator that strings two iterators together.
Expand Down
Loading

0 comments on commit 9347756

Please sign in to comment.