I am trying to implement a (special purpose) logging structure where I will sequentially (one thread - no concurrency) add "Values". I want the values tocontain information about the context they where added and have been trying to buils a struct for that.
Please see Rust Playground (rust-lang.org) for a minimal example.
Basically the idea is to have a linked-list of Writers such that whenever a function has a muteable reference to a Writer it can either:
- Add values to the writer, they will be added with information on the current scope (which the calling function may not know)
- Create a new "wrapped" Writer - i.e creating a nested scope. Now be able to log to that writer and to pass it to sub-functions.
I am however struggling to extpress this in a way so the borrow checker / lifetimes match.
In the linked playground example for example, i know that on line 78 w3 (from line 67) has been dropped (gone out of scope) and should therefor be able to use w2 again.
I suspect the problem is the implementation of Writer::push where the liftime 'a of self becomes the same as the lifetime of the new scope created, i,e, that when w3 is created at line 67 it will actually have the full lifetime of w2, and thus not be dropped at the cloding brace. Regardless - I have tried various ways to express this pattern such that the lifetime of w3 will only be in the innermost scope and only "borrow" the muteability from w2 but not managed.
Am I barking up the wrong tree here? Is this not the best way to express such a pattern in rust? Or am I simply missing somehting else?
Thank you in advance