@@ -8,16 +8,9 @@ not to change until an RFC ratifies them.
8
8
9
9
### Layout of individual union fields
10
10
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:
11
+ A union consists of several variants, one for each field. All variants have the
12
+ same size and start at the same memory address, such that in memory the variants
13
+ overlap. This can be visualized as follows:
21
14
22
15
``` rust,ignore
23
16
[ <--> [field0_ty] <----> ]
@@ -31,10 +24,11 @@ after each field. This can be visualized as follows:
31
24
> ` [field{i}_ty] ` is the bytes of the type of the ` i ` -th union field.
32
25
33
26
The individual fields (` [field{i}_ty_] ` ) are blocks of fixed size determined by
34
- the field's layout. The compiler picks the offset of the fields with respect to
35
- the union and the ` union ` size according to certain constraints like, for
36
- example, the alignment requirements of the fields, the ` #[repr] ` attribute of
37
- the ` union ` , etc.
27
+ the field's layout. The only degrees of freedom the compiler has when computing
28
+ the layout of a union are the size of the union and the offset of each union
29
+ field within its variant. How these are picked depends on certain constraints,
30
+ lik for example, the alignment requirements of the fields, the ` #[repr] `
31
+ attribute of the ` union ` , etc.
38
32
39
33
### Unions with default layout ("` repr(Rust) ` ")
40
34
0 commit comments