Skip to content

Commit 3c5add6

Browse files
authored
Merge pull request #2227 from RalfJung/enums-with-fields
enums with fields: clarify that repr(C, int) is quite different from repr(int)
2 parents 2ed1cb8 + 7b708f0 commit 3c5add6

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

src/type-layout.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@ The representation of a primitive representation enum is a `repr(C)` union of `r
455455
> [!NOTE]
456456
> This representation is unchanged if the tag is given its own member in the union, should that make manipulation more clear for you (although to follow the C++ standard the tag member should be wrapped in a `struct`).
457457
458+
> [!NOTE]
459+
> This representation is quite different from `repr(C)` for enums with fields.
460+
458461
```rust
459462
// This enum has the same representation as ...
460463
#[repr(u8)]
@@ -501,6 +504,10 @@ r[layout.repr.primitive-c]
501504

502505
For enums with fields, it is also possible to combine `repr(C)` and a primitive representation (e.g., `repr(C, u8)`). This modifies the [`repr(C)`] by changing the representation of the discriminant enum to the chosen primitive instead. So, if you chose the `u8` representation, then the discriminant enum would have a size and alignment of 1 byte.
503506

507+
> [!NOTE]
508+
> This means `repr(C, u8)` is quite different from `repr(u8)`!
509+
> The former is a struct with two fields (tag and a union of variants), the latter is a union where each field starts with the tag.
510+
504511
The discriminant enum from the example [earlier][`repr(C)`] then becomes:
505512

506513
```rust

0 commit comments

Comments
 (0)