Enums should magically get a trait for getting `discriminant` and `variant_count`

How would declaring their variant counts to be #{distinctValues} be any less blocking of other concepts?

In it's current form, variant_count can't return the number of possible values of

  • u32 and above on 16 bit systems
  • u64 and above on 32 bit systems
  • u128 on 64 bit systems
  • Many non-integers

How would you resolve that?

Another example:

#[repr(transparent)]
struct MyWeekday(DayOfWeek);

MyWeekday is supposed to have the same ABI as DayOfWeek here because of repr(transparent), and yet I’d still be surprised if this had a variant count of 7 rather than 1. I’m sure there are others who think differently, and that’s how we end up with the behavior on non-enums remaining unspecified. :frowning:

The number of possible values for u32 cannot be represented even in 32 bit systems, and the number of possible values for u64 cannot be represented even in 64 bit systems. AFAIK there are no 128 bit systems yet, but once we get there the number of possible values for u128> won't be representable there.

Because these types need to represent the zero, the maximum number they can represent is lower by one than the number of values they can represent.

2 Likes

I'd rather variant_count not be defined for fundamental types at all.

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