Skip to content

Commit

Permalink
Clarify niche optimization on enums with reprs (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshtriplett authored Oct 6, 2021
1 parent e0aa678 commit 7cf00d4
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/other-reprs.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ manipulate its tag and fields. See [the RFC][really-tagged] for details.

These `repr`s have no effect on a struct.

Adding an explicit `repr(u*)`, `repr(i*)`, or `repr(C)` to an enum suppresses the null-pointer optimization, like:
Adding an explicit `repr(u*)`, `repr(i*)`, or `repr(C)` to an enum with fields suppresses the null-pointer optimization, like:

```rust
# use std::mem::size_of;
Expand All @@ -110,6 +110,8 @@ assert_eq!(8, size_of::<MyOption<&u16>>());
assert_eq!(16, size_of::<MyReprOption<&u16>>());
```

This optimization still applies to fieldless enums with an explicit `repr(u*)`, `repr(i*)`, or `repr(C)`.

## repr(packed)

`repr(packed)` forces Rust to strip any padding, and only align the type to a
Expand Down

0 comments on commit 7cf00d4

Please sign in to comment.