@@ -2108,19 +2108,18 @@ impl<A, I, F> RandomAccessIterator<A> for Inspect<A, I, F> where
2108
2108
/// }
2109
2109
/// ```
2110
2110
#[ experimental]
2111
- pub struct Unfold < ' a , A , St > {
2112
- f: | & mut St | : ' a -> Option < A > ,
2111
+ pub struct Unfold < A , St , F > where F : FnMut ( & mut St ) -> Option < A > {
2112
+ f : F ,
2113
2113
/// Internal state that will be passed to the closure on the next iteration
2114
2114
pub state : St ,
2115
2115
}
2116
2116
2117
2117
#[ experimental]
2118
- impl < ' a , A , St > Unfold < ' a , A , St > {
2118
+ impl < A , St , F > Unfold < A , St , F > where F : FnMut ( & mut St ) -> Option < A > {
2119
2119
/// Creates a new iterator with the specified closure as the "iterator
2120
2120
/// function" and an initial state to eventually pass to the closure
2121
2121
#[ inline]
2122
- pub fn new < ' a > ( initial_state : St , f: |& mut St |: ' a -> Option <A >)
2123
- -> Unfold <' a, A , St > {
2122
+ pub fn new ( initial_state : St , f : F ) -> Unfold < A , St , F > {
2124
2123
Unfold {
2125
2124
f : f,
2126
2125
state : initial_state
@@ -2129,7 +2128,7 @@ impl<'a, A, St> Unfold<'a, A, St> {
2129
2128
}
2130
2129
2131
2130
#[ experimental]
2132
- impl <' a , A , St > Iterator < A > for Unfold < ' a , A , St > {
2131
+ impl < A , St , F > Iterator < A > for Unfold < A , St , F > where F : FnMut ( & mut St ) -> Option < A > {
2133
2132
#[ inline]
2134
2133
fn next ( & mut self ) -> Option < A > {
2135
2134
( self . f ) ( & mut self . state )
@@ -2456,18 +2455,24 @@ impl<A: Clone> RandomAccessIterator<A> for Repeat<A> {
2456
2455
fn idx ( & mut self , _: uint ) -> Option < A > { Some ( self . element . clone ( ) ) }
2457
2456
}
2458
2457
2459
- type IterateState < ' a , T > = ( | T | : ' a -> T , Option <T >, bool ) ;
2458
+ type IterateState < T , F > = ( F , Option < T > , bool ) ;
2460
2459
2461
2460
/// An iterator that repeatedly applies a given function, starting
2462
2461
/// from a given seed value.
2463
2462
#[ experimental]
2464
- pub type Iterate < ' a , T > = Unfold < ' a , T , IterateState < ' a , T > > ;
2463
+ pub type Iterate < T , F > = Unfold < T , IterateState < T , F > , fn ( & mut IterateState < T , F > ) -> Option < T > > ;
2465
2464
2466
2465
/// Create a new iterator that produces an infinite sequence of
2467
2466
/// repeated applications of the given function `f`.
2468
2467
#[ experimental]
2469
- pub fn iterate < ' a , T : Clone > ( seed : T , f: |T |: ' a -> T ) -> Iterate <' a, T > {
2470
- Unfold :: new ( ( f , Some ( seed ) , true ) , |st| {
2468
+ pub fn iterate < T , F > ( seed : T , f : F ) -> Iterate < T , F > where
2469
+ T : Clone ,
2470
+ F : FnMut ( T ) -> T ,
2471
+ {
2472
+ fn next < T , F > ( st : & mut IterateState < T , F > ) -> Option < T > where
2473
+ T : Clone ,
2474
+ F : FnMut ( T ) -> T ,
2475
+ {
2471
2476
let & ( ref mut f, ref mut val, ref mut first) = st;
2472
2477
if * first {
2473
2478
* first = false ;
@@ -2480,7 +2485,9 @@ pub fn iterate<'a, T: Clone>(seed: T, f: |T|: 'a -> T) -> Iterate<'a, T> {
2480
2485
}
2481
2486
}
2482
2487
val. clone ( )
2483
- } )
2488
+ }
2489
+
2490
+ Unfold :: new ( ( f, Some ( seed) , true ) , next)
2484
2491
}
2485
2492
2486
2493
/// Create a new iterator that endlessly repeats the element `elt`.
0 commit comments