@@ -8,12 +8,16 @@ not to change until an RFC ratifies them.
8
8
9
9
### Layout of individual union fields
10
10
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:
17
21
18
22
``` rust,ignore
19
23
[ P P [field0_ty] P P P P ]
0 commit comments