struct Foo(u32);
struct Bar(u16, u16, u16);
impl Get for Foo {
fn get(&self) -> u32 {
self.0
}
}
impl Get for Bar {
fn get(&self) -> u32 {
self.0 as u32 + self.1 as u32 + self.2 as u32
}
}
There are two ways that these trait objects usually end up in a collection:
let mut v1 = Vec::<&dyn Get>::new(); // collection of trait object references
let mut v2 = Vec::<Box<dyn Get>>::new(); // collection of boxed trait objects
I want a function that can operate on generic collections of this trait object. I don't know if passing an iterator as an argument is the right approach here, but that's what I've been trying.
I can do either of these:
fn sum_from_iter_box<'a, I, T>(it: I)
where
I: IntoIterator<Item = &'a Box<T>>, // <-- Box<T>
T: Get + 'a + ?Sized,
{ ... }
fn sum_from_iter_ref<'a, I, T>(it: I)
where
I: IntoIterator<Item = &'a &'a T>, // <-- &'a T
T: Get + 'a + ?Sized,
{ ... }
But I haven't been able to figure out how to write a single function that can take an Iterator with either Item type.