@@ -65,7 +65,7 @@ use default::Default;
6565use marker;
6666use mem;
6767use num:: { ToPrimitive , Int } ;
68- use ops:: { Add , Deref , FnMut } ;
68+ use ops:: { Add , Deref , FnMut , RangeFrom } ;
6969use option:: Option ;
7070use option:: Option :: { Some , None } ;
7171use 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) ]
23852449pub 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" ) ]
24102479pub 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) ]
24322490pub 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) ]
24432503impl < 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) ]
24942556impl < 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) ]
25102573pub 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) ]
25192583pub 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) ]
25282593impl < 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) ]
25642630impl < 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) ]
26172659pub 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 }
0 commit comments