I'm rewriting a C++ application in Rust and I need to implement an array of pointers to the base class and populate the array with derived classes.
The C++:
BaseClass base[2] = new BaseClass[2];
base[0] = FirstDerivedClass ();
base[1] = SecondDerivedClass ();
FirstDerivedClass *fderived = dynamic_cast<FirstDerivedClass> (base[0]);
if (fderived != nullptr) {
fderived.exclusive_method ();
}
I've tried to create something similar using Vec<Box<BaseTrait>>
but there is no way to cast it to the appropriate derived class.
A solution with enum is not appropriate as there are great differences in size between the variants and I need to allocate several thousands of elements. This led me to combine a struct boxed in enum, but I don't know how to implement it.