#iterator #generator #yield #create #genawaiter

genawaiter_iterator

A convenient way to create Iterators from Genawaiter's generators

4 releases

0.1.3 Feb 4, 2024
0.1.2 Feb 4, 2024
0.1.1 Feb 4, 2024
0.1.0 Feb 4, 2024

#9 in #yield

MIT license

5KB
50 lines

See the documentation for further explanations, or just check the sources, they're small enough.


lib.rs:

This crate is intended for easy creation of iterators without exposing that they're actually Genawaiter generators. Generators have more overhead than iterators (in this implementation), but it's much easier to write them so it's worth the overhead, I think.

Use genawaiter_iterator! to create the iterator struct, then use gen! to create a generator and automatically coerce it to the output type (implied that you invoke gen! in an iter(&self) -> IterStructName method (might have another receiver type)).

Usage example:

use genawaiter_iterator::{genawaiter_iterator, gen};
use genawaiter::yield_;

genawaiter_iterator!(struct Iter yields usize);

fn iter() -> Iter {
    gen!({
        yield_!(1);
        yield_!(2);
        yield_!(3);
    })
}

let mut iterator_instance = iter();
assert_eq!(iterator_instance.next(), Some(1));
assert_eq!(iterator_instance.next(), Some(2));
assert_eq!(iterator_instance.next(), Some(3));
assert_eq!(iterator_instance.next(), None);

Dependencies

~100KB