Skip to content

Commit

Permalink
Documented the #[repr(align(x))] attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitshifter committed Dec 27, 2017
1 parent 36fc52c commit 2866fda
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
24 changes: 3 additions & 21 deletions src/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,8 @@ interpreted:
- `linkage` - on a static, this specifies the [linkage
type](http://llvm.org/docs/LangRef.html#linkage-types).

On `enum`s:

- `repr` - on C-like enums, this sets the underlying type used for
representation. Takes one argument, which is the primitive
type this enum should be represented for, or `C`, which specifies that it
should be the default `enum` size of the C ABI for that platform. Note that
enum representation in C is undefined, and this may be incorrect when the C
code is compiled with certain flags.

On `struct`s:

- `repr` - specifies the representation to use for this struct. Takes a list
of options. The currently accepted ones are `C` and `packed`, which may be
combined. `C` will use a C ABI compatible struct layout, and `packed` will
remove any padding between fields (note that this is very fragile and may
break platforms which require aligned access).

On `union`s:

- `repr` - Same as per `struct`.
See [type layout](type-layout.html) for documentation on the `repr` attribute
which can be used to control type layout.

## Macro-related attributes

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

[Doc comments]: comments.html#doc-comments
[The Rustdoc Book]: ../rustdoc/the-doc-attribute.html
[procedural macros]: procedural-macros.html
[procedural macros]: procedural-macros.html
14 changes: 12 additions & 2 deletions src/type-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,23 @@ For all other enumerations, the layout is unspecified.

Likewise, combining two primitive representations together is unspecified.

### The `packed` Representation
### `align(x)` Representation

The `align(x)` representation can be used on `struct`s and `union`s to raise the
alignment of the type to the given value. The alignment value must be a power of
two of type `u32`. The `align` representation cannot lower the alignment of a
type.

### `packed` Representation

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

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

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

> Warning: Dereferencing an unaligned pointer is [undefined behaviour] and it is
> possible to [safely create unaligned pointers to `packed` fields][27060].
> Like all ways to create undefined behavior in safe Rust, this is a bug.
Expand All @@ -284,4 +294,4 @@ padding bytes and forcing the alignment of the type to `1`.
[C-like enumerations]: items/enumerations.html#c-like-enumerations
[zero-variant enumerations]: items/enumerations.html#zero-variant-enumerations
[undefined behavior]: behavior-considered-undefined.html
[27060]: https://github.com/rust-lang/rust/issues/27060
[27060]: https://github.com/rust-lang/rust/issues/27060

0 comments on commit 2866fda

Please sign in to comment.