@@ -119,31 +119,34 @@ assert_eq!(16, size_of::<MyReprOption<&u16>>());
119
119
120
120
This optimization still applies to fieldless enums with an explicit ` repr(u*) ` , ` repr(i*) ` , or ` repr(C) ` .
121
121
122
- ## repr(packed)
122
+ ## repr(packed), repr(packed(n))
123
123
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.
127
129
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.
134
136
135
137
[ As this can cause undefined behavior] [ ub loads ] , the lint has been implemented
136
138
and it will become a hard error.
137
139
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.
140
142
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) ` .
142
145
143
146
## repr(align(n))
144
147
145
148
` 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 ` .
147
150
148
151
This enables several tricks, like making sure neighboring elements of an array
149
152
never share the same cache line with each other (which may speed up certain
0 commit comments