pub struct Gn<A = ()> { /* private fields */ }Expand description
Generator helper
Implementations§
Source§impl<A> Gn<A>
impl<A> Gn<A>
Sourcepub fn new_scoped<'a, T, F>(f: F) -> Generator<'a, A, T>
pub fn new_scoped<'a, T, F>(f: F) -> Generator<'a, A, T>
create a scoped generator with default stack size
Examples found in repository?
examples/pipe.rs (lines 6-11)
5 fn square<'a, T: Iterator<Item = u32> + Send + 'a>(input: T) -> Generator<'a, (), u32> {
6 Gn::new_scoped(|mut s| {
7 for i in input {
8 s.yield_with(i * i);
9 }
10 done!();
11 })
12 }
13
14 // fn sum<'a, T: Iterator<Item = u32> + 'a>(input: T) -> impl Iterator<Item = u32> + 'a {
15 fn sum<'a, T: Iterator<Item = u32> + Send + 'a>(input: T) -> Generator<'a, (), u32> {
16 Gn::new_scoped(|mut s| {
17 let mut acc = 0;
18 for i in input {
19 acc += i;
20 s.yield_with(acc);
21 }
22 done!();
23 })
24 }More examples
examples/range.rs (lines 5-12)
3fn main() {
4 let n = 100000;
5 let range = Gn::new_scoped(move |mut s| {
6 let mut num = 0;
7 while num < n {
8 s.yield_(num);
9 num += 1;
10 }
11 done!();
12 });
13
14 let sum: usize = range.sum();
15 println!("sum ={sum}");
16}examples/fib.rs (lines 4-12)
3fn main() {
4 let g = Gn::new_scoped(|mut s| {
5 let (mut a, mut b) = (0, 1);
6 while b < 200 {
7 std::mem::swap(&mut a, &mut b);
8 b += a;
9 s.yield_(b);
10 }
11 done!();
12 });
13
14 for i in g {
15 println!("{i}");
16 }
17}examples/number.rs (lines 4-17)
3fn factors(n: u32) -> Generator<'static, (), u32> {
4 Gn::new_scoped(move |mut s| {
5 if n == 0 {
6 return 0;
7 }
8
9 s.yield_with(1);
10
11 for i in 2..n {
12 if n % i == 0 {
13 s.yield_with(i);
14 }
15 }
16 done!();
17 })
18}examples/yield_from.rs (lines 16-20)
12fn main() {
13 let g1 = Gn::new(|| xrange(0, 10));
14 let g2 = Gn::new(|| xrange(10, 20));
15
16 let g = Gn::new_scoped(|mut s| {
17 s.yield_from(g1);
18 s.yield_from(g2);
19 done!();
20 });
21
22 g.fold(0, |sum, x| {
23 println!("i={}, sum={}", x, sum + x);
24 sum + x
25 });
26}examples/lifetime.rs (lines 4-16)
1fn main() {
2 let str = "foo".to_string();
3
4 let mut gen = generator::Gn::new_scoped(|mut s| {
5 std::thread::scope(|s2| {
6 s2.spawn(|| {
7 std::thread::sleep(std::time::Duration::from_millis(500));
8 println!("{str}");
9 });
10 // here we can't use `yield_` because it still ref to `str`
11 // `yield_` only impl for static captured lifetime
12 // s.yield_(());
13 unsafe { s.yield_unsafe(()) };
14 });
15 generator::done!();
16 });
17
18 gen.next();
19 // std::mem::forget(gen);
20 // drop(gen);
21 // drop(str);
22 std::thread::sleep(std::time::Duration::from_millis(1000));
23}Additional examples can be found in:
Sourcepub fn new_scoped_local<'a, T, F>(f: F) -> LocalGenerator<'a, A, T>
pub fn new_scoped_local<'a, T, F>(f: F) -> LocalGenerator<'a, A, T>
create a scoped local generator with default stack size
Sourcepub fn new_scoped_opt<'a, T, F>(size: usize, f: F) -> Generator<'a, A, T>
pub fn new_scoped_opt<'a, T, F>(size: usize, f: F) -> Generator<'a, A, T>
create a scoped generator with specified stack size
Sourcepub fn new_scoped_opt_local<'a, T, F>(
size: usize,
f: F,
) -> LocalGenerator<'a, A, T>
pub fn new_scoped_opt_local<'a, T, F>( size: usize, f: F, ) -> LocalGenerator<'a, A, T>
create a scoped local generator with specified stack size
Source§impl<A: Any> Gn<A>
impl<A: Any> Gn<A>
Sourcepub fn new<'a, T: Any, F>(f: F) -> Generator<'a, A, T>
👎Deprecated since 0.6.18: please use scope version instead
pub fn new<'a, T: Any, F>(f: F) -> Generator<'a, A, T>
scope version insteadcreate a new generator with default stack size
Examples found in repository?
More examples
examples/yield_from.rs (line 13)
12fn main() {
13 let g1 = Gn::new(|| xrange(0, 10));
14 let g2 = Gn::new(|| xrange(10, 20));
15
16 let g = Gn::new_scoped(|mut s| {
17 s.yield_from(g1);
18 s.yield_from(g2);
19 done!();
20 });
21
22 g.fold(0, |sum, x| {
23 println!("i={}, sum={}", x, sum + x);
24 sum + x
25 });
26}examples/send.rs (line 20)
18fn main() {
19 // we specify the send type is u32
20 let mut s = Gn::<u32>::new(|| sum(0));
21 // first start the generator
22 assert_eq!(s.raw_send(None).unwrap(), 0);
23 let mut cur = 1;
24 let mut last = 1;
25
26 while !s.is_done() {
27 // println!("send={}", last);
28 mem::swap(&mut cur, &mut last);
29 cur = s.send(cur); // s += cur
30 // println!("cur={} last={}", cur, last);
31 println!("{cur}");
32 }
33}Auto Trait Implementations§
impl<A> Freeze for Gn<A>
impl<A> RefUnwindSafe for Gn<A>where
A: RefUnwindSafe,
impl<A> Send for Gn<A>where
A: Send,
impl<A> Sync for Gn<A>where
A: Sync,
impl<A> Unpin for Gn<A>where
A: Unpin,
impl<A> UnwindSafe for Gn<A>where
A: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more