Continuing the discussion from When to use dynamic dispatch?:
I know I can have static dispatch with Enum like this:
struct Dog;
struct Cat;
pub enum Animal {
Dog(Dog),
Cat(Cat)
}
trait Run {
fn run(&self);
}
impl Run for Animal {
fn run(&self) {
match self {
Animal::Dog(dog) => {
// dog run
},
Animal::Cat(cat) => {
// cat run
}
}
}
}
fn animal_run(animal: &Animal) {
animal.run();
}
// static dispatch
animal_run(Animal::Dog(Dog));
animal_run(Animal::Cat(Cat));
And when I want to downcast Animal to Dog or Cat, I can write a method like below:
impl Animal {
pub fn as_dog(&self) -> Result<&Dog, Error> {
if let Animal::Dog(dog) = self {
dog
} else {
Err(format_err!("downcast Animal to Dog failed"))
}
}
}
My question is, when downcast a Enum, should I return a Result or just panic? I personally tend to panic directly, though.
impl Animal {
pub fn as_dog(&self) -> &Dog {
if let Animal::Dog(dog) = self {
dog
} else {
panic!("failed to convert ui_state to editable dog")
}
}
}
I'm not sure whether it's really a problem, but I want to know if there's any best practice to downcast?