Rust seems to fail to take advantage of niches for very simple cases where it seems like it would be trivial to do so.
#[repr(u8)]
pub enum Bar {A = 0, B = 1}
enum Foo {
Two(Bar, Bar),
One(Bar)
}
fn main() {
println!("{}", std::mem::size_of::<Foo>());
}
This code gives a size of 3 for Foo while obviously it can be stored in 2 bytes, representing the Two variant as two consecutive byte-sized Bar values, and the One variant as a byte with any value other than 0 or 1 followed by a byte-size Bar value.
Using an array of two values for the Two variant, specifying non-zero discriminants for Bar and and using NonZeroU8 all seem to have no effect.
Rust seems to fail to take advantage of niches for very simple cases where it seems like it would be trivial to do so.
This code gives a size of 3 for Foo while obviously it can be stored in 2 bytes, representing the Two variant as two consecutive byte-sized Bar values, and the One variant as a byte with any value other than 0 or 1 followed by a byte-size Bar value.
Using an array of two values for the Two variant, specifying non-zero discriminants for Bar and and using NonZeroU8 all seem to have no effect.