Skip to content

Commit bde09ee

Browse files
committed
Auto merge of #23347 - aturon:stab-misc, r=alexcrichton
This commit deprecates the `count`, `range` and `range_step` functions in `iter`, in favor of range notation. To recover all existing functionality, a new `step_by` adapter is provided directly on `ops::Range` and `ops::RangeFrom`. [breaking-change] r? @alexcrichton
2 parents cc78919 + 1d5983a commit bde09ee

File tree

16 files changed

+134
-93
lines changed

16 files changed

+134
-93
lines changed

Diff for: src/libcollections/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![feature(unsafe_destructor)]
3434
#![feature(unique)]
3535
#![feature(unsafe_no_drop_flag)]
36+
#![feature(step_by)]
3637
#![cfg_attr(test, feature(rand, rustc_private, test))]
3738
#![cfg_attr(test, allow(deprecated))] // rand
3839

Diff for: src/libcollections/slice.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ use core::clone::Clone;
9292
use core::cmp::Ordering::{self, Greater, Less};
9393
use core::cmp::{self, Ord, PartialEq};
9494
use core::iter::{Iterator, IteratorExt};
95-
use core::iter::{range_step, MultiplicativeIterator};
95+
use core::iter::MultiplicativeIterator;
9696
use core::marker::Sized;
9797
use core::mem::size_of;
9898
use core::mem;
@@ -1387,7 +1387,7 @@ fn merge_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order
13871387
// We could hardcode the sorting comparisons here, and we could
13881388
// manipulate/step the pointers themselves, rather than repeatedly
13891389
// .offset-ing.
1390-
for start in range_step(0, len, insertion) {
1390+
for start in (0.. len).step_by(insertion) {
13911391
// start <= i < len;
13921392
for i in start..cmp::min(start + insertion, len) {
13931393
// j satisfies: start <= j <= i;
@@ -1427,7 +1427,7 @@ fn merge_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order
14271427
// a time, placing the result in `buf_tmp`.
14281428

14291429
// 0 <= start <= len.
1430-
for start in range_step(0, len, 2 * width) {
1430+
for start in (0..len).step_by(2 * width) {
14311431
// manipulate pointers directly for speed (rather than
14321432
// using a `for` loop with `range` and `.offset` inside
14331433
// that loop).

Diff for: src/libcore/iter.rs

+113-69
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use default::Default;
6565
use marker;
6666
use mem;
6767
use num::{ToPrimitive, Int};
68-
use ops::{Add, Deref, FnMut};
68+
use ops::{Add, Deref, FnMut, RangeFrom};
6969
use option::Option;
7070
use option::Option::{Some, None};
7171
use marker::Sized;
@@ -2366,34 +2366,101 @@ impl<A, St, F> Iterator for Unfold<St, F> where F: FnMut(&mut St) -> Option<A> {
23662366
}
23672367
}
23682368

2369+
/// An adapter for stepping range iterators by a custom amount.
2370+
///
2371+
/// The resulting iterator handles overflow by stopping. The `A`
2372+
/// parameter is the type being iterated over, while `R` is the range
2373+
/// type (usually one of `std::ops::{Range, RangeFrom}`.
2374+
#[derive(Clone)]
2375+
#[unstable(feature = "step_by", reason = "recent addition")]
2376+
pub struct StepBy<A, R> {
2377+
step_by: A,
2378+
range: R,
2379+
}
2380+
2381+
impl<A: Add> RangeFrom<A> {
2382+
/// Creates an iterator starting at the same point, but stepping by
2383+
/// the given amount at each iteration.
2384+
///
2385+
/// # Examples
2386+
///
2387+
/// ```ignore
2388+
/// for i in (0u8..).step_by(2) {
2389+
/// println!("{}", i);
2390+
/// }
2391+
/// ```
2392+
///
2393+
/// This prints all even `u8` values.
2394+
#[unstable(feature = "step_by", reason = "recent addition")]
2395+
pub fn step_by(self, by: A) -> StepBy<A, Self> {
2396+
StepBy {
2397+
step_by: by,
2398+
range: self
2399+
}
2400+
}
2401+
}
2402+
2403+
impl<A: Int> ::ops::Range<A> {
2404+
/// Creates an iterator with the same range, but stepping by the
2405+
/// given amount at each iteration.
2406+
///
2407+
/// The resulting iterator handles overflow by stopping.
2408+
///
2409+
/// # Examples
2410+
///
2411+
/// ```
2412+
/// # #![feature(step_by, core)]
2413+
/// for i in (0..10).step_by(2) {
2414+
/// println!("{}", i);
2415+
/// }
2416+
/// ```
2417+
///
2418+
/// This prints:
2419+
///
2420+
/// ```text
2421+
/// 0
2422+
/// 2
2423+
/// 4
2424+
/// 6
2425+
/// 8
2426+
/// ```
2427+
#[unstable(feature = "step_by", reason = "recent addition")]
2428+
pub fn step_by(self, by: A) -> StepBy<A, Self> {
2429+
StepBy {
2430+
step_by: by,
2431+
range: self
2432+
}
2433+
}
2434+
}
2435+
23692436
/// An infinite iterator starting at `start` and advancing by `step` with each
23702437
/// iteration
2371-
#[derive(Clone)]
23722438
#[unstable(feature = "core",
23732439
reason = "may be renamed or replaced by range notation adapters")]
2374-
pub struct Counter<A> {
2375-
/// The current state the counter is at (next value to be yielded)
2376-
state: A,
2377-
/// The amount that this iterator is stepping by
2378-
step: A,
2379-
}
2440+
#[deprecated(since = "1.0.0-beta", reason = "use range notation and step_by")]
2441+
pub type Counter<A> = StepBy<A, RangeFrom<A>>;
23802442

2381-
/// Creates a new counter with the specified start/step
2443+
/// Deprecated: use `(start..).step_by(step)` instead.
23822444
#[inline]
23832445
#[unstable(feature = "core",
23842446
reason = "may be renamed or replaced by range notation adapters")]
2447+
#[deprecated(since = "1.0.0-beta", reason = "use (start..).step_by(step) instead")]
2448+
#[allow(deprecated)]
23852449
pub fn count<A>(start: A, step: A) -> Counter<A> {
2386-
Counter{state: start, step: step}
2450+
StepBy {
2451+
range: RangeFrom { start: start },
2452+
step_by: step,
2453+
}
23872454
}
23882455

23892456
#[stable(feature = "rust1", since = "1.0.0")]
2390-
impl<A: Add<Output=A> + Clone> Iterator for Counter<A> {
2457+
impl<A: Add<Output=A> + Clone> Iterator for StepBy<A, RangeFrom<A>> {
23912458
type Item = A;
23922459

23932460
#[inline]
23942461
fn next(&mut self) -> Option<A> {
2395-
let result = self.state.clone();
2396-
self.state = self.state.clone() + self.step.clone();
2462+
let result = self.range.start.clone();
2463+
self.range.start = result.clone() + self.step_by.clone();
23972464
Some(result)
23982465
}
23992466

@@ -2404,31 +2471,22 @@ impl<A: Add<Output=A> + Clone> Iterator for Counter<A> {
24042471
}
24052472

24062473
/// An iterator over the range [start, stop)
2474+
#[allow(deprecated)]
24072475
#[derive(Clone)]
24082476
#[unstable(feature = "core",
24092477
reason = "will be replaced by range notation")]
2478+
#[deprecated(since = "1.0.0-beta", reason = "use range notation")]
24102479
pub struct Range<A> {
24112480
state: A,
24122481
stop: A,
24132482
one: A,
24142483
}
24152484

2416-
/// Returns an iterator over the given range [start, stop) (that is, starting
2417-
/// at start (inclusive), and ending at stop (exclusive)).
2418-
///
2419-
/// # Examples
2420-
///
2421-
/// ```
2422-
/// let array = [0, 1, 2, 3, 4];
2423-
///
2424-
/// for i in range(0, 5) {
2425-
/// println!("{}", i);
2426-
/// assert_eq!(i, array[i]);
2427-
/// }
2428-
/// ```
2485+
/// Deprecated: use `(start..stop)` instead.
24292486
#[inline]
2430-
#[unstable(feature = "core",
2431-
reason = "will be replaced by range notation")]
2487+
#[unstable(feature = "core", reason = "will be replaced by range notation")]
2488+
#[deprecated(since = "1.0.0-beta", reason = "use (start..stop) instead")]
2489+
#[allow(deprecated)]
24322490
pub fn range<A: Int>(start: A, stop: A) -> Range<A> {
24332491
Range {
24342492
state: start,
@@ -2440,6 +2498,8 @@ pub fn range<A: Int>(start: A, stop: A) -> Range<A> {
24402498
// FIXME: #10414: Unfortunate type bound
24412499
#[unstable(feature = "core",
24422500
reason = "will be replaced by range notation")]
2501+
#[deprecated(since = "1.0.0-beta", reason = "use range notation")]
2502+
#[allow(deprecated)]
24432503
impl<A: Int + ToPrimitive> Iterator for Range<A> {
24442504
type Item = A;
24452505

@@ -2491,6 +2551,8 @@ impl<A: Int + ToPrimitive> Iterator for Range<A> {
24912551
/// the direction it is consumed.
24922552
#[unstable(feature = "core",
24932553
reason = "will be replaced by range notation")]
2554+
#[deprecated(since = "1.0.0-beta", reason = "use range notation")]
2555+
#[allow(deprecated)]
24942556
impl<A: Int + ToPrimitive> DoubleEndedIterator for Range<A> {
24952557
#[inline]
24962558
fn next_back(&mut self) -> Option<A> {
@@ -2507,6 +2569,7 @@ impl<A: Int + ToPrimitive> DoubleEndedIterator for Range<A> {
25072569
#[derive(Clone)]
25082570
#[unstable(feature = "core",
25092571
reason = "likely to be replaced by range notation and adapters")]
2572+
#[allow(deprecated)]
25102573
pub struct RangeInclusive<A> {
25112574
range: Range<A>,
25122575
done: bool,
@@ -2516,6 +2579,7 @@ pub struct RangeInclusive<A> {
25162579
#[inline]
25172580
#[unstable(feature = "core",
25182581
reason = "likely to be replaced by range notation and adapters")]
2582+
#[allow(deprecated)]
25192583
pub fn range_inclusive<A: Int>(start: A, stop: A) -> RangeInclusive<A> {
25202584
RangeInclusive {
25212585
range: range(start, stop),
@@ -2525,6 +2589,7 @@ pub fn range_inclusive<A: Int>(start: A, stop: A) -> RangeInclusive<A> {
25252589

25262590
#[unstable(feature = "core",
25272591
reason = "likely to be replaced by range notation and adapters")]
2592+
#[allow(deprecated)]
25282593
impl<A: Int + ToPrimitive> Iterator for RangeInclusive<A> {
25292594
type Item = A;
25302595

@@ -2561,6 +2626,7 @@ impl<A: Int + ToPrimitive> Iterator for RangeInclusive<A> {
25612626

25622627
#[unstable(feature = "core",
25632628
reason = "likely to be replaced by range notation and adapters")]
2629+
#[allow(deprecated)]
25642630
impl<A: Int + ToPrimitive> DoubleEndedIterator for RangeInclusive<A> {
25652631
#[inline]
25662632
fn next_back(&mut self) -> Option<A> {
@@ -2578,61 +2644,39 @@ impl<A: Int + ToPrimitive> DoubleEndedIterator for RangeInclusive<A> {
25782644
}
25792645

25802646
/// An iterator over the range [start, stop) by `step`. It handles overflow by stopping.
2581-
#[derive(Clone)]
25822647
#[unstable(feature = "core",
25832648
reason = "likely to be replaced by range notation and adapters")]
2584-
pub struct RangeStep<A> {
2585-
state: A,
2586-
stop: A,
2587-
step: A,
2588-
rev: bool,
2589-
}
2649+
#[deprecated(since = "1.0.0-beta", reason = "use range notation and step_by")]
2650+
pub type RangeStep<A> = StepBy<A, ::ops::Range<A>>;
25902651

2591-
/// Return an iterator over the range [start, stop) by `step`.
2592-
///
2593-
/// It handles overflow by stopping.
2594-
///
2595-
/// # Examples
2596-
///
2597-
/// ```
2598-
/// use std::iter::range_step;
2599-
///
2600-
/// for i in range_step(0, 10, 2) {
2601-
/// println!("{}", i);
2602-
/// }
2603-
/// ```
2604-
///
2605-
/// This prints:
2606-
///
2607-
/// ```text
2608-
/// 0
2609-
/// 2
2610-
/// 4
2611-
/// 6
2612-
/// 8
2613-
/// ```
2652+
/// Deprecated: use `(start..stop).step_by(step)` instead.
26142653
#[inline]
26152654
#[unstable(feature = "core",
26162655
reason = "likely to be replaced by range notation and adapters")]
2656+
#[deprecated(since = "1.0.0-beta",
2657+
reason = "use `(start..stop).step_by(step)` instead")]
2658+
#[allow(deprecated)]
26172659
pub fn range_step<A: Int>(start: A, stop: A, step: A) -> RangeStep<A> {
2618-
let rev = step < Int::zero();
2619-
RangeStep{state: start, stop: stop, step: step, rev: rev}
2660+
StepBy {
2661+
step_by: step,
2662+
range: ::ops::Range { start: start, end: stop },
2663+
}
26202664
}
26212665

2622-
#[unstable(feature = "core",
2623-
reason = "likely to be replaced by range notation and adapters")]
2624-
impl<A: Int> Iterator for RangeStep<A> {
2666+
#[stable(feature = "rust1", since = "1.0.0")]
2667+
impl<A: Int> Iterator for StepBy<A, ::ops::Range<A>> {
26252668
type Item = A;
26262669

26272670
#[inline]
26282671
fn next(&mut self) -> Option<A> {
2629-
if (self.rev && self.state > self.stop) || (!self.rev && self.state < self.stop) {
2630-
let result = self.state;
2631-
match self.state.checked_add(self.step) {
2632-
Some(x) => self.state = x,
2633-
None => self.state = self.stop.clone()
2672+
let rev = self.step_by < Int::zero();
2673+
let start = self.range.start;
2674+
if (rev && start > self.range.end) || (!rev && start < self.range.end) {
2675+
match start.checked_add(self.step_by) {
2676+
Some(x) => self.range.start = x,
2677+
None => self.range.start = self.range.end.clone()
26342678
}
2635-
Some(result)
2679+
Some(start)
26362680
} else {
26372681
None
26382682
}

Diff for: src/libcore/prelude.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub use marker::{Copy, Send, Sized, Sync};
2929
pub use ops::{Drop, Fn, FnMut, FnOnce};
3030

3131
// Reexported functions
32+
#[allow(deprecated)]
3233
pub use iter::range;
3334
pub use mem::drop;
3435

Diff for: src/libcoretest/iter.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -775,12 +775,12 @@ fn test_range_inclusive() {
775775

776776
#[test]
777777
fn test_range_step() {
778-
assert_eq!(range_step(0, 20, 5).collect::<Vec<int>>(), [0, 5, 10, 15]);
779-
assert_eq!(range_step(20, 0, -5).collect::<Vec<int>>(), [20, 15, 10, 5]);
780-
assert_eq!(range_step(20, 0, -6).collect::<Vec<int>>(), [20, 14, 8, 2]);
781-
assert_eq!(range_step(200, 255, 50).collect::<Vec<u8>>(), [200, 250]);
782-
assert_eq!(range_step(200, -5, 1).collect::<Vec<int>>(), []);
783-
assert_eq!(range_step(200, 200, 1).collect::<Vec<int>>(), []);
778+
assert_eq!((0..20).step_by(5).collect::<Vec<int>>(), [0, 5, 10, 15]);
779+
assert_eq!((20..0).step_by(-5).collect::<Vec<int>>(), [20, 15, 10, 5]);
780+
assert_eq!((20..0).step_by(-6).collect::<Vec<int>>(), [20, 14, 8, 2]);
781+
assert_eq!((200..255).step_by(50).collect::<Vec<u8>>(), [200, 250]);
782+
assert_eq!((200..-5).step_by(1).collect::<Vec<int>>(), []);
783+
assert_eq!((200..200).step_by(1).collect::<Vec<int>>(), []);
784784
}
785785

786786
#[test]

Diff for: src/librand/distributions/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
//! internally. The `IndependentSample` trait is for generating values
1818
//! that do not need to record state.
1919
20-
#![unstable(feature = "rand")]
21-
2220
use core::prelude::*;
2321
use core::num::{Float, Int};
2422
use core::marker::PhantomData;

Diff for: src/librustc/middle/infer/combine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub trait Combine<'tcx> : Sized {
154154
b_tys.len())));
155155
}
156156

157-
range(0, a_tys.len()).map(|i| {
157+
(0.. a_tys.len()).map(|i| {
158158
let a_ty = a_tys[i];
159159
let b_ty = b_tys[i];
160160
let v = variances.map_or(ty::Invariant, |v| v[i]);

Diff for: src/librustc_back/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#![feature(path_ext)]
4949
#![feature(std_misc)]
5050
#![feature(path_relative_from)]
51+
#![feature(step_by)]
5152

5253
extern crate syntax;
5354
extern crate serialize;

Diff for: src/librustc_back/svh.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
4949
use std::fmt;
5050
use std::hash::{Hash, SipHasher, Hasher};
51-
use std::iter::range_step;
5251
use syntax::ast;
5352
use syntax::visit;
5453

@@ -103,7 +102,7 @@ impl Svh {
103102

104103
let hash = state.finish();
105104
return Svh {
106-
hash: range_step(0, 64, 4).map(|i| hex(hash >> i)).collect()
105+
hash: (0..64).step_by(4).map(|i| hex(hash >> i)).collect()
107106
};
108107

109108
fn hex(b: u64) -> char {

0 commit comments

Comments
 (0)