Skip to content

Commit 5426423

Browse files
committed
Fix incorrect pseudocode for #[repr(C)] struct alignment
1 parent 48a079f commit 5426423

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/type-layout.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,18 @@ for field in struct.fields_in_declaration_order() {
205205
// Increase the current offset so that it's a multiple of the alignment
206206
// of this field. For the first field, this will always be zero.
207207
// The skipped bytes are called padding bytes.
208-
current_offset += field.alignment % current_offset;
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+
current_offset += padding_needed_for(current_offset, field.alignment);
209213
210214
struct[field].offset = current_offset;
211215
212216
current_offset += field.size;
213217
}
214218
215-
struct.size = current_offset + current_offset % struct.alignment;
219+
struct.size = current_offset + padding_needed_for(current_offset, struct.alignment);
216220
```
217221

218222
> Note: This algorithm can produce zero-sized structs. This differs from

0 commit comments

Comments
 (0)