edef1c / libfringe

a Rust library implementing safe, lightweight context switches, without relying on kernel services
https://edef1c.github.io/libfringe
Apache License 2.0
512 stars 31 forks source link

Ensure that the closure outlives the generator [breaking-change] #50

Closed edef1c closed 8 years ago

edef1c commented 8 years ago

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.

Amanieu commented 8 years ago

The only part I'm still not sure about is whether this handles variance correctly...

Amanieu commented 8 years ago

So I had a look at the variance, and here is what I think it should be:

This means that you be using the following PhantomData types:

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.

Amanieu commented 8 years ago

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.