@@ -312,6 +312,19 @@ impl<A, T: Iterator<A>> Iterator<A> for TakeIterator<T> {
312312 }
313313}
314314
315+ pub struct ScanIterator < ' self , A , B , T , St > {
316+ priv iter : T ,
317+ priv f: & ' self fn ( & mut St , A ) -> Option < B > ,
318+ state : St
319+ }
320+
321+ impl < ' self , A , B , T : Iterator < A > , St > Iterator < B > for ScanIterator < ' self , A , B , T , St > {
322+ #[ inline]
323+ fn next ( & mut self ) -> Option < B > {
324+ self . iter . next ( ) . chain ( |a| ( self . f ) ( & mut self . state , a) )
325+ }
326+ }
327+
315328pub struct UnfoldrIterator < ' self , A , St > {
316329 priv f: & ' self fn ( & mut St ) -> Option < A > ,
317330 state : St
@@ -335,16 +348,25 @@ impl<'self, A, St> Iterator<A> for UnfoldrIterator<'self, A, St> {
335348 }
336349}
337350
338- pub struct ScanIterator < ' self , A , B , T , St > {
339- priv iter : T ,
340- priv f : & ' self fn ( & mut St , A ) -> Option < B > ,
341- state : St
351+ /// An infinite iterator starting at `start` and advancing by `step` with each iteration
352+ pub struct Counter < A > {
353+ state : A ,
354+ step : A
342355}
343356
344- impl < ' self , A , B , T : Iterator < A > , St > Iterator < B > for ScanIterator < ' self , A , B , T , St > {
345- #[ inline]
346- fn next ( & mut self ) -> Option < B > {
347- self . iter . next ( ) . chain ( |a| ( self . f ) ( & mut self . state , a) )
357+ pub impl < A > Counter < A > {
358+ #[ inline( always) ]
359+ fn new ( start : A , step : A ) -> Counter < A > {
360+ Counter { state : start, step : step}
361+ }
362+ }
363+
364+ impl < A : Add < A , A > + Clone > Iterator < A > for Counter < A > {
365+ #[ inline( always) ]
366+ fn next ( & mut self ) -> Option < A > {
367+ let result = self . state . clone ( ) ;
368+ self . state = self . state . add ( & self . step ) ; // FIXME: #6050
369+ Some ( result)
348370 }
349371}
350372
@@ -353,6 +375,13 @@ mod tests {
353375 use super :: * ;
354376 use prelude:: * ;
355377
378+ #[ test]
379+ fn test_counter_to_vec ( ) {
380+ let mut it = Counter :: new ( 0 , 5 ) . take ( 10 ) ;
381+ let xs = iter:: iter_to_vec ( |f| it. advance ( f) ) ;
382+ assert_eq ! ( xs, ~[ 0 , 5 , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45 ] ) ;
383+ }
384+
356385 #[ test]
357386 fn test_iterator_chain ( ) {
358387 let xs = [ 0 u, 1 , 2 , 3 , 4 , 5 ] ;
0 commit comments