File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ mod sources;
49
49
pub use sources:: { convert, Convert } ;
50
50
pub use sources:: { convert_ref, ConvertRef } ;
51
51
pub use sources:: { empty, Empty } ;
52
+ pub use sources:: { from_fn, FromFn } ;
52
53
pub use sources:: { once, Once } ;
53
54
pub use sources:: { once_with, OnceWith } ;
54
55
Original file line number Diff line number Diff line change @@ -42,7 +42,13 @@ pub fn empty<T>() -> Empty<T> {
42
42
}
43
43
}
44
44
45
- /// Creates an iterator that returns exactly one item
45
+ /// Creates an iterator that returns items from a function call.
46
+ #[ inline]
47
+ pub fn from_fn < T , F : FnMut ( ) -> Option < T > > ( gen : F ) -> FromFn < T , F > {
48
+ FromFn { gen, item : None }
49
+ }
50
+
51
+ /// Creates an iterator that returns exactly one item.
46
52
#[ inline]
47
53
pub fn once < T > ( item : T ) -> Once < T > {
48
54
Once {
@@ -219,6 +225,27 @@ impl<T> DoubleEndedStreamingIterator for Empty<T> {
219
225
fn advance_back ( & mut self ) { }
220
226
}
221
227
228
+ /// A simple iterator that returns items from a function call.
229
+ #[ derive( Clone , Debug ) ]
230
+ pub struct FromFn < T , F > {
231
+ gen : F ,
232
+ item : Option < T > ,
233
+ }
234
+
235
+ impl < T , F : FnMut ( ) -> Option < T > > StreamingIterator for FromFn < T , F > {
236
+ type Item = T ;
237
+
238
+ #[ inline]
239
+ fn advance ( & mut self ) {
240
+ self . item = ( self . gen ) ( ) ;
241
+ }
242
+
243
+ #[ inline]
244
+ fn get ( & self ) -> Option < & Self :: Item > {
245
+ self . item . as_ref ( )
246
+ }
247
+ }
248
+
222
249
/// A simple iterator that returns exactly one item.
223
250
#[ derive( Clone , Debug ) ]
224
251
pub struct Once < T > {
You can’t perform that action at this time.
0 commit comments