@@ -65,7 +65,7 @@ use default::Default;
65
65
use marker;
66
66
use mem;
67
67
use num:: { ToPrimitive , Int } ;
68
- use ops:: { Add , Deref , FnMut } ;
68
+ use ops:: { Add , Deref , FnMut , RangeFrom } ;
69
69
use option:: Option ;
70
70
use option:: Option :: { Some , None } ;
71
71
use marker:: Sized ;
@@ -2366,34 +2366,101 @@ impl<A, St, F> Iterator for Unfold<St, F> where F: FnMut(&mut St) -> Option<A> {
2366
2366
}
2367
2367
}
2368
2368
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
+
2369
2436
/// An infinite iterator starting at `start` and advancing by `step` with each
2370
2437
/// iteration
2371
- #[ derive( Clone ) ]
2372
2438
#[ unstable( feature = "core" ,
2373
2439
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 > > ;
2380
2442
2381
- /// Creates a new counter with the specified start/ step
2443
+ /// Deprecated: use `( start..).step_by( step)` instead.
2382
2444
#[ inline]
2383
2445
#[ unstable( feature = "core" ,
2384
2446
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) ]
2385
2449
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
+ }
2387
2454
}
2388
2455
2389
2456
#[ 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 > > {
2391
2458
type Item = A ;
2392
2459
2393
2460
#[ inline]
2394
2461
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 ( ) ;
2397
2464
Some ( result)
2398
2465
}
2399
2466
@@ -2404,31 +2471,22 @@ impl<A: Add<Output=A> + Clone> Iterator for Counter<A> {
2404
2471
}
2405
2472
2406
2473
/// An iterator over the range [start, stop)
2474
+ #[ allow( deprecated) ]
2407
2475
#[ derive( Clone ) ]
2408
2476
#[ unstable( feature = "core" ,
2409
2477
reason = "will be replaced by range notation" ) ]
2478
+ #[ deprecated( since = "1.0.0-beta" , reason = "use range notation" ) ]
2410
2479
pub struct Range < A > {
2411
2480
state : A ,
2412
2481
stop : A ,
2413
2482
one : A ,
2414
2483
}
2415
2484
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.
2429
2486
#[ 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) ]
2432
2490
pub fn range < A : Int > ( start : A , stop : A ) -> Range < A > {
2433
2491
Range {
2434
2492
state : start,
@@ -2440,6 +2498,8 @@ pub fn range<A: Int>(start: A, stop: A) -> Range<A> {
2440
2498
// FIXME: #10414: Unfortunate type bound
2441
2499
#[ unstable( feature = "core" ,
2442
2500
reason = "will be replaced by range notation" ) ]
2501
+ #[ deprecated( since = "1.0.0-beta" , reason = "use range notation" ) ]
2502
+ #[ allow( deprecated) ]
2443
2503
impl < A : Int + ToPrimitive > Iterator for Range < A > {
2444
2504
type Item = A ;
2445
2505
@@ -2491,6 +2551,8 @@ impl<A: Int + ToPrimitive> Iterator for Range<A> {
2491
2551
/// the direction it is consumed.
2492
2552
#[ unstable( feature = "core" ,
2493
2553
reason = "will be replaced by range notation" ) ]
2554
+ #[ deprecated( since = "1.0.0-beta" , reason = "use range notation" ) ]
2555
+ #[ allow( deprecated) ]
2494
2556
impl < A : Int + ToPrimitive > DoubleEndedIterator for Range < A > {
2495
2557
#[ inline]
2496
2558
fn next_back ( & mut self ) -> Option < A > {
@@ -2507,6 +2569,7 @@ impl<A: Int + ToPrimitive> DoubleEndedIterator for Range<A> {
2507
2569
#[ derive( Clone ) ]
2508
2570
#[ unstable( feature = "core" ,
2509
2571
reason = "likely to be replaced by range notation and adapters" ) ]
2572
+ #[ allow( deprecated) ]
2510
2573
pub struct RangeInclusive < A > {
2511
2574
range : Range < A > ,
2512
2575
done : bool ,
@@ -2516,6 +2579,7 @@ pub struct RangeInclusive<A> {
2516
2579
#[ inline]
2517
2580
#[ unstable( feature = "core" ,
2518
2581
reason = "likely to be replaced by range notation and adapters" ) ]
2582
+ #[ allow( deprecated) ]
2519
2583
pub fn range_inclusive < A : Int > ( start : A , stop : A ) -> RangeInclusive < A > {
2520
2584
RangeInclusive {
2521
2585
range : range ( start, stop) ,
@@ -2525,6 +2589,7 @@ pub fn range_inclusive<A: Int>(start: A, stop: A) -> RangeInclusive<A> {
2525
2589
2526
2590
#[ unstable( feature = "core" ,
2527
2591
reason = "likely to be replaced by range notation and adapters" ) ]
2592
+ #[ allow( deprecated) ]
2528
2593
impl < A : Int + ToPrimitive > Iterator for RangeInclusive < A > {
2529
2594
type Item = A ;
2530
2595
@@ -2561,6 +2626,7 @@ impl<A: Int + ToPrimitive> Iterator for RangeInclusive<A> {
2561
2626
2562
2627
#[ unstable( feature = "core" ,
2563
2628
reason = "likely to be replaced by range notation and adapters" ) ]
2629
+ #[ allow( deprecated) ]
2564
2630
impl < A : Int + ToPrimitive > DoubleEndedIterator for RangeInclusive < A > {
2565
2631
#[ inline]
2566
2632
fn next_back ( & mut self ) -> Option < A > {
@@ -2578,61 +2644,39 @@ impl<A: Int + ToPrimitive> DoubleEndedIterator for RangeInclusive<A> {
2578
2644
}
2579
2645
2580
2646
/// An iterator over the range [start, stop) by `step`. It handles overflow by stopping.
2581
- #[ derive( Clone ) ]
2582
2647
#[ unstable( feature = "core" ,
2583
2648
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 > > ;
2590
2651
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.
2614
2653
#[ inline]
2615
2654
#[ unstable( feature = "core" ,
2616
2655
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) ]
2617
2659
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
+ }
2620
2664
}
2621
2665
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 > > {
2625
2668
type Item = A ;
2626
2669
2627
2670
#[ inline]
2628
2671
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 ( )
2634
2678
}
2635
- Some ( result )
2679
+ Some ( start )
2636
2680
} else {
2637
2681
None
2638
2682
}
0 commit comments