A trait like this prevents &dyn DoAction
because of the generic function:
trait DoAction {
fn action<T: T1 + T2>(&self, s: &T) {
s.action_t1();
s.action_t2();
}
}
Is there a way to write a function where the Vec
contains different concrete types, but they all implement the trait DoAction
?
fn run_all(runners: Vec<&impl DoAction>) {}
The main issue I want to solve is being able to loop over these different concrete types - but I cannot use Vec<&dyn T>
trait objects as decribed in How do I create a heterogeneous collection of objects? because of the generic trait function.
For example:
struct SA {
sa: u32,
}
struct SB {
sb: u32,
}
trait T1 {
fn action_t1(&self) -> bool {
true
}
}
trait T2 {
fn action_t2(&self) -> bool {
true
}
}
impl T1 for SA {}
impl T1 for SB {}
impl T2 for SA {}
impl T2 for SB {}
impl T1 for &SA {}
impl T1 for &SB {}
impl T2 for &SA {}
impl T2 for &SB {}
trait DoAction {
fn action<T: T1 + T2>(&self, s: &T) {
s.action_t1();
s.action_t2();
}
}
struct Runner1 {}
impl DoAction for Runner1 {}
struct Runner2 {}
impl DoAction for Runner2 {}
fn run_all(runners: Vec<&impl DoAction>, s: (impl T1 + T2)) {
for r in runners {
r.action(&s);
}
}
fn main() {
let a = SA { sa: 123 };
let r1 = Runner1 {};
let r2 = Runner2 {};
let ls = vec![&r1, &r2];
run_all(ls, &a);
}