Skip to content

Commit d029b86

Browse files
authored
Merge pull request #1163 from joshlf/patch-1
Clarify guarantees provided by repr(packed)
2 parents 9f0de65 + c7bbfaf commit d029b86

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/type-layout.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ assert_eq!(std::mem::size_of::<Enum16>(), 4);
501501

502502
The `align` and `packed` modifiers can be used to respectively raise or lower
503503
the alignment of `struct`s and `union`s. `packed` may also alter the padding
504-
between fields.
504+
between fields (although it will not alter the padding inside of any field).
505505

506506
The alignment is specified as an integer parameter in the form of
507507
`#[repr(align(x))]` or `#[repr(packed(x))]`. The alignment value must be a
@@ -515,6 +515,11 @@ For `packed`, if the specified alignment is greater than the type's alignment
515515
without the `packed` modifier, then the alignment and layout is unaffected.
516516
The alignments of each field, for the purpose of positioning fields, is the
517517
smaller of the specified alignment and the alignment of the field's type.
518+
Inter-field padding is guaranteed to be the minimum required in order to
519+
satisfy each field's (possibly altered) alignment (although note that, on its
520+
own, `packed` does not provide any guarantee about field ordering). An
521+
important consequence of these rules is that a type with `#[repr(packed(1))]`
522+
(or `#[repr(packed)]`) will have no inter-field padding.
518523

519524
The `align` and `packed` modifiers cannot be applied on the same type and a
520525
`packed` type cannot transitively contain another `align`ed type. `align` and

0 commit comments

Comments
 (0)