This will eventually have way more states than the ones described above. I wanted to differentiate which states are manual from the ones that are automatic. Traditionally, in other languages I used to have a class for each state and they would implement an interface that would force the implementing class to define whether the manual flag would be set for each state.
What would be the best way to achieve a similar effect based on an enum in Rust?
Meta code to illustrate the construction I'm trying to achieve:
let s: State = State::Manual { description: "Something", next_state: "next" };
if s.manual { // (or if s.is_manual() or if some_manual_indicator.contains(State::Manual) or ?)
/// do something
} else {
// do something else
}
If you were to use an interface in another language, you probably want to have a look at generics and traits in Rust. This is especially true if either:
the number of variants will grow high;
or the variants are basically independent of each other except for this one property;
or you don't need the dynamic dispatch (i.e. you want to write generic code, but at a given point, you'll only operate on a single type when using said code).
Although I don't think it's the only way to "force" each variant to declare whether it's manual. You can also achieve that by writing an is_manual() method that returns a bool, and then pattern matching on the enum value itself. The compiler won't allow you to have non-exhaustive patterns, so if you ever add a variant to your type, you'll have to decide what to return from that method when you encounter it, in order to get your code to compile.
Using matching is exactly what I did, perhaps you missed my current code at the end (my fault as I should have posted it more towards the beginning of the post):
Technically, yes, but if you want to ensure that you make a conscious decision about every case, then it's advisable to list each variant explicitly.
If on the other hand you know that the design of your library requires that State::Manual will be the only manual kind, then you can simplify the above code further to
Having another manual state will happen but will indeed be an exception, I will think about whether to go with this whitelisting approach vs. having it implement every case explicitly. For now I think having it based on exceptions will be good and I'll make sure it's properly documented.
Now this was worth the whole thread, love learning about this macro even though I don't think I'll be able to use it, since we'll have more manual states.
All and all really really appreciate all the insight here!