[−][src]Macro next_gen::gen_iter
Emulate a for
-loop iteration over a generator. The call itself evaluates
to the Return
value of the Generator
.
Example
use ::next_gen::prelude::*; type Question = &'static str; type Answer = i32; #[generator(Question)] fn answer () -> Answer { yield_!("What is the answer to life, the universe and everything?"); 42 } let ret = gen_iter!( for question in answer() { assert_eq!( question, "What is the answer to life, the universe and everything?", ); } ); assert_eq!(ret, 42); // You can also give it an already instanced generator: mk_gen!(let mut generator = answer()); assert_eq!( generator.as_mut().resume(), ::next_gen::GeneratorState::Yield( "What is the answer to life, the universe and everything?" ), ); let ret = gen_iter!( for _ in generator { unreachable!(); } ); assert_eq!(ret, 42);
Note that you do not need this macro when you don't care about the return value:
use ::next_gen::prelude::*; type Question = &'static str; type Answer = i32; #[generator(Question)] fn answer () -> Answer { yield_!("What is the answer to life, the universe and everything?"); 42 } mk_gen!(let generator = answer()); for question in generator { assert_eq!( question, "What is the answer to life, the universe and everything?", ); }