File tree 1 file changed +11
-4
lines changed
1 file changed +11
-4
lines changed Original file line number Diff line number Diff line change @@ -197,6 +197,17 @@ Here is this algorithm described in pseudocode.
197
197
198
198
<!-- ignore: pseudocode -->
199
199
``` rust,ignore
200
+ fn padding_needed_for(offset: usize, alignment: usize) -> usize {
201
+ let misalignment = offset % alignment;
202
+ if misalignment > 0 {
203
+ // round up to next multiple of `alignment`
204
+ alignment - misalignment
205
+ } else {
206
+ // already a multiple of `alignment`
207
+ 0
208
+ }
209
+ }
210
+
200
211
struct.alignment = struct.fields().map(|field| field.alignment).max();
201
212
202
213
let current_offset = 0;
@@ -205,10 +216,6 @@ for field in struct.fields_in_declaration_order() {
205
216
// Increase the current offset so that it's a multiple of the alignment
206
217
// of this field. For the first field, this will always be zero.
207
218
// The skipped bytes are called padding bytes.
208
- //
209
- // padding_needed_for() is equivalent to
210
- // std::alloc::Layout::padding_needed_for(), but takes an integer rather
211
- // than a Layout as the first argument.
212
219
current_offset += padding_needed_for(current_offset, field.alignment);
213
220
214
221
struct[field].offset = current_offset;
You can’t perform that action at this time.
0 commit comments