#generator #iterator #utils #temporary #gen-iter-return

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

#35 in #temporary

Download history 1785/week @ 2024-02-14 1033/week @ 2024-02-21 1155/week @ 2024-02-28 1411/week @ 2024-03-06 310/week @ 2024-03-13 531/week @ 2024-03-20 90/week @ 2024-03-27 133/week @ 2024-04-03 50/week @ 2024-04-10 46/week @ 2024-04-17 62/week @ 2024-04-24 71/week @ 2024-05-01 50/week @ 2024-05-08 76/week @ 2024-05-15 83/week @ 2024-05-22 55/week @ 2024-05-29

286 downloads per month
Used in 3 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