Skip to content

Commit a6ce041

Browse files
committed
talk about degrees of freedom
1 parent fee3cd4 commit a6ce041

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

reference/src/layout/structs-and-tuples.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,23 @@ struct Foo {
7272
(In fact, one may use such field names in patterns or in accessor
7373
expressions like `foo.0`.)
7474

75-
When layout out a struct, the compiler in particular has to decide how the
76-
fields of the struct are arranged, which can be visualized as follows:
75+
The degrees of freedom the compiler has when computing the layout of a struct or
76+
tuple is to determine the order of the fields, and the "gaps" before, between,
77+
and after the fields. The layout of these fields themselves is already entirely
78+
determined by their types, and since we intend to allow creating references to
79+
fields (`&s.f1`), structs do not have any wiggle-room there.
80+
81+
This can be visualized as follows:
7782
```text
78-
[ <--> [field 1] <-----> [field 2] <-> [ field 3 ] <--> ]
83+
[ <--> [field 3] <-----> [field 1] <-> [ field 2 ] <--> ]
7984
```
80-
The individual fields are blocks of fixed size (determined by the field's
85+
Here, the individual fields are blocks of fixed size (determined by the field's
8186
layout). The compiler freely picks an order for the fields to be in (this does
8287
not have to be the order of declaration in the source), and it picks the gaps
8388
between the fields (under some constraints, such as alignment).
8489

85-
What exactly the compiler does, as well as other aspects of layout beyond size
86-
and field offset, can be controlled by a `#[repr]` attribute:
90+
How exactly the compiler picks order and gaps, as well as other aspects of
91+
layout beyond size and field offset, can be controlled by a `#[repr]` attribute:
8792

8893
- `#[repr(Rust)]` -- the default.
8994
- `#[repr(C)]` -- request C compatibility

0 commit comments

Comments
 (0)