Skip to content

mention size-filling padding #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions reference/src/layout/unions.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ like
union U { f1: T1, f2: T2 }
```

is to determine the offset of the fields. The layout of these fields themselves
is to determine the offset of the fields. The layout of these fields themselves
is already entirely determined by their types, and since we intend to allow
creating references to fields (`&u.f1`), unions do not have any wiggle-room
there.
Expand Down Expand Up @@ -42,8 +42,11 @@ to obtain a pointer to any field, and vice versa.
#### Padding

Since all fields are at offset 0, `repr(C)` unions do not have padding before
their fields. They can, however, have trailing padding, to make sure the size is
a multiple of the alignment:
their fields. They can, however, have padding in each union variant *after* the
field, to make all variants have the same size.

Moreover, the entire union can have trailing padding, to make sure the size is a
multiple of the alignment:

```rust
# use std::mem::{size_of, align_of};
Expand Down