I'm trying to hold a vector of references across the nested hierarchy (cloning is not an option), but I'm having trouble grasping lifetimes.
I've tried to summarize my code here:
struct A {
b: B,
c: C,
}
impl A {
fn add(&mut self) {
for i in self.b.d.iter() {
self.c.e.push(&i);
}
}
}
struct B {
d: Vec<E>,
}
struct C {
e: Vec<&E>,
}
struct E {}
I'm running a simulation and storing individual results in B
, and then picking up only a subset of that for visualization, so the Vec
in C
is compiled, rendered and discarded each frame.
My actual hierarchy is more complex than this, but I hope that if I understand this problem I'll be able to scale it up.
How do I approach setting up lifetimes here? Is this even an idiomatic Rust way or should I re-organize my entities?