Take ownership of roxmltree::Document

roxmltree::Document is currently defined as roxmltree::Document<'input> as it does not take ownership of the &str it contains.

I am attempting to embed a Document within an existing struct that does not have lifetime parameters on it. Adding the lifetime parameters isn't the end of the world, except that this requirement cascades throughout nearly every struct in my application.

Is there some way to take ownership of the Document in this case?

The &str that it will contain is parsed from a file read in during serialization.

This is my first time really running into lifetime issues while writing rust, and I am a bit frustrated.

Thanks in advance!

Your options are:

  • Find a library other than roxmltree that is willing to take ownership.

  • Reorganize your application so that only a tolerable number of structs need new lifetime annotations.

  • Leak the string, and use that to construct a roxmltree::Document<'static>.

  • Use one of the libraries that carefully allow you to define “self-referential” structures that contain the String and the Document together, such as ouroboros or self_cell.

4 Likes

another option is yoke:

1 Like

I didn’t mention yoke because I was under the impression that yoke required that all borrowing types involved implemented the Yokeable trait, meaning it would be unusable here since roxmltree::Document doesn’t implement Yokeable. I have now actually tested that case and learned that it is allowed (derive(Yokeable) works on a struct containing a struct that does not implement Yokeable). Thank you!

Marking this as a solution for the immediate problem of lifetimes.

I wound up having to reimplement roxmltree, with owned datatypes so I could mutate the tree for a different problem.

Thanks for all the suggestions!