Pre-RFC Compile Time Enum Variant Iterator

So I’ve had a few enums that I’ve made that have tons of different variants. Something that I’ve wanted for a long time is a way to say for all variants of this enum do the same thing on each of them without having to write an explicit match for each. I know rust can’t do run time reflection but I think compile time support for this would be perfectly fine.

Example: https://is.gd/RzMDP8

Why not just write a small macro for this?

You can also take a look at the "static foreach" of D language. It's a more general solution.

I actually found a crate called enum_derive that I’m going to try. I think it might be able to do what I’m after. Edit: Looks like it can’t handle enums that have payload variants. So that won’t work

Sure it does. You want EnumInnerAsTrait! with Display. Hell, the example of it it pretty much exactly what you asked for. An iterator will never work, first because the payload types aren’t the same, and secondly because the iterator cannot simply summon new payloads out of thin air.

1 Like

@DanielKeep I took a closer look at your docs and I think that is exactly what I’m after. Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.