Trying to implement a data structure where many "things" can be owned by an "owner", while each "thing" should know who its owner is. As far as I understand it can be achieved by having a Weak reference to the owner in each "thing". The question I cannot figure out how to implement the constructor (new) function for these structures. Here is a simplified structure of what I am trying to do:
use std::rc::{Rc, Weak};
struct Owner {
thing1: Thing,
thing2: Thing,
}
impl Owner {
fn new() -> Self {
Self {
thing1: Thing::new(???), // 1
thing2: Thing::new(???), // 1
}
}
}
struct Thing {
owner: Weak<Owner>,
value: u32,
}
impl Thing {
fn new(owner: ???) -> Self { // 2
Self {
owner: Weak::clone(???), // 2
value: 0,
}
}
}
So here I can't figure out two things:
- When constructing Owner - how to pass itself as a parameter to the constructor of "things" ?
- When constructing the Thing - how exactly create the reference to the owner?
Perhaps my approach is completely wrong, in this case please suggest the right one.
Update:
A more concrete example of a use-case for such a structure:
Consider an electronic circuit simulation, where each "thing" is a pin of an electronic component, which is the "owner". So each time the value of a pin is externally changed, it should trigger the owner component, which in turn will change the other connector values.