Match multiple types as &Debug


#1

Is it possible to do something like this?

            SendEventError(ref err as &Debug) |
            SerialisationError(ref err as &Debug) => err.fmt(f),

i.e. I have an enum wrapping multiple types, each implementing Debug, and I’d like an easy way of matching and printing a bunch of them like this. (I can’t derive(Debug) either because I want to customise some entries.)


#2

What’s the static type of err for both of those variants? Can you paste the enum definition with those 2?


#3

Why does it matter?

These are just a couple of the enum entries from this list. I think there’s seven of them just wrap another error type which implements Debug.
routing::error


#4

Ah sorry, the as &Debug threw me for a bit - I see what you’re saying now. Do all variants have something to Debug display? Maybe you can implement Debug for the whole enum manually, and do the customization there?


#5

Yes, I’m implementing Debug (or Display) for the whole enum manually. Just slightly tedious with so many options but never mind.


#6

Are you doing the same thing to each variant? Try something like this:

let inner = match *self {
    A(ref val) => val as &Debug,
    B(ref val) => val as &Debug,
};
do_something_with(inner);

#7

With a few of the variants, yes, but not with all, no.


#8
let inner = match *self {
    A(ref val) => val as &Debug,
    B(ref val) => val as &Debug,
    _ => return;
};
do_something_with(inner);

Does that work? Any variant other than A or B will just terminate the function early.