Skip to content

Commit 7f4ca2f

Browse files
committed
Reword more
1 parent 094b801 commit 7f4ca2f

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

reference/src/layout/unions.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ not to change until an RFC ratifies them.
88

99
### Layout of individual union fields
1010

11-
The main degree of freedom the compiler has when computing the layout of a union
12-
is to determine the offset of the fields. Union fields are laid out by the
13-
compiler "on top" of each other, that is, the bytes of one field might overlap
14-
with the bytes of another field - as opposed to, e.g., `struct`s, where the
15-
fields are laid out "next to" each other, such that the bytes of one field never
16-
overlap with the bytes of another field. This can be visualized as follows:
11+
A union consists of several variants, one for each field. These variants are
12+
laid out "on top" of each other, so they must all have the same size. That is,
13+
the byte of each variant overlaps with the byte located at the same offset in
14+
all other variants, and the bytes of one field might overlap with the bytes of
15+
another field - as opposed to, e.g., `struct`s, where the fields are laid out
16+
"next to" each other, such that the bytes of one field never overlap with the
17+
bytes of another field. The main degree of freedom the compiler has when
18+
computing the layout of a union is to pick where in its variant each field is
19+
situated, i.e., the compiler picks the gap (often called padding) before and
20+
after each field. This can be visualized as follows:
1721

1822
```rust,ignore
1923
[ P P [field0_ty] P P P P ]

0 commit comments

Comments
 (0)