PoiScript / orgize

A Rust library for parsing org-mode files.
https://poiscript.github.io/orgize/
MIT License
278 stars 34 forks source link

Provide a way to call into_owned on elements #13

Closed calmofthestorm closed 4 years ago

calmofthestorm commented 4 years ago

Elements provide an into_owned method to give them a static lifetime, so they can be used independently of the indextree (arena). This cannot actually be called by users, however, because it is a consuming method, and there doesn't seem to be any way to get an owning reference to an element*.

This PR addresses the problem by making all public elements which provide into_owned derive Clone. I don't see any obvious reason why they should not be Clone.

Another option would be to change into_owned to to_owned and having it take &self instead of self. I started to write that, but found that it resulted in a lot of duplicated code -- if the structs aren't Clone, to_owned can't call into_owned, and having into_owned call to_owned will result in unnecessary copies (e.g., if some/all struct members are already owned).

I also looked at implementing std::borrow::ToOwned as part of that, but can't seem to figure out how to convince it to change the lifetime to static, and a bit of googling convinced me it may not be possible at present.

PoiScript commented 4 years ago

Thanks for the pull request.

Speaking of using indextree, I'm actually thinking it was an overdesigned. Keeping everything inside a single arena seems have more cons than pros. I opened a discussion at #18, leave a comment there if you have any idea for dropping indextree.