Hopefully a simple syntax question.
In a match, is there some way I can say "I don't care about the specific variant as long as it has an associated value"?
And assuming that AssocOne and AssocTwo both implement a trait Foo with function my_trait_func...
How do I do the match in the following?
impl Foo for Ex {
fn my_trait_func( &self, x : something ) {
match self {
VarOne => x.magic( "One" ),
VarTwo => x.magic( "Two" ),
_( t ) => t.my_trait_func( x ),
}
}
}
The above gives me the error: expected one of =>, if, or |, found (
If I change the _ to Ex I get the error: expected tuple struct or tuple variant, found enum 'Ex'
I wanted to avoid listing all the variants
I realise that traits are at the enum level, it is the associated AssocOne and AssocTwo that implement the traits, not their variants.
It's a pity that this isn't supported, it seems like a fairly useful/common thing to do...
Yes, but I had hoped that the pattern matching of match would mean I wouldn't need to list Var3 and Var4 explicitly and could instead do _(f) to match all variants with an associated value.
With out this, I have to add the variant to the enum and then also add it to impl. Whereas it should be easily provable at compile time that _(f) is legal if all variants have the right kind of f from a trait perspective... perhaps with the help of a where on the type of f
I think it again comes down to how trait objects are used in rust. By default all things are on the stack with a Known size. In your original example, the t doesn't have a Known size since it could be either AssocOne or AssocTwo. Box<dyn Trait> puts the thing on the Heap.