Open M1cha opened 2 years ago
Admittedly it'd currently be impossible (to my knowledge) to write a reference-yielding Generator manually by implementing the trait since you can't specify the lifetimes properly.
I think this is exactly why this is an issue. Generators just lower down to special objects that implement the Generator
trait -- there's no way to express this lifetime relationship, since the yield type of a generator cannot capture the lifetime of the self
passed in.
@rustbot label +A-generators
I think this particular feature would be possible after something like #69268 is implemented
Consider this:
Currently, this causes: error[E0515]: cannot yield value referencing local variable
buf
I don't see a reason why the compiler couldn't track that lifetime like it does when returning references to
self
from synchronous functions. As long as you want to use the return value, you'd simply not be allowed to callresume()
since you'd need a reference to the generator for that.Admittedly it'd currently be impossible (to my knowledge) to write a reference-yielding Generator manually by implementing the trait since you can't specify the lifetimes properly. It might be possible using GAT but afaik that'd require a change to the signature of the Generator trait.