# Writing down binary data... with padding bytes

**URL:** <https://internals.rust-lang.org/t/writing-down-binary-data-with-padding-bytes/11197>\
**Category:** Uncategorized\
**Created:** [October 27, 2019, 5:48pm UTC](https://internals.rust-lang.org/t/writing-down-binary-data-with-padding-bytes/11197 "2019-10-27T17:48:25Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![felix.s](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/felix.s/32/8073_2.png) [@felix.s](https://internals.rust-lang.org/u/felix.s)\
**Post date:** [October 27, 2019, 8:32pm UTC](https://internals.rust-lang.org/t/writing-down-binary-data-with-padding-bytes/11197/3 "2019-10-27T20:32:26Z")

</div>

> [@HadrienG](#):
>
> May I request your opinion on if I have overlooked anything somewhere? In particular, since neither of the above options are particularly appealing, "third option" solutions that go for something completely different are much welcome.

~~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/conflation-of-alignment-and-packing/10443/14)

~~[https://internals.rust-lang.org/t/not-rouding-size-of-types-up-to-a-multiple-of-alignment/5106~~](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.

```rust
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`).

> [@sfackler](#):
>
> freeze doesn't actually work due to the existence of things like MADV\_FREE.

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

---

_[View the full topic](https://internals.rust-lang.org/t/writing-down-binary-data-with-padding-bytes/11197)._
