Skip to content

Commit 2866fda

Browse files
committed
Documented the #[repr(align(x))] attribute.
1 parent 36fc52c commit 2866fda

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

src/attributes.md

+3-21
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,8 @@ interpreted:
130130
- `linkage` - on a static, this specifies the [linkage
131131
type](http://llvm.org/docs/LangRef.html#linkage-types).
132132

133-
On `enum`s:
134-
135-
- `repr` - on C-like enums, this sets the underlying type used for
136-
representation. Takes one argument, which is the primitive
137-
type this enum should be represented for, or `C`, which specifies that it
138-
should be the default `enum` size of the C ABI for that platform. Note that
139-
enum representation in C is undefined, and this may be incorrect when the C
140-
code is compiled with certain flags.
141-
142-
On `struct`s:
143-
144-
- `repr` - specifies the representation to use for this struct. Takes a list
145-
of options. The currently accepted ones are `C` and `packed`, which may be
146-
combined. `C` will use a C ABI compatible struct layout, and `packed` will
147-
remove any padding between fields (note that this is very fragile and may
148-
break platforms which require aligned access).
149-
150-
On `union`s:
151-
152-
- `repr` - Same as per `struct`.
133+
See [type layout](type-layout.html) for documentation on the `repr` attribute
134+
which can be used to control type layout.
153135

154136
## Macro-related attributes
155137

@@ -416,4 +398,4 @@ You can implement `derive` for your own type through [procedural macros].
416398

417399
[Doc comments]: comments.html#doc-comments
418400
[The Rustdoc Book]: ../rustdoc/the-doc-attribute.html
419-
[procedural macros]: procedural-macros.html
401+
[procedural macros]: procedural-macros.html

src/type-layout.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,23 @@ For all other enumerations, the layout is unspecified.
264264

265265
Likewise, combining two primitive representations together is unspecified.
266266

267-
### The `packed` Representation
267+
### `align(x)` Representation
268+
269+
The `align(x)` representation can be used on `struct`s and `union`s to raise the
270+
alignment of the type to the given value. The alignment value must be a power of
271+
two of type `u32`. The `align` representation cannot lower the alignment of a
272+
type.
273+
274+
### `packed` Representation
268275

269276
The `packed` representation can only be used on `struct`s and `union`s.
270277

271278
It modifies the representation (either the default or `C`) by removing any
272279
padding bytes and forcing the alignment of the type to `1`.
273280

281+
The `align` and `packed` representations cannot be applied on the same type and
282+
a `packed` type cannot transitively contain another `align`ed type.
283+
274284
> Warning: Dereferencing an unaligned pointer is [undefined behaviour] and it is
275285
> possible to [safely create unaligned pointers to `packed` fields][27060].
276286
> Like all ways to create undefined behavior in safe Rust, this is a bug.
@@ -284,4 +294,4 @@ padding bytes and forcing the alignment of the type to `1`.
284294
[C-like enumerations]: items/enumerations.html#c-like-enumerations
285295
[zero-variant enumerations]: items/enumerations.html#zero-variant-enumerations
286296
[undefined behavior]: behavior-considered-undefined.html
287-
[27060]: https://github.com/rust-lang/rust/issues/27060
297+
[27060]: https://github.com/rust-lang/rust/issues/27060

0 commit comments

Comments
 (0)