Fast convert from any unsigned int to enum?

Thanks everyone for your input and suggestions on this problem. It seemed there was no way to do it generically, or with the ergonomics I was hoping for. I went a different way, but I tried to think of which suggested solution I'd use if I could. I'm not sure, so I "liked" a few of them.

I also appreciate the crates suggested.

The num_enum and strum crates have a couple macros that might've worked for part of the solution, but I wasn't excited about bringing in 16k lines of dependency code for it. If I didn't have a custom syntax I'm working with for the enum definition, and doing a couple other things there, I might have used the enum_map crate.

I ended up doing the following..

The enum will correspond to an array of objects for each variant. So, instead of my library function sending back a subset of enum variants that index into the array, I added a bool to the objects to indicate if it was selected. For my use case, it means the library doesn't need to care about the enum business and just loop through objects and mark them as selected.

It's not the best, but since I also need a macro to read the enum definition, I can generate a fn get(variant: SomeEnum) -> Option<ObjectFromArray> for the enum or a wrapper struct to see if a variant was selected.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.