Skip to content

Commit 1f3f7c4

Browse files
authored
Merge pull request #460 from luqmana/repr-packed-n
other-reprs: Add details for n!=1 repr(packed)
2 parents 75eb897 + 6120f45 commit 1f3f7c4

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/other-reprs.md

+17-14
Original file line numberDiff line numberDiff line change
@@ -119,31 +119,34 @@ assert_eq!(16, size_of::<MyReprOption<&u16>>());
119119

120120
This optimization still applies to fieldless enums with an explicit `repr(u*)`, `repr(i*)`, or `repr(C)`.
121121

122-
## repr(packed)
122+
## repr(packed), repr(packed(n))
123123

124-
`repr(packed)` forces Rust to strip any padding, and only align the type to a
125-
byte. This may improve the memory footprint, but will likely have other negative
126-
side-effects.
124+
`repr(packed(n))` (where `n` is a power of two) forces the type to have an
125+
alignment of *at most* `n`. Most commonly used without an explicit `n`,
126+
`repr(packed)` is equivalent to `repr(packed(1))` which forces Rust to strip
127+
any padding, and only align the type to a byte. This may improve the memory
128+
footprint, but will likely have other negative side-effects.
127129

128-
In particular, most architectures *strongly* prefer values to be aligned. This
129-
may mean the unaligned loads are penalized (x86), or even fault (some ARM
130-
chips). For simple cases like directly loading or storing a packed field, the
131-
compiler might be able to paper over alignment issues with shifts and masks.
132-
However if you take a reference to a packed field, it's unlikely that the
133-
compiler will be able to emit code to avoid an unaligned load.
130+
In particular, most architectures *strongly* prefer values to be naturally
131+
aligned. This may mean that unaligned loads are penalized (x86), or even fault
132+
(some ARM chips). For simple cases like directly loading or storing a packed
133+
field, the compiler might be able to paper over alignment issues with shifts
134+
and masks. However if you take a reference to a packed field, it's unlikely
135+
that the compiler will be able to emit code to avoid an unaligned load.
134136

135137
[As this can cause undefined behavior][ub loads], the lint has been implemented
136138
and it will become a hard error.
137139

138-
`repr(packed)` is not to be used lightly. Unless you have extreme requirements,
139-
this should not be used.
140+
`repr(packed)/repr(packed(n))` is not to be used lightly. Unless you have
141+
extreme requirements, this should not be used.
140142

141-
This repr is a modifier on `repr(C)` and `repr(Rust)`.
143+
This repr is a modifier on `repr(C)` and `repr(Rust)`. For FFI compatibilty
144+
you most likely always want to be explicit: `repr(C, packed)`.
142145

143146
## repr(align(n))
144147

145148
`repr(align(n))` (where `n` is a power of two) forces the type to have an
146-
alignment of *at least* n.
149+
alignment of *at least* `n`.
147150

148151
This enables several tricks, like making sure neighboring elements of an array
149152
never share the same cache line with each other (which may speed up certain

0 commit comments

Comments
 (0)