Is it possible to get back the implementing type from a trait, when you have used the trait in order to store different generic structs in a Vec
? The following attempt conveys the idea.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
struct AStruct<T> { a: T }
trait A {
fn downcast(&self) -> &Self;
}
impl<T> A for AStruct<T> where T: Eq {
fn downcast(&self) -> &AStruct<T> {
&self
}
}
fn main() {
let mut v: Vec<Box<A>> = Vec::new();
let a_num_box = Box::new(AStruct {a: 42});
let a_str_box = Box::new(AStruct {a: "the answer"});
v.push(a_num_box);
v.push(a_str_box);
let a: &AStruct<usize> = v[0].as_ref().downcast();
}