I am reading Object Safety Is Required for Trait Objects and I don't understand the problem with generic type parameters.
The same is true of generic type parameters that are filled in with concrete type parameters when the trait is used: the concrete types become part of the type that implements the trait. When the type is forgotten through the use of a trait object, there is no way to know what types to fill in the generic type parameters with.
I am trying to code an example but I can't make sense of it. Generic type parameters for what?
I tried to make a trait object out of a parameterized trait, but once the parameter is given a concrete value it works just fine:
trait Creator<T> {
fn create(&self) -> T;
}
struct CreationHouse {
creators: Vec<Box<dyn Creator<u32>>>
}
struct NumCreator { seed: u32 }
impl Creator<u32> for NumCreator {
fn create(&self) -> u32 {
return self.seed;
}
}
fn main() {
let ch = CreationHouse{
creators: vec![Box::new(NumCreator{seed: 3})]
};
}
(Compiles well, except "unused" warnings)
What I don't get is what does it mean "generic type parameters that are filled in with concrete type parameters when the trait is used" and how could the generic types be lost (as the trait "carries" them with itself). If you could write an example of the case described in the paragraph I'd be grateful.