Skip to content

Commit f5c2b0b

Browse files
committed
Document repr packed(N).
1 parent 8bb0d86 commit f5c2b0b

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/type-layout.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ representations can be applied to a single type.
119119
The representation of a type can be changed by applying the `repr` attribute
120120
to it. The following example shows a struct with a `C` representation.
121121

122-
```
122+
```rust
123123
#[repr(C)]
124124
struct ThreeInts {
125125
first: i16,
@@ -148,7 +148,7 @@ There are no guarantees of data layout made by this representation.
148148

149149
The `C` representation is designed for dual purposes. One purpose is for
150150
creating types that are interoperable with the C Language. The second purpose is
151-
to create types that you can soundly performing operations that rely on data
151+
to create types that you can soundly perform operations on that rely on data
152152
layout such as reinterpreting values as a different type.
153153

154154
Because of this dual purpose, it is possible to create types that are not useful
@@ -205,7 +205,7 @@ The union will have a size of the maximum size of all of its fields rounded to
205205
its alignment, and an alignment of the maximum alignment of all of its fields.
206206
These maximums may come from different fields.
207207

208-
```
208+
```rust
209209
#[repr(C)]
210210
union Union {
211211
f1: u16,
@@ -280,19 +280,29 @@ The `align` representation can be used on `struct`s and `union`s to raise the
280280
alignment of the type to a given value.
281281

282282
Alignment is specified as a parameter in the form of `#[repr(align(x))]`. The
283-
alignment value must be a power of two of type `u32`. The `align` representation
284-
can raise the alignment of a type to be greater than it's primitive alignment,
285-
it cannot lower the alignment of a type.
283+
alignment value must be a power of two up to 2<sup>29</sup>. The `align`
284+
representation can raise the alignment of a type to be greater than it's
285+
primitive alignment, it cannot lower the alignment of a type.
286286

287287
The `align` and `packed` representations cannot be applied on the same type and
288288
a `packed` type cannot transitively contain another `align`ed type.
289289

290290
### The `packed` Representation
291291

292-
The `packed` representation can only be used on `struct`s and `union`s.
292+
The `packed` representation can be used on `struct`s and `union`s to lower the
293+
alignment and padding of the type.
294+
295+
The packing value is specified as a parameter in the form of
296+
`#[repr(packed(x))]`. If no value is given, as in `#[repr(packed)]`, then the
297+
packing value is 1. The packing value must be a power of two up to
298+
2<sup>29</sup>.
293299

294-
It modifies the representation (either the default or `C`) by removing any
295-
padding bytes and forcing the alignment of the type to `1`.
300+
The `packed` representation sets the alignment to the smaller of the specified
301+
packing and the current alignment (either default or `C`). The alignments of
302+
each field for the purpose of positioning fields would also be the smaller of
303+
the specified packing and the alignment of the type of that field. If the
304+
specified packing is greater than or equal to the default alignment of the
305+
type, then the alignment and layout is unaffected.
296306

297307
The `align` and `packed` representations cannot be applied on the same type and
298308
a `packed` type cannot transitively contain another `align`ed type.

0 commit comments

Comments
 (0)