I have an issue where I'm struggling to come up with a satisfying solution.
I have 3 entities which can be linked like so:
A (1 <-> N) -> B <- (1 <-> M) C
My current solution involves:
pub struct A { bs: Vec<B> }
impl A {
fn cs(&self, z: &Z) -> Vec<C> {
let mut cs: Vec<C> = Vec::new();
for b in self.bs {
// the clone I'm not happy about
let inners = C::cs(Rc::new(b.clone()), z);
cs.append(&mut inners);
}
cs
}
}
pub struct B { }
impl B {
fn fs(&self, z: &Z) -> Vec<f64> {
vec![]
}
}
pub struct C { b: Rc<B>, f: f64 }
impl C {
// tags each f with the proper B
fn cs(b: Rc<B>, z: &Z) -> Vec<C> {
b.fs(z).iter().map(|f| C { Rc::clone(&b), *f }).collect()
}
}
pub struct Z { }
I'm not happy with having to clone each B once and would like to remove that part.
If you have any ideas, there are welcome, thank you