@@ -113,8 +113,7 @@ All user-defined composite types (`struct`s, `enum`s, and `union`s) have a
113
113
* representation* that specifies what the layout is for the type.
114
114
115
115
The possible representations for a type are the default representation, ` C ` ,
116
- the primitive representations, ` packed ` , and ` transparent ` . Multiple
117
- representations can be applied to a single type.
116
+ the primitive representations, and ` transparent ` .
118
117
119
118
The representation of a type can be changed by applying the ` repr ` attribute
120
119
to it. The following example shows a struct with a ` C ` representation.
@@ -128,14 +127,36 @@ struct ThreeInts {
128
127
}
129
128
```
130
129
130
+ The alignment may be raised or lowered with the ` align ` and ` packed ` modifiers
131
+ respectively. They alter the representation specified in the attribute:
132
+
133
+ ``` rust
134
+ // Default representation, alignment lowered to 2.
135
+ #[repr(packed(2))]
136
+ struct PackedStruct {
137
+ first : i16 ,
138
+ second : i8 ,
139
+ third : i32
140
+ }
141
+
142
+ // C representation, alignment raised to 8
143
+ #[repr(C , align(8))]
144
+ struct AlignedStruct {
145
+ first : i16 ,
146
+ second : i8 ,
147
+ third : i32
148
+ }
149
+ ```
150
+
131
151
> Note: As a consequence of the representation being an attribute on the item,
132
152
> the representation does not depend on generic parameters. Any two types with
133
153
> the same name have the same representation. For example, ` Foo<Bar> ` and
134
154
> ` Foo<Baz> ` both have the same representation.
135
155
136
- The representation of a type does not change the layout of its fields. For
137
- example, a struct with a ` C ` representation that contains a struct ` Inner ` with
138
- the default representation will not change the layout of Inner.
156
+ The representation of a type can change the padding between fields, but does
157
+ not change the layout of the fields themselves. For example, a struct with a
158
+ ` C ` representation that contains a struct ` Inner ` with the default
159
+ representation will not change the layout of Inner.
139
160
140
161
### The Default Representation
141
162
@@ -274,38 +295,42 @@ For all other enumerations, the layout is unspecified.
274
295
275
296
Likewise, combining two primitive representations together is unspecified.
276
297
277
- ### The ` align ` Representation
298
+ ### The ` align ` modifier
278
299
279
- The ` align ` representation can be used on ` struct ` s and ` union ` s to raise the
300
+ The ` align ` modifier can be used on ` struct ` s and ` union ` s to raise the
280
301
alignment of the type to a given value.
281
302
282
- Alignment is specified as a parameter in the form of ` #[repr(align(x))] ` . The
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.
303
+ The alignment is specified as an integer parameter in the form of
304
+ ` #[repr(align(x))] ` . The alignment value must be a power of two from 1 up to
305
+ 2<sup >29</sup >.
306
+
307
+ The ` align ` modifier raises the type's alignment to the specified alignment.
308
+ If the specified alignment is less than the alignment of the type without the
309
+ ` align ` modifier, then the alignment is unaffected.
286
310
287
- The ` align ` and ` packed ` representations cannot be applied on the same type and
288
- a ` packed ` type cannot transitively contain another ` align ` ed type.
311
+ The ` align ` and ` packed ` modifiers cannot be applied on the same type and a
312
+ ` packed ` type cannot transitively contain another ` align ` ed type. ` align ` may
313
+ only be applied to the default and ` C ` representations.
289
314
290
- ### The ` packed ` Representation
315
+ ### The ` packed ` modifier
291
316
292
- The ` packed ` representation can be used on ` struct ` s and ` union ` s to lower the
293
- alignment and padding of the type.
317
+ The ` packed ` modifier can be used on ` struct ` s and ` union ` s to lower the
318
+ alignment of the type to a given value .
294
319
295
- The packing value is specified as a parameter in the form of
320
+ The packing value is specified as an integer parameter in the form of
296
321
` #[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
322
+ packing value is 1. The packing value must be a power of two from 1 up to
298
323
2<sup >29</sup >.
299
324
300
- The ` packed ` representation sets the type's 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, is the smaller of
303
- the specified packing and the alignment of the field's type. 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.
325
+ The ` packed ` modifier lowers the type's alignment to the specified packing. If
326
+ the specified packing is greater to the alignment of the type without the
327
+ ` packed ` modifier, then the alignment and layout is unaffected. The alignments
328
+ of each field, for the purpose of positioning fields, is the smaller of the
329
+ specified packing and the alignment of the field's type.
306
330
307
- The ` align ` and ` packed ` representations cannot be applied on the same type and
308
- a ` packed ` type cannot transitively contain another ` align ` ed type.
331
+ The ` align ` and ` packed ` modifiers cannot be applied on the same type and a
332
+ ` packed ` type cannot transitively contain another ` align ` ed type. ` packed ` may
333
+ only be applied to the default and ` C ` representations.
309
334
310
335
<div class =" warning " >
311
336
0 commit comments