#iterator #generator #temporary

nightly gen-iter

temporary util for creating iterators using generators

6 releases

Uses old Rust 2015

0.3.0 Jan 3, 2023
0.2.1 Feb 7, 2022
0.2.0 Apr 30, 2020
0.1.2 Aug 30, 2017

#27 in #temporary

Download history 140/week @ 2024-06-21 71/week @ 2024-06-28 127/week @ 2024-07-05 31/week @ 2024-07-12 59/week @ 2024-07-19 87/week @ 2024-07-26 53/week @ 2024-08-02 38/week @ 2024-08-09 32/week @ 2024-08-16 4/week @ 2024-08-23 10/week @ 2024-08-30 24/week @ 2024-09-06 47/week @ 2024-09-13 41/week @ 2024-09-20 11/week @ 2024-09-27 3/week @ 2024-10-04

109 downloads per month
Used in 2 crates

MIT/Apache

10KB
182 lines

gen_iter - iterators using generators.

see the docs for examples and usage.

License

dual MIT / apache-2.0


lib.rs:

gen_iter - create generators to use as iterators

GenIter and gen_iter!

GenIter converts a Generator<(), Return=()> into an iterator over the yielded type of the generator. The return type of the generator needs to be ().

gen_iter! helps to create a GenIter

#![feature(generators)]

use gen_iter::gen_iter;

fn fibonacci() -> impl Iterator<Item = u64> {
    gen_iter!({
        let mut a = 0;
        let mut b = 1;

        loop {
            let c = a + b;
            a = b;
            b = c;

            yield a;
        }
    })
}

for elem in fibonacci().map(|x| 2 * x).take(10) {
    println!("{}", elem);
}

GenIterReturn and gen_iter_return!

GenIterReturn can be converted from a Generator<()>, &mut GenIterReturn<G> can be used as iterator. The return value of the generator can be got after the iterator is exhausted.

gen_iter_return! helps to create a GenIterReturn.

#![feature(generators)]

use gen_iter::gen_iter_return;

let mut g = gen_iter_return!({
    yield 1;
    yield 2;
    return "done";
});

for y in &mut g {
    println!("yield {}", y);
}
println!("generator is_done={}", g.is_done()); // true
println!("generator returns {}", g.return_or_self().ok().unwrap()); // "done"

No runtime deps