Skip to content

Commit d2acc38

Browse files
authored
Merge pull request #1152 from Darksonn/type-layout-repr-rust-field-alignment
Specify guarantees for repr(rust) structs
2 parents 9d0d3c0 + 1ae7c2d commit d2acc38

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/type-layout.md

+21-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ String slices are a UTF-8 representation of characters that have the same layout
8888

8989
## Tuple Layout
9090

91-
Tuples do not have any guarantees about their layout.
91+
Tuples are laid out according to the [default representation][Default].
9292

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

9696
## Trait Object Layout
@@ -164,7 +164,25 @@ representation will not change the layout of `Inner`.
164164
Nominal types without a `repr` attribute have the default representation.
165165
Informally, this representation is also called the `rust` representation.
166166

167-
There are no guarantees of data layout made by this representation.
167+
The only data layout guarantees made by this representation are those required
168+
for soundness. They are:
169+
170+
1. The fields are properly aligned.
171+
2. The fields do not overlap.
172+
3. The alignment of the type is at least the maximum alignment of its fields.
173+
174+
Formally, the first guarantee means that the offset of any field is divisible by
175+
that field's alignment. The second guarantee means that the fields can be
176+
ordered such that the offset plus the size of any field is less than or equal to
177+
the offset of the next field in the ordering. The ordering does not have to be
178+
the same as the order in which the fields are specified in the declaration of
179+
the type.
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.
184+
185+
There are no other guarantees of data layout made by this representation.
168186

169187
### The `C` Representation
170188

0 commit comments

Comments
 (0)