Skip to content

Commit a8d29ad

Browse files
committed
Clarify layout of enums with a single variant
1 parent ec2e0d0 commit a8d29ad

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

Diff for: reference/src/layout/enums.md

+9-28
Original file line numberDiff line numberDiff line change
@@ -378,47 +378,28 @@ enum Enum2<T> {
378378

379379
[niche]: ../glossary.html#niche
380380

381-
### Layout of single variant enums
381+
### Layout of enums with a single variant
382382

383383
> **NOTE**: the guarantees in this section have not been approved by an RFC process.
384384

385-
**Single variant data-carrying*** enums without a `repr()` annotation have the
386-
same layout as the variant field. **Single variant fieldless** enums have the
387-
same layout as a unit struct.
385+
**Data-carrying** enums with a single variant without a `repr()` annotation have
386+
the same layout as the variant field. **Fieldless** enums with a single variant
387+
have the same layout as a unit struct.
388388

389-
Here:
389+
For example, here:
390390

391391
```rust
392-
# use std::mem::{size_of, align_of};
393392
struct UnitStruct;
394-
enum SingleVariantFieldless { FieldlessVariant }
395-
# fn main() {
396-
assert_eq!(size_of::<SingleVariantFieldless>(), size_of::<UnitStruct>());
397-
assert_eq!(align_of::<SingleVariantFieldless>(), align_of::<UnitStruct>());
398-
// assert_eq!(call_abi_of::<SingleVariantFieldless>(), call_abi_of::<UnitStruct>());
399-
// assert_eq!(niches_of::<SingleVariantFieldless>(), niches_of::<UnitStruct>());
400-
# }
401-
```
402-
403-
the single-variant fieldless enum `SingleVariantFieldless` has the same layout
404-
as `UnitStruct`.
393+
enum FieldlessSingleVariant { FieldlessVariant }
405394

406-
Here:
407-
408-
```rust
409-
# use std::mem::{size_of, align_of};
410395
struct SomeStruct { x: u32 }
411-
enum SingleVariantDataCarrying {
396+
enum DataCarryingSingleVariant {
412397
DataCarryingVariant(SomeStruct),
413398
}
414-
# fn main() {
415-
# assert_eq!(size_of::<SingleVariantDataCarrying>(), size_of::<SomeStruct>());
416-
# assert_eq!(align_of::<SingleVariantDataCarrying>(), align_of::<SomeStruct>());
417-
# }
418399
```
419400

420-
the single-variant data-carrying enum `SingleVariantDataCarrying` has the same
421-
layout as `SomeStruct`.
401+
* `FieldSingleVariant` has the same layout as `UnitStruct`,
402+
* `DataCarryingSingleVariant` has the same layout as `SomeStruct`.
422403

423404
### Layout of multi-variant enums with one inhabited variant
424405

0 commit comments

Comments
 (0)