@@ -34,10 +34,11 @@ the `union`, etc.
34
34
35
35
### Unions with default layout ("` repr(Rust) ` ")
36
36
37
- The default layout of Rust unions is ** unspecified** .
37
+ Except for the guarantees provided below for some specific cases, the default
38
+ layout of Rust unions is, _ in general_ , ** unspecified** .
38
39
39
- That is, there are no guarantees about the offset of the fields, whether all
40
- fields have the same offset, etc.
40
+ That is, there are no _ general _ guarantees about the offset of the fields,
41
+ whether all fields have the same offset, etc.
41
42
42
43
<details ><summary ><b >Rationale</b ></summary >
43
44
@@ -59,8 +60,8 @@ you have to use `#[repr(C)]`.
59
60
The layout of unions with a single non-zero-sized field is the same as the
60
61
layout of that field if:
61
62
62
- * that field has no padding bits , and
63
- * the alignment requirement of all zero-sized fields is 1 .
63
+ * all zero-sized fields are [ 1-ZST ] , and
64
+ * the non-zero sized field has no padding bits .
64
65
65
66
For example, here:
66
67
@@ -83,7 +84,7 @@ union U0 {
83
84
84
85
the union ` U0 ` has the same layout as ` SomeStruct ` , because ` SomeStruct ` has no
85
86
padding bits - it is equivalent to an ` i32 ` due to ` repr(transparent) ` - and
86
- because the alignment of ` Zst ` is 1 .
87
+ because ` Zst ` is a [ 1-ZST ] .
87
88
88
89
On the other hand, here:
89
90
@@ -104,9 +105,10 @@ assert_eq!(align_of::<Zst2>(), 16);
104
105
# }
105
106
```
106
107
107
- the alignment requirement of ` Zst2 ` is not 1, and ` SomeOtherStruct ` has an
108
- unspecified layout and could contain padding bits. Therefore, the layout of ` U1 `
109
- is ** unspecified** .
108
+ the layout of ` U1 ` is ** unspecified** because:
109
+
110
+ * ` Zst2 ` is not a [ 1-ZST] , and
111
+ * ` SomeOtherStruct ` has an unspecified layout and could contain padding bits.
110
112
111
113
### C-compatible layout ("repr C")
112
114
@@ -117,3 +119,5 @@ a field type to obtain a pointer to any field, and vice versa.
117
119
118
120
[ 6.5.8.5 ] : http://port70.net/~nsz/c/c11/n1570.html#6.5.8p5
119
121
[ 6.7.2.1.16 ] : http://port70.net/~nsz/c/c11/n1570.html#6.7.2.1p16
122
+
123
+ [ 1-ZST ] : ../glossary.md#zero-sized-type--zst
0 commit comments