Given enum values implement the same trait, is it possible to extract trait object from enum value?
Form the doc example:
fn main() {
enum IpAddr {
V4(String),
V6(String),
}
let home = IpAddr::V4(String::from("127.0.0.1"));
let loopback = IpAddr::V6(String::from("::1"));
}
Here all the enum values hold a String
, is it possible to extract it without enumerating all the options with match
?
Something like:
let result = factory.get_enum(); // -> IpAddr
let str: String = (some_cast)result.getValue();
I'd like to avoid:
match result {
Product1(product1) => {},
...
ProductN(productN) => {}
}
And more complicated case: let's say all the enum values hold a trait object:
trait TProduct {
fn something();
}
impl TProduct for Product1 {}
...
impl TProduct for ProductN {}
enum ProductResult {
product1(Product1),
..
productN(ProductN)
}
...
let result: ProductResult = factory.build();
let product = result.getValue() as dyn TProduct; // is it possible with Rust?
let something = product.something();