@@ -110,16 +110,18 @@ Closures have no layout guarantees.
110110## Representations
111111
112112All user-defined composite types (` struct ` s, ` enum ` s, and ` union ` s) have a
113- * representation* that specifies what the layout is for the type.
113+ * representation* that specifies what the layout is for the type. The possible
114+ representations for a type are:
114115
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+ - [ Default]
117+ - [ ` C ` ]
118+ - The [ primitive representations]
119+ - [ ` transparent ` ]
118120
119121The representation of a type can be changed by applying the ` repr ` attribute
120122to it. The following example shows a struct with a ` C ` representation.
121123
122- ```
124+ ``` rust
123125#[repr(C )]
124126struct ThreeInts {
125127 first : i16 ,
@@ -128,14 +130,37 @@ struct ThreeInts {
128130}
129131```
130132
133+ The alignment may be raised or lowered with the ` align ` and ` packed ` modifiers
134+ respectively. They alter the representation specified in the attribute.
135+ If no representation is specified, the default one is altered.
136+
137+ ``` rust
138+ // Default representation, alignment lowered to 2.
139+ #[repr(packed(2))]
140+ struct PackedStruct {
141+ first : i16 ,
142+ second : i8 ,
143+ third : i32
144+ }
145+
146+ // C representation, alignment raised to 8
147+ #[repr(C , align(8))]
148+ struct AlignedStruct {
149+ first : i16 ,
150+ second : i8 ,
151+ third : i32
152+ }
153+ ```
154+
131155> Note: As a consequence of the representation being an attribute on the item,
132156> the representation does not depend on generic parameters. Any two types with
133157> the same name have the same representation. For example, ` Foo<Bar> ` and
134158> ` Foo<Baz> ` both have the same representation.
135159
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.
160+ The representation of a type can change the padding between fields, but does
161+ not change the layout of the fields themselves. For example, a struct with a
162+ ` C ` representation that contains a struct ` Inner ` with the default
163+ representation will not change the layout of ` Inner ` .
139164
140165### The Default Representation
141166
@@ -148,7 +173,7 @@ There are no guarantees of data layout made by this representation.
148173
149174The ` C ` representation is designed for dual purposes. One purpose is for
150175creating 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
176+ to create types that you can soundly perform operations on that rely on data
152177layout such as reinterpreting values as a different type.
153178
154179Because of this dual purpose, it is possible to create types that are not useful
@@ -274,28 +299,28 @@ For all other enumerations, the layout is unspecified.
274299
275300Likewise, combining two primitive representations together is unspecified.
276301
277- ### The ` align ` Representation
278-
279- The ` align ` representation can be used on ` struct ` s and ` union ` s to raise the
280- alignment of the type to a given value.
281-
282- 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.
302+ ### The alignment modifiers
286303
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.
304+ The ` align ` and ` packed ` modifiers can be used to respectively raise or lower
305+ the alignment of ` struct ` s and ` union ` s. ` packed ` may also alter the padding
306+ between fields.
289307
290- ### The ` packed ` Representation
308+ The alignment is specified as an integer parameter in the form of
309+ ` #[repr(align(x))] ` or ` #[repr(packed(x))] ` . The alignment value must be a
310+ power of two from 1 up to 2<sup >29</sup >. For ` packed ` , if no value is given,
311+ as in ` #[repr(packed)] ` , then the value is 1.
291312
292- The ` packed ` representation can only be used on ` struct ` s and ` union ` s.
313+ For ` align ` , if the specified alignment is less than the alignment of the type
314+ without the ` align ` modifier, then the alignment is unaffected.
293315
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 ` .
316+ For ` packed ` , if the specified alignment is greater than the type's alignment
317+ without the ` packed ` modifier, then the alignment and layout is unaffected.
318+ The alignments of each field, for the purpose of positioning fields, is the
319+ smaller of the specified alignment and the alignment of the field's type.
296320
297- The ` align ` and ` packed ` representations cannot be applied on the same type and
298- a ` packed ` type cannot transitively contain another ` align ` ed type.
321+ The ` align ` and ` packed ` modifiers cannot be applied on the same type and a
322+ ` packed ` type cannot transitively contain another ` align ` ed type. ` align ` and
323+ ` packed ` may only be applied to the [ default] and [ ` C ` ] representations.
299324
300325<div class =" warning " >
301326
@@ -333,3 +358,7 @@ used with any other representation.
333358[ undefined behavior ] : behavior-considered-undefined.html
334359[ 27060 ] : https://github.com/rust-lang/rust/issues/27060
335360[ `PhantomData<T>` ] : special-types-and-traits.html#phantomdatat
361+ [ Default ] : #the-default-representation
362+ [ `C` ] : #the-c-representation
363+ [ primitive representations ] : #primitive-representations
364+ [ `transparent` ] : #the-transparent-representation
0 commit comments