Java's EnumMap relates each enumeration to an index using it's ordinal. This ordinal also exists for Rust in the discriminant, which functions similarly in that they both spit out a unique value per variant. Both languages can and do implement a null-initialized array with a bitset denoting which slots are filled, which can be indexed by an ordinal/discriminant. This is, from my understanding, essentially an EnumMap. The extra data that you use above would also provide 0 extra overhead in the equivalent implementation in Rust as, just like Java, Rust would simply use the discriminant instead of looking at enum's data.
Also, a discriminant is not the value given to the enum by the user, but it is it's own value that is attached to each variant at compile time, the 8-bit field you mention would be data attached to a variant rather than being a discriminant.
Java's EnumMap relates each enumeration to an index using it's ordinal.
I think you meant to say EnumSet, and not EnumMap? What you are describing is an EnumSet, so I'll assume so for the rest of this comment.
The extra data that you use above would also provide 0 extra overhead in the equivalent implementation in Rust as, just like Java, Rust would simply use the discriminant instead of looking at enum's data.
The problem is, in Rust, if I make an enum where one of the values is Crono(hp: u8, attack: u8, defense: u8), there is nothing stopping me from making multiple instances of Crono. In my Java example from the comment you responded, there is, and always will be, only one instance of Crono -- there can't be multiple.
So how do we denote inclusion in the bitset? My argument is that, if you stick to using rust enums with state, you will have to find some way to index each instance, and if you do that, then you will be slower than what Java does.
But regardless, I promised a benchmark that is due in less than 24 hours (Ctrl+F RemindMe). I already finished the Java half of the benchmark, I'm just trying to do the rust part now.
Fun fact by the way -- all of the enumset implementations I found in Rust explicitly say that enums with state are not permitted to be used in their implementation lol. So, I'm not even sure how I am going to write a benchmark for the rust side, since all the enumset implementations explicitly choose not to support the use case that I am trying to benchmark lol.
I might just have to stick to like an identityset, which is the closest parallel there is. I'll list all of that in my benchmark findings.
View all comments
Show parent comment
2
u/4lineclear Oct 08 '25
Java's EnumMap relates each enumeration to an index using it's ordinal. This ordinal also exists for Rust in the discriminant, which functions similarly in that they both spit out a unique value per variant. Both languages can and do implement a null-initialized array with a bitset denoting which slots are filled, which can be indexed by an ordinal/discriminant. This is, from my understanding, essentially an EnumMap. The extra data that you use above would also provide 0 extra overhead in the equivalent implementation in Rust as, just like Java, Rust would simply use the discriminant instead of looking at enum's data.
Also, a discriminant is not the value given to the enum by the user, but it is it's own value that is attached to each variant at compile time, the 8-bit field you mention would be data attached to a variant rather than being a discriminant.