From 52a16e51d88087f606d2480f8d30d6ac3c4d87a1 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Mon, 27 Jan 2020 15:02:36 +0100 Subject: [PATCH 1/2] `enum`s can now be `#[repr(transparent)]` --- src/type-layout.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 7557d951c..035cc7eac 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -357,13 +357,14 @@ Like all ways to create undefined behavior in safe Rust, this is a bug. ### The `transparent` Representation -The `transparent` representation can only be used on `struct`s that have: +The `transparent` representation can only be used on a [`struct`][structs] that has, +or an [`enum`][enumerations] with a single variant which has: - a single field with non-zero size, and - any number of fields with size 0 and alignment 1 (e.g. [`PhantomData`]). -Structs with this representation have the same layout and ABI as the single -non-zero sized field. +Structs and enums with this representation have the same layout and ABI +as the single non-zero sized field. This is different than the `C` representation because a struct with the `C` representation will always have the ABI of a `C` `struct` @@ -380,6 +381,7 @@ used with any other representation. [`Sized`]: ../std/marker/trait.Sized.html [dynamically sized types]: dynamically-sized-types.md [C-like enumerations]: items/enumerations.md#custom-discriminant-values-for-fieldless-enumerations +[enumerations]: items/enumerations.md [zero-variant enumerations]: items/enumerations.md#zero-variant-enums [undefined behavior]: behavior-considered-undefined.md [27060]: https://github.com/rust-lang/rust/issues/27060 @@ -387,5 +389,6 @@ used with any other representation. [Default]: #the-default-representation [`C`]: #the-c-representation [primitive representations]: #primitive-representations +[structs]: items/structs.md [`transparent`]: #the-transparent-representation [`Layout`]: ../std/alloc/struct.Layout.html From 4515957cf2fc738c434c5880ec55897095fc5cae Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Wed, 8 Apr 2020 19:26:36 +0200 Subject: [PATCH 2/2] tweak wording for `repr(transparent)` enums --- src/type-layout.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 035cc7eac..9c3f1fd68 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -357,8 +357,8 @@ Like all ways to create undefined behavior in safe Rust, this is a bug. ### The `transparent` Representation -The `transparent` representation can only be used on a [`struct`][structs] that has, -or an [`enum`][enumerations] with a single variant which has: +The `transparent` representation can only be used on a [`struct`][structs] +or an [`enum`][enumerations] with a single variant that has: - a single field with non-zero size, and - any number of fields with size 0 and alignment 1 (e.g. [`PhantomData`]).