I've created this simple example to simulate what happens with references in my program.
struct Child {}
struct Parent<'a> {
child: &'a Child
}
struct Family {
child: Box<Child>,
parents: Vec<Box<dyn Any>>
}
fn main() {
let child = Box::new(Child {});
let mut family = Family { child, parents: vec![] }; // child was moved to the family
let parent = Box::new(Parent { child: &family.child }); // Parent accepts reference to family's child
family.parents.push(parent); // Now parent was moved to the family.
// So Family owns both: child and parent,
// so reference to the child in the parent will be correct during
// lifetime of family
}
This code looks correct for me because the Family owns both: Child and Parent, so they will be dropped at the same time and the Parent can safely hold a reference to the child. But rust compiler doesn't think so:
error[E0597]: `family.child` does not live long enough
|
18 | let parent = Box::new(Parent { child: &family.child }); // Parent accepts reference to family's child
| ^^^^^^^^^^^^^ borrowed value does not live long enough
19 |
20 | family.parents.push(parent); // Now
| ------ cast requires that `family.child` is borrowed for `'static`
21 | }
| - `family.child` dropped here while still borrowed
Usage of dyn Any
and boxes is intentional - it simulates what happens in my program.