File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ pub use sources::{from_fn, FromFn};
53
53
pub use sources:: { once, Once } ;
54
54
pub use sources:: { once_with, OnceWith } ;
55
55
pub use sources:: { repeat, Repeat } ;
56
+ pub use sources:: { repeat_with, RepeatWith } ;
56
57
57
58
/// An interface for dealing with streaming iterators.
58
59
pub trait StreamingIterator {
Original file line number Diff line number Diff line change @@ -73,6 +73,12 @@ pub fn repeat<T>(item: T) -> Repeat<T> {
73
73
Repeat { item }
74
74
}
75
75
76
+ /// Creates an iterator that endlessly returns items from a function call.
77
+ #[ inline]
78
+ pub fn repeat_with < T , F : FnMut ( ) -> T > ( gen : F ) -> RepeatWith < T , F > {
79
+ RepeatWith { gen, item : None }
80
+ }
81
+
76
82
/// A streaming iterator which yields elements from a normal, non-streaming, iterator.
77
83
#[ derive( Clone , Debug ) ]
78
84
pub struct Convert < I >
@@ -355,3 +361,29 @@ impl<T> DoubleEndedStreamingIterator for Repeat<T> {
355
361
#[ inline]
356
362
fn advance_back ( & mut self ) { }
357
363
}
364
+
365
+ /// A simple iterator that endlessly returns items from a function call.
366
+ #[ derive( Clone , Debug ) ]
367
+ pub struct RepeatWith < T , F > {
368
+ gen : F ,
369
+ item : Option < T > ,
370
+ }
371
+
372
+ impl < T , F : FnMut ( ) -> T > StreamingIterator for RepeatWith < T , F > {
373
+ type Item = T ;
374
+
375
+ #[ inline]
376
+ fn advance ( & mut self ) {
377
+ self . item = Some ( ( self . gen ) ( ) ) ;
378
+ }
379
+
380
+ #[ inline]
381
+ fn get ( & self ) -> Option < & Self :: Item > {
382
+ self . item . as_ref ( )
383
+ }
384
+
385
+ #[ inline]
386
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
387
+ ( usize:: MAX , None )
388
+ }
389
+ }
You can’t perform that action at this time.
0 commit comments