Here is a simplified example of my problem:
struct User;
struct Community;
enum Actor {
User(User),
Community(Community),
}
trait Name {
fn name() -> &'static str;
}
impl Name for User {
fn name() -> &'static str { "/u/nutomic" }
}
impl Name for Community {
fn name() -> &'static str { "/c/rust " }
}
fn main() {
let actor = Actor::Community(Community);
println!("{}", actor.name());
}
As you may have noticed, this doesnt compile. The only solution I can think of is with a match. I want to avoid that because I have an enum with 30+ variants, and a trait with multiple methods. So that would be a huge match statement in each method, when the compiler should know that the enum variants all have the same trait.
So, do you know any way to make this work without match?
If you don’t care whether the public-facing type is a struct or an enum, you could make the enum private and use
#[serde(from="PrivateEnum")]
on the public struct.I dont care what the public type is, so that sounds perfect. But its not clear to me from the documentation how that attribute works. Do you happen to know an example?
Hmm… so you still need a single match block.