Rust allows you to create an enum with a variant name and an associated constant of the same name:
enum Foo {
Bar,
}
impl Foo {
const Bar: u8 = 1;
}
However, I can't figure out if there's any way to refer to the constant by name (e.g., see here). Both Self::Bar and Foo::Bar seem to refer to the variant. Is there any way around this?
Tried for 30 minutes and haven't managed to figure it out. What are you using this for by the way?
It is possible to specify the enum representation, in case it solves your use case:
I wanted to avoid having the user to have to specify both the variant name (Tcp) and the constant name (TCP). However, I ended up realizing that I could just use the numeric values directly without using constants at all. The constants made sense before when we were writing these by hand and didn't want to make a mistake in writing down numeric literals, but they don't make sense when we're automatically generating the code from a macro. So it's not an issue I need to solve anymore.
For the curious, here's what I ended up coming up with.