pretty.rs icon indicating copy to clipboard operation
pretty.rs copied to clipboard

Don't borrow allocator for 'a lifetime

Open Alxandr opened this issue 6 years ago • 2 comments

I'm trying to create a function that takes a ToDoc type (I made a trait that basically has the function (&self, &'a Allocator) -> Allocator::Doc) and returns a "document", without the caller having to care about allocators, however I'm constantly running into lifetime issues, and aside from going ham with mem::transmute, I'm not quite sure how to solve this. Currently, the only obstacle I have to being able to create a self-referential struct containing both the doc and the arena is the fact that all of the functions on the arena takes the arena by &'a Arena, instead of just &Arena, so I was wondering if there is a good reason for these two to be required to have the same lifetime?

Alxandr avatar Dec 14 '19 21:12 Alxandr

I guess RefDoc could take two lifetimes to separate the lifetime from the arena and the lifetime of the &str in Doc. That might fix the issues that you have.

Marwes avatar Dec 14 '19 22:12 Marwes

That would hopefully solve it. This is what I currently have to do: https://github.com/YoloDev/fat-stream/blob/master/src/formatted.rs#L28, and this forces me to make the ToDoc trait unsafe which means at this point probably 90% or more of my crate is unsafe code 😓.

Alxandr avatar Dec 14 '19 23:12 Alxandr