#![feature(generators, generator_trait)] use simple_generators::*; #[test] fn test_macro() { let sum: u64 = gen_macro(10).sum(); assert_eq!(sum, 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9); } #[test] fn test_macro_lifetime() { let sum: u64 = Foo { vec: vec![10, 20, 30], } .test_gen() .sum(); assert_eq!(sum, 10 + 20 + 30); } #[test] fn test_iter() { let sum: u64 = gen_iter(10).sum(); assert_eq!(sum, 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9); } #[generator] fn gen_macro(n: u64) -> impl Iterator { let mut num = 0; while num < n { yield num; num += 1; } } fn gen_iter(n: u64) -> impl Iterator { (move || { let mut num = 0; while num < n { yield num; num += 1; } }) .iter() } fn gen_adapter(n: u64) -> impl Iterator { GeneratorIteratorAdapter(move || { let mut num = 0; while num < n { yield num; num += 1; } }) } struct Foo { vec: Vec, } impl Foo { #[generator] fn test_gen<'a>(&'a self) -> impl Iterator + 'a { for item in &self.vec { yield *item; } } }