Skip to content

Commit 85c89aa

Browse files
committed
mention size-filling padding
1 parent f438c09 commit 85c89aa

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Diff for: reference/src/layout/unions.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ like
1313
union U { f1: T1, f2: T2 }
1414
```
1515

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

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

4851
```rust
4952
# use std::mem::{size_of, align_of};

0 commit comments

Comments
 (0)