TL;DR;
struc: TheirPoint
, TheirLine
trait: TheirShadow
, MySpecialShadow
TheirShadow for TheirPoint
TheirShadow for TheirLine
MySpecialShadow for TheirPoint
MySpecialShadow for TheirLine
let vec_shapes: Vec<Box<dyn TheirShadow>> = vec![Box::new(point), Box::new(line)];
Problem:
for shape in vec_shapes {
shape.myspecialshadow_function()
//doesnt work - what can be done? Downcast? - but how to handle different types?
}
Long version: Playground
I'm working with a lib <TheirThings> and use that in <MyCode>.
Specifically, I use different of their Shapes (TheirPoint
, TheirLine
) which have all a common
- output:
TheirOutput
which is an enum - function:
cast_shadow
provided by TraitTheirShadow
, which returnsTheirOutput
I have a Vec<Box<dyn TheirShadow>>
let vec_shapes: Vec<Box<dyn TheirShadow<Output=TheirOutput>>> = vec![Box::new(p), Box::new(l)];
I've created a Trait MySpecialShadow
, having a function special_shadow
which I implement individually for TheirPoint
, TheirLine
, and I struggle being able to call it during a loop over vec_shapes.
I tried the following without success
let vec_shapes: Vec<Box<dyn TheirShadow<Output=TheirOutput, MySpecialShadow>>> = vec![Box::new(p), Box::new(l)];
Do you have a recommendation how to structure the code so that a mixed vec based on dyn Trait
enables call ability to other traits? Downcasting? How would that look like for a mixed vec?
Thanks for ideas!