Hello
Does the following type has a guarantied representation?
std::result::Result<(), std::num::NonZeroU32>
On the playground it is 4 bytes but I wonder if I can assume it is or not.
Yes, it comes under something called "niche optimisation".
The NonZeroU32 type is guaranteed to have a niche (the bit pattern 0x00 will never be used), so the compiler is smart enough to use that to represent Ok(()) because () is a zero-sized type.
Note that while this specific case is guaranteed by the compiler, a large number of cases like this are not. The vast majority of things have no guarantee of any kind, and it should not be assumed unless you're positive.
In my reading, it is not explicitly guaranteed for this case - because the guidelines talk about a unit variant. Ok(()) is functionally equivalent to that, but I wouldn't read the guarantee as applying to that, without a more explicit explanation.
My interpretation is that when they say "unit variant" they mean "variant which contains no data", which applies to all ZST.