Description
I tried this code:
fn main() {
println!("{}", std::mem::size_of::<Result<char,u16>>());
}
I expected to see this happen:
Expected it to print 4. Expected the Ok
case to have the same representation as char
. Expected the Err
case to have a non-zero discriminant value in the upper 11 bits (upper 11 bits for char
are always zero) and expected the u16
to reside in the two least significant bytes.
Instead, this happened:
Got 8, i.e. the compiler didn't make good use the top 11 bits of 32 bits that u16
doesn't occupy at all and that are zero for char
, since char
only uses the lower 21 bits.
There should be enough available discriminant bits that Option<Result<char,u16>>
could have size 4, too.
Use case: Surfacing the unpaired surrogate as Err
when decoding UTF-16.
Meta
Tried the current stable and nightly on https://play.rust-lang.org/?version=nightly&mode=release&edition=2021
It said:
Stable channel
Build using the Stable version: 1.74.0
Beta channel
Build using the Beta version: 1.75.0-beta.3
(2023-11-20 b66b795)
Nightly channel
Build using the Nightly version: 1.76.0-nightly
(2023-11-26 6cf0888)