Forbidden/niche values

Don't we have two kinds of niche values? Most of the solution only appears to be able to solve one kind of it.

  • niche value that operates on bit level, for example 10000000 where 1 is the niche value no matter what values the 0 have, this is sometimes used as a hack for pointers since the last bit is usable IIRC, see GitHub - maciejhirsz/beef: Faster, more compact implementation of std: :Cow for an example
  • niche value that operates on value level / context, for example number 0 of u8 (only 00000000 is valid), most of the proposed solution belongs to this category I believe, an example is Option and NonZero*

While at it we may also need to take care of large niche value space, like having every a niche value for every prime numbers, ideally these would all be compile-time only to avoid foot guns like reflection magic.

Maybe instead of trying to explain to the compiler where's the niche, there could be a run-time hook for packing/unpacking values from a custom enum representation?

I have no idea how it could look like syntax-wise, but the general idea is that you could implement some trait with "set enum to this value" and "get me current value of this enum" methods, and these could perform arbitrary work (maybe const) to pack the data however they want.

Hi, it surely wouldn't be just the enum right? What types is this approach intended to pack together? Niches seem all about making A * B type take less than sizeof(A) + sizeof(B) bytes, so what are A and B?

What I had in mind is a type like union U {A, B} plus a function that knows whether it's currently holding A or B, so you can have fn get_discriminant(&U) -> Discriminant<U> or maybe fn get_a(&U) -> Option<&A>.

1 Like

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