I have multiple types and an enum:
struct Foo;
struct Bar;
enum MyEnum {
Foos(Vec<Foo>),
Bars(Vec<Bar>),
}
Is there a way to make a generic .push
method for this, like
impl MyEnum {
fn push<T>(&mut self, item: T) {
if item.type_id() == TypeId::of::<Foo>() {
if let MyEnum::Foos(ref mut vec) = self {
vec.push(item); // doesn't compile because item is generic type `T` instead of `Foo`
}
} else if item.type_id == TypeId::of::<Bar>() {
if let MyEnum::Bars(ref mut vec) = self {
vec.push(item);
}
}
}
}
This doesn't compile because .push
only sees generic type T
, not the type it was expecting.
Is there a way to accomplish this?