File: issue-58662-generator-with-lifetime.rs

package info (click to toggle)
rustc-web 1.70.0%2Bdfsg1-7~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,517,036 kB
  • sloc: xml: 147,962; javascript: 10,210; sh: 8,590; python: 8,220; ansic: 5,901; cpp: 4,635; makefile: 4,006; asm: 2,856
file content (39 lines) | stat: -rw-r--r-- 991 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// check-pass

#![feature(generators, generator_trait)]
#![feature(type_alias_impl_trait)]

use std::ops::{Generator, GeneratorState};
use std::pin::Pin;

type RandGenerator<'a> = impl Generator<Return = (), Yield = u64> + 'a;
fn rand_generator<'a>(rng: &'a ()) -> RandGenerator<'a> {
    move || {
        let _rng = rng;
        loop {
            yield 0;
        }
    }
}

pub type RandGeneratorWithIndirection<'c> = impl Generator<Return = (), Yield = u64> + 'c;
pub fn rand_generator_with_indirection<'a>(rng: &'a ()) -> RandGeneratorWithIndirection<'a> {
    fn helper<'b>(rng: &'b ()) -> impl 'b + Generator<Return = (), Yield = u64> {
        move || {
            let _rng = rng;
            loop {
                yield 0;
            }
        }
    }

    helper(rng)
}

fn main() {
    let mut gen = rand_generator(&());
    match unsafe { Pin::new_unchecked(&mut gen) }.resume(()) {
        GeneratorState::Yielded(_) => {}
        GeneratorState::Complete(_) => {}
    };
}