which is a pseudo-code version of me trying to encapsulate an environment and session from the onnxruntime library in a struct, and giving it a constructor. The library is probably irrelevant to the question.
This will of course fail to compile with returns a value referencing data owned by the current function, given that session borrows environment, which is a local variable.
I have been trying for two hours to make it work with different ideas (initialization function instead of a new, storing session and/or environment as Rcs, Options, etc.) but I hit a different problem every time. It seems that I can't overcome the fact that session will borrow environment.
This kind of struct declaration with a lifetime parameter 'a means that the contents of session borrow from something outside of the struct A. What you’re trying is called a “self-referential” struct/data type. These aren’t supported in Rust natively (without using e.g. pointers and – crucially – lots of unsafe), but there’s crates such as ouroboros that provide safe macros for doing something like this.
Alternatively, it’s often just avoided to bundle some owned data together with borrowed data based on the owned one. You can also search for the term “self-referential struct” if you want to find out more.
Thank you for your reply.
I would be happy to get rid of that lifetime parameter and make everything owned by the struct, though... I just put this lifetime parameter because the compiler complained (heh), given that apparently this type requires a lifetime.