From d46db14097f7a8b762481823fdf1d64b76db5b45 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Wed, 9 Feb 2022 06:32:21 -0800 Subject: [PATCH 1/3] Clarify guarantees provided by repr(packed) Clarify that `repr(packed)` minimizes inter-field padding and, in the special case of `repr(packed(1))`/`repr(packed)`, guarantees no inter-field padding. --- src/type-layout.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/type-layout.md b/src/type-layout.md index 22b5752bb..252bdbf91 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -515,6 +515,11 @@ For `packed`, if the specified alignment is greater than the type's alignment without the `packed` modifier, then the alignment and layout is unaffected. The alignments of each field, for the purpose of positioning fields, is the smaller of the specified alignment and the alignment of the field's type. +Inter-field padding is guaranteed to be the minimum required in order to +satisfy each field's (possibly altered) alignment (although note that, on its +own, `packed` does not provide any guarantee about field ordering). An +important special case is `#[repr(packed(1))]` (or `#[repr(packed)]`), which +guarantees that no inter-field padding will be present. The `align` and `packed` modifiers cannot be applied on the same type and a `packed` type cannot transitively contain another `align`ed type. `align` and From 4f2cc957eb2a698b5c177e1a011dcaf98ab7ca41 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Wed, 9 Feb 2022 06:33:54 -0800 Subject: [PATCH 2/3] Update type-layout.md --- src/type-layout.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/type-layout.md b/src/type-layout.md index 252bdbf91..f45c960d4 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -501,7 +501,7 @@ assert_eq!(std::mem::size_of::(), 4); The `align` and `packed` modifiers can be used to respectively raise or lower the alignment of `struct`s and `union`s. `packed` may also alter the padding -between fields. +between fields (although it will not alter the padding inside of any field). The alignment is specified as an integer parameter in the form of `#[repr(align(x))]` or `#[repr(packed(x))]`. The alignment value must be a From c7bbfaf9ab410e1ff27b37d9d098ab3e1f498ebe Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Mon, 21 Mar 2022 12:45:51 -0700 Subject: [PATCH 3/3] Use "An important consequence" language --- src/type-layout.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index f45c960d4..ce9296662 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -517,9 +517,9 @@ The alignments of each field, for the purpose of positioning fields, is the smaller of the specified alignment and the alignment of the field's type. Inter-field padding is guaranteed to be the minimum required in order to satisfy each field's (possibly altered) alignment (although note that, on its -own, `packed` does not provide any guarantee about field ordering). An -important special case is `#[repr(packed(1))]` (or `#[repr(packed)]`), which -guarantees that no inter-field padding will be present. +own, `packed` does not provide any guarantee about field ordering). An +important consequence of these rules is that a type with `#[repr(packed(1))]` +(or `#[repr(packed)]`) will have no inter-field padding. The `align` and `packed` modifiers cannot be applied on the same type and a `packed` type cannot transitively contain another `align`ed type. `align` and