I'd like to be able to destructure/upcast enum variants to a commonly implemented trait. Here's the setup:
trait SomeTrait {
fn some_func(&self);
}
struct SomeStruct1;
impl SomeTrait for SomeStruct1 {
fn some_func(&self) {}
}
struct SomeStruct2;
impl SomeTrait for SomeStruct2 {
fn some_func(&self) {}
}
enum SomeEnum {
Value1(SomeStruct1),
Value2(SomeStruct2),
}
Here's one possibility I tried:
fn call_some_func(some_enum: SomeEnum) {
match some_enum {
SomeEnum::Value1(ref some_trait: &SomeTrait) |
SomeEnum::Value2(ref some_trait: &SomeTrait) => some_trait.some_func()
}
}
It resulted in this error:
error: expected one of `)`, `,`, or `@`, found `:`
--> src/main.rs:22:40
|
22 | SomeEnum::Value1(ref some_trait: &SomeTrait) |
| ^ expected one of `)`, `,`, or `@` here
This was another idea I had:
fn call_some_func2(some_enum: SomeEnum) {
match some_enum {
_(ref some_trait: &SomeTrait) => some_trait.some_func()
}
}
Which also failed:
error: expected one of `=>`, `if`, or `|`, found `(`
--> src/main.rs:22:10
|
22 | _(ref some_trait: &SomeTrait) => some_trait.some_func()
| ^ expected one of `=>`, `if`, or `|` her
Does anyone know of a way to accomplish this type of destructuring?