Skip to content

Commit 6502c2d

Browse files
committed
Summarzie text
1 parent 36bc3b6 commit 6502c2d

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

reference/src/layout/unions.md

+8-14
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@ not to change until an RFC ratifies them.
88

99
### Layout of individual union fields
1010

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:
2114

2215
```rust,ignore
2316
[ <--> [field0_ty] <----> ]
@@ -31,10 +24,11 @@ after each field. This can be visualized as follows:
3124
> `[field{i}_ty]` is the bytes of the type of the `i`-th union field.
3225
3326
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.
3832

3933
### Unions with default layout ("`repr(Rust)`")
4034

0 commit comments

Comments
 (0)