Using data inside and outside of a generator

I am using the genawaiter crate to step thought the parsing of a custom programming language. The genawaiter crate allows for generator-like behavior using async/await. I need to be able to access the current parse tree, modified within the generator, from outside. I have program nodes currently written as methods (functions with &mut self).

I came up with two solutions, neither or which I could get rust to agree with:

  1. I hold the parse tree in a RwLock outside and pass it into the generator by ref. Then, when stepping, I move the self ref into a function that drops it, yields, then regrabs and returns the mut ref from somewhere in the Rwlocked tree. I wasn't able to change the &mut self to a self:RwLockReadGuard<'a, Self> however.
  2. I hold the parse tree in a RwLock inside the generator. I change the &mut self to a custom pointer that on deref, grabs the node from the tree. I think I can do this with projection (self: <NodeRef as Deref>::Target). Then I can easily pass a ref to the tree back when yielding.

I was unable to get rust to allow either of these solutions due to lifetime issues. Is there a better way to do this that I didn't think of or is this just impossible with the crate and/or the way I am using it?

Since you haven't gotten any replies, I have two suggestions:

  • Post the code for the two solutions you tried. Provide runnable code so others can try it and see the same errors you're seeing. Or if that's not feasible then at least post relevant portions of the code along with the full errors you get from running cargo in the terminal -- there are often hints and clues in the full error messages.
  • If you'd rather not post code, then post your question as a github issue in the genawaiter repo.