[go: up one dir, main page]

Gn

Struct Gn 

Source
pub struct Gn<A = ()> { /* private fields */ }
Expand description

Generator helper

Implementations§

Source§

impl<A> Gn<A>

Source

pub fn new_scoped<'a, T, F>(f: F) -> Generator<'a, A, T>
where for<'scope> F: FnOnce(Scope<'scope, 'a, A, T>) -> T + Send + 'a, T: Send + 'a, A: Send + 'a,

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
Hide additional 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}
Source

pub fn new_scoped_local<'a, T, F>(f: F) -> LocalGenerator<'a, A, T>
where F: FnOnce(Scope<'_, '_, A, T>) -> T + 'a, T: 'a, A: 'a,

create a scoped local generator with default stack size

Source

pub fn new_scoped_opt<'a, T, F>(size: usize, f: F) -> Generator<'a, A, T>
where for<'scope> F: FnOnce(Scope<'scope, 'a, A, T>) -> T + Send + 'a, T: Send + 'a, A: Send + 'a,

create a scoped generator with specified stack size

Source

pub fn new_scoped_opt_local<'a, T, F>( size: usize, f: F, ) -> LocalGenerator<'a, A, T>
where F: FnOnce(Scope<'_, '_, A, T>) -> T + 'a, T: 'a, A: 'a,

create a scoped local generator with specified stack size

Source§

impl<A: Any> Gn<A>

Source

pub fn new<'a, T: Any, F>(f: F) -> Generator<'a, A, T>
where F: FnOnce() -> T + Send + 'a,

👎Deprecated since 0.6.18: please use scope version instead

create a new generator with default stack size

Examples found in repository?
examples/get_yield.rs (line 18)
16fn main() {
17    // we specify the send type is u32
18    let mut s = Gn::<u32>::new(|| sum(1));
19    let mut i = 1u32;
20    while !s.is_done() {
21        i = s.send(i);
22        println!("{i}");
23    }
24}
More examples
Hide additional 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}
Source

pub fn new_opt<'a, T: Any, F>(size: usize, f: F) -> Generator<'a, A, T>
where F: FnOnce() -> T + Send + 'a,

create a new generator with specified stack size

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.