Skip to content

Commit 5a8a664

Browse files
committed
Specify guarantees for repr(rust) structs
1 parent 411c2f0 commit 5a8a664

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/type-layout.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ String slices are a UTF-8 representation of characters that have the same layout
8686

8787
## Tuple Layout
8888

89-
Tuples do not have any guarantees about their layout.
89+
Tuples have the same layout guarantees a struct with the same fields laid out
90+
according to the default struct representation.
9091

91-
The exception to this is the unit tuple (`()`) which is guaranteed as a
92+
The exception to this is the unit tuple (`()`), which is guaranteed as a
9293
zero-sized type to have a size of 0 and an alignment of 1.
9394

9495
## Trait Object Layout
@@ -162,7 +163,24 @@ representation will not change the layout of `Inner`.
162163
Nominal types without a `repr` attribute have the default representation.
163164
Informally, this representation is also called the `rust` representation.
164165

165-
There are no guarantees of data layout made by this representation.
166+
There are very few data layout guarantees made by this representation. The only
167+
guarantees are:
168+
169+
1. The fields of the struct are properly aligned.
170+
2. The fields do not overlap.
171+
3. The alignment of the struct is not less than the alignment of any of its
172+
fields.
173+
174+
Formally, the first guarantee means that the offset of any field in the struct
175+
is divisible by that field's alignment. The second guarantee means that the
176+
fields can be ordered such that the offset plus the size of any field is less
177+
than or equal to the offset of the next field in the ordering. The ordering does
178+
not have to be the same as the order in which the field are specified in the
179+
declaration of the struct.
180+
181+
Be aware that the second guarantee does not imply that the fields have distinct
182+
addresses: zero-sized types may have the same address as other fields in the
183+
same struct.
166184

167185
### The `C` Representation
168186

0 commit comments

Comments
 (0)