Marwes / pretty.rs

Wadler-style pretty-printing combinators in Rust
MIT License
143 stars 22 forks source link

Don't borrow allocator for 'a lifetime #58

Open Alxandr opened 4 years ago

Alxandr commented 4 years ago

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?

Marwes commented 4 years ago

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.

Alxandr commented 4 years ago

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 😓.