Closed
Description
I'm writing a simple Generator that takes a reference to an RNG, and yields values from it. This makes sense as long as the generator doesn't outlive the RNG which it references.
I want to name the type of the generator with an existential type, but I can't do so unless I wrap a version of the function that uses an impl Trait
formulation.
Here's the direct version which doesn't work
existential type RandGenerator<'a>: 'a + Generator<Return=!, Yield=u64>;
fn rand_generator<'a>(rng: &'a mut MyRng) -> RandGenerator<'a> {
move || {
loop {
yield rng.gen::<u64>();
};
}
}
and here's the more convoluted version which does
existential type RandGeneratorWithIndirection<'a>: 'a + Generator<Return=!, Yield=u64>;
fn rand_generator_with_indirection<'a>(rng: &'a mut MyRng) -> RandGeneratorWithIndirection<'a> {
fn helper<'b>(rng: &'b mut MyRng) -> impl 'b + Generator<Return=!, Yield=u64> {
move || {
loop {
yield rng.gen::<u64>();
};
}
}
helper(rng)
}
and here's a playground link containing both versions, as well as a bit of scaffolding
The full error message I'm seeing looks like this
Compiling playground v0.0.1 (/playground)
error: non-defining existential type use in defining scope
--> src/main.rs:16:64
|
16 | fn rand_generator<'a>(rng: &'a mut MyRng) -> RandGenerator<'a> {
| ________________________________________________________________^
17 | | move || {
18 | | loop {
19 | | yield rng.gen::<u64>();
20 | | };
21 | | }
22 | | }
| |_^ lifetime `` is part of concrete type but not used in parameter list of existential type
error: aborting due to previous error
error: Could not compile `playground`.
To learn more, run the command again with --verbose.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Done