This is basically the code I would like to implement:
use std::boxed::Box;
struct X<'a> {
data: &'a String
}
struct Y {
data: String
}
impl Y {
fn create<'a>(&'a self) -> X<'a> {
X { data: &self.data }
}
}
struct Z<'a> {
y: Box<Y>,
x: X<'a>
}
impl<'a> Z<'a> {
fn new(y: Box<Y>) -> Z<'a> {
let x = y.create();
Z { y: y, x: x }
}
fn destroy(self) -> Box<Y>
{
self.y
}
}
fn main() {
let y = Box::new(Y { data: "foo".to_string() });
let z = Z::new(y);
}
Structures X and Y come from a 3rd party library so I cannot change them. I would like to create structure Z. Putting Y in a box is not required, I just through I might be able to get away with more if it was.
What I'd really like to do is implement Drop for Z so that Y is passed back to a collection of Ys. So I think i'm going to actually have to use Box::into_raw and Box::from_raw so that the destruction of Z doesn't destroy Y.
However I still have the problem of the lifetime on Z, since X needs a lifetime, Z needs a lifetime, but I don't know how to set Z's lifetime.