this post was submitted on 02 Jul 2021
13 points (100.0% liked)

Rust Programming

8306 readers
16 users here now

founded 5 years ago
MODERATORS
 

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());
}

Playground link

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?

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 3 years ago (4 children)

You are right, if you are using an enum for tons of different types, you pretty much have no choice but to use a match.

Actor to me there shouldn't be an enum, but a parent trait or struct, that all of those implement, with a name function.

[–] [email protected] 1 points 3 years ago (3 children)

The reason I am using an enum is because I am deserializing this data with serde, which works perfectly as I only need to call serde_json::from_str() once. Without the enum, I would have to execute it once for every struct. In another case I am using the same pattern to deserialize into 30+ different structs, so I would need to call serde_json::from_str() 30+ times in the worst case, until I try the right struct.

I guess generating the match with a macro is the best solution.

[–] [email protected] 2 points 3 years ago (1 children)

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.

[–] [email protected] 2 points 3 years ago (1 children)

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?

[–] [email protected] 2 points 3 years ago* (last edited 3 years ago)
load more comments (1 replies)
load more comments (1 replies)