File tree Expand file tree Collapse file tree 1 file changed +11
-4
lines changed Expand file tree Collapse file tree 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.
197197
198198<!-- ignore: pseudocode -->
199199``` 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+
200211struct.alignment = struct.fields().map(|field| field.alignment).max();
201212
202213let current_offset = 0;
@@ -205,10 +216,6 @@ for field in struct.fields_in_declaration_order() {
205216 // Increase the current offset so that it's a multiple of the alignment
206217 // of this field. For the first field, this will always be zero.
207218 // 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.
212219 current_offset += padding_needed_for(current_offset, field.alignment);
213220
214221 struct[field].offset = current_offset;
You can’t perform that action at this time.
0 commit comments