Closed edef1c closed 8 years ago
The only part I'm still not sure about is whether this handles variance correctly...
So I had a look at the variance, and here is what I think it should be:
Generator
should be variant over 'a
and Output
, but invariant over Input
.Yielder
should be variant over Input
but invariant over Output
.This means that you be using the following PhantomData
types:
Generator
: PhantomData<(&'a (), *mut Input, *const Output)>
Yielder
: PhantomData<(*const Input, *mut Output)>
Additionally, you should only provide an immutable reference to Yielder
in the generator function. A mutable reference would allow calling mem::swap
on the yielder and replacing it with a different one. Yielder::suspend
only requires an immutable reference anyways.
And finally, you can add Send
and Sync
for Generator
(neither are applicable to Yielder
). Sync
should always be available, while Send
should only be available if Stack: Send
.
Previously, it was possible to cause unsafety by having the closure refer to values with lifetimes that weren't enclosed by the generator's lifetime.