Writing down binary data... with padding bytes

Separate stride from size in the language. It has been discussed before:

~~Conflation of alignment and packing? - #14 by RalfJung

~~https://internals.rust-lang.org/t/not-rouding-size-of-types-up-to-a-multiple-of-alignment/5106~~

It would also bring other benefits, like the ability to stuff data into the space that today is wasted on padding bytes. However, I feel actually delivering this feature might be even harder that either of the aforementioned ‘unappealing’ options.

And even then, it would not work for #[repr(C)] types, which may contain inner padding bytes.

Never mind, I realised this would not help even for Rust ABI types. It's still possible to have internal padding even when stride ≠ size.

struct X {
    a: u32,
    b: u8,
}

struct Y {
    a: X,
    b: u16,
}

Assuming X is laid out as (u32, u8) (the obvious choice with no padding at all), you can either lay out Y as (X, u16) (which will have 1 byte of padding after X), or as (u16, X) (which will have two bytes of padding before X).

Well, at least it cannot compile to a no-op, anyway.

1 Like