From 8a7609509c5ccc125a935a31a308173fcdab1f01 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Fri, 21 Aug 2020 23:58:32 +0200 Subject: [PATCH 01/10] Document the enum changes in RFC 2195 --- src/type-layout.md | 176 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 155 insertions(+), 21 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 3adbdc95f..8005fb02f 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -174,7 +174,8 @@ layout such as reinterpreting values as a different type. Because of this dual purpose, it is possible to create types that are not useful for interfacing with the C programming language. -This representation can be applied to structs, unions, and enums. +This representation can be applied to structs, unions, and enums. The exception +is [zero-variant enums] for which the `C` representation is an error. #### \#[repr(C)] Structs @@ -273,9 +274,9 @@ assert_eq!(std::mem::size_of::(), 8); // Size of 6 from b, assert_eq!(std::mem::align_of::(), 4); // From a ``` -#### \#[repr(C)] Enums +#### \#[repr(C)] Field-less Enums -For [C-like enumerations], the `C` representation has the size and alignment of +For [field-less enums], the `C` representation has the size and alignment of the default `enum` size and alignment for the target platform's C ABI. > Note: The enum representation in C is implementation defined, so this is @@ -285,21 +286,94 @@ the default `enum` size and alignment for the target platform's C ABI.
Warning: There are crucial differences between an `enum` in the C language and -Rust's C-like enumerations with this representation. An `enum` in C is +Rust's [field-less enums] with this representation. An `enum` in C is mostly a `typedef` plus some named constants; in other words, an object of an `enum` type can hold any integer value. For example, this is often used for -bitflags in `C`. In contrast, Rust’s C-like enumerations can only legally hold -the discriminant values, everything else is undefined behaviour. Therefore, -using a C-like enumeration in FFI to model a C `enum` is often wrong. +bitflags in `C`. In contrast, Rust’s [field-less enums] can only legally hold +the discrimnant values, everything else is [undefined behavior]. Therefore, +using a field-less enum in FFI to model a C `enum` is often wrong.
-It is an error for [zero-variant enumerations] to have the `C` representation. +#### \#[repr(C)] Enums With Fields -For all other enumerations, the layout is unspecified. +The representation of a `repr(C)` enum with fields is a `repr(C)` struct with +two fields, also called a "tagged union" in C: -Likewise, combining the `C` representation with a primitive representation, the -layout is unspecified. +- a `repr(C)` version of the enum with all fields removed ("the tag") +- a `repr(C)` union of `repr(C)` structs for the fields of each variant that had + them ("the payload") + +> Note: due to the representation of `repr(C)` structs and unions, if a variant +> has a single field there is no difference between putting that field directly +> in the union or wrapping it in a struct; any system which wishes to manipulate +> such an `enum`'s representation may therefore use whichever form is more +> convenient/consistent for them + +```rust +// This Enum has the same layout as ... +#[repr(C)] +enum MyEnum { + A(u32), + B(f32, u64), + C { x: u32, y: u8 }, + D, + } + +// ... this struct. +#[repr(C)] +struct MyEnumRepr { + tag: MyEnumDiscriminant, + payload: MyEnumFields, +} + +// This is the discriminant enum. +#[repr(C)] +enum MyEnumDiscriminant { A, B, C, D } + +// This is the variant union. +#[repr(C)] +union MyEnumFields { + A: MyAFields, + B: MyBFields, + C: MyCFields, + D: MyDFields, +} + +#[repr(C)] +#[derive(Copy, Clone)] +struct MyAFields(u32); + +#[repr(C)] +#[derive(Copy, Clone)] +struct MyBFields(f32, u64); + +#[repr(C)] +#[derive(Copy, Clone)] +struct MyCFields { x: u32, y: u8 } + +// This struct could be omitted (it is a zero-sized type), and it must be in +// C/C++ headers. +#[repr(C)] +#[derive(Copy, Clone)] +struct MyDFields; +``` + +> Note: `union`s with non-`Copy` fields are unstable, see [55149]. + +Combining the `repr(C)` +and a primitive representation is only defined for enums with fields. The +primitive representation modifies the `repr(C)` by changing the representation +of the discriminant enum to have the representation of the chosen primitive +representation. So, if you chose the `u8` representation, then the discriminant +enum would have a size and alignment of 1 byte. + +> Note: This representation is primarily intended for Rust code that wants to +> interoperate with the idioms of preexisting C/C++ codebases. If you have +> control over both the Rust and C code, such as using C as FFI glue between +> Rust and some third language, then you should use a +> [primitive representation](#primitive-representation-of-enums-with-fields) +> instead. ### Primitive representations @@ -307,18 +381,76 @@ The *primitive representations* are the representations with the same names as the primitive integer types. That is: `u8`, `u16`, `u32`, `u64`, `u128`, `usize`, `i8`, `i16`, `i32`, `i64`, `i128`, and `isize`. -Primitive representations can only be applied to enumerations. +Primitive representations can only be applied to enumerations and have +different behavior whether the enum has fields or no fields. It is an error +for [zero-variant enumerations] to have a primitive representation. + +Combining two primitive representations together is unspecified. + +Combining the `C` representation and a primitive representation is described +[above](#c-primitive-representation). + +#### Primitive Representation of Field-less Enums + +For [field-less enums], they set the size and alignment to be the same as +the primitive type of the same name. For example, a field-less enum with +a `u8` representation can only have discriminants between 0 and 255 inclusive. + +#### Primitive Representation of Enums With Fields + +The representation of a `repr(int)` enum is a `repr(C)` union of `repr(C)` +structs for each variant with a field. The first field of each struct in the +union is a `repr(int)` version of the enum with all fields removed ("the tag") +and the remaining fields are the fields of that variant. + +> Note: this representation is unchanged if the tag is given its own member in +> the union, should that make manipulation more clear for you (although in C++, +> to follow The Exact Word Of The Standard the tag member should be wrapped in +> a `struct`). + +Because unions with non-`Copy` fields aren't allowed, this representation can +only be expressed in Rust if every field is also [`Copy`]. -For [C-like enumerations], they set the size and alignment to be the same as the -primitive type of the same name. For example, a C-like enumeration with a `u8` -representation can only have discriminants between 0 and 255 inclusive. +```rust +// This enum has the same layout as ... +#[repr(u8)] +enum MyEnum { + A(u32), + B(f32, u64), + C { x: u32, y: u8 }, + D, + } + +// ... this union. +#[repr(C)] +union MyEnumRepr { + A: MyVariantA, + B: MyVariantB, + C: MyVariantC, + D: MyVariantD, +} + +// This is the discriminant enum. +#[repr(u8)] +#[derive(Copy, Clone)] +enum MyEnumDiscriminant { A, B, C, D } + +#[repr(C)] +#[derive(Clone, Copy)] +struct MyVariantA(MyEnumDiscriminant, u32); -It is an error for [zero-variant enumerations] to have a primitive -representation. +#[repr(C)] +#[derive(Clone, Copy)] +struct MyVariantB(MyEnumDiscriminant, f32, u64); -For all other enumerations, the layout is unspecified. +#[repr(C)] +#[derive(Clone, Copy)] +struct MyVariantC { tag: MyEnumDiscriminant, x: u32, y: u8 } -Likewise, combining two primitive representations together is unspecified. +#[repr(C)] +#[derive(Clone, Copy)] +struct MyVariantD(MyEnumDiscriminant); +``` ### The alignment modifiers @@ -379,12 +511,14 @@ used with any other representation. [`align_of`]: ../std/mem/fn.align_of.html [`size_of`]: ../std/mem/fn.size_of.html [`Sized`]: ../std/marker/trait.Sized.html +[`Copy`]: ../std/marker/trait.Copy.html [dynamically sized types]: dynamically-sized-types.md -[C-like enumerations]: items/enumerations.md#custom-discriminant-values-for-fieldless-enumerations +[field-less enums]: items/enumerations.md#custom-discriminant-values-for-fieldless-enumerations [enumerations]: items/enumerations.md -[zero-variant enumerations]: items/enumerations.md#zero-variant-enums +[zero-variant enums]: items/enumerations.md#zero-variant-enums [undefined behavior]: behavior-considered-undefined.md [27060]: https://github.com/rust-lang/rust/issues/27060 +[55149]: https://github.com/rust-lang/rust/issues/55149 [`PhantomData`]: special-types-and-traits.md#phantomdatat [Default]: #the-default-representation [`C`]: #the-c-representation From fddf097be161d877291a527a0c74a44b45974b54 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Sat, 22 Aug 2020 19:54:17 +0200 Subject: [PATCH 02/10] Address review --- src/type-layout.md | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 8005fb02f..c2c1ad1ba 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -361,20 +361,6 @@ struct MyDFields; > Note: `union`s with non-`Copy` fields are unstable, see [55149]. -Combining the `repr(C)` -and a primitive representation is only defined for enums with fields. The -primitive representation modifies the `repr(C)` by changing the representation -of the discriminant enum to have the representation of the chosen primitive -representation. So, if you chose the `u8` representation, then the discriminant -enum would have a size and alignment of 1 byte. - -> Note: This representation is primarily intended for Rust code that wants to -> interoperate with the idioms of preexisting C/C++ codebases. If you have -> control over both the Rust and C code, such as using C as FFI glue between -> Rust and some third language, then you should use a -> [primitive representation](#primitive-representation-of-enums-with-fields) -> instead. - ### Primitive representations The *primitive representations* are the representations with the same names as @@ -383,12 +369,22 @@ the primitive integer types. That is: `u8`, `u16`, `u32`, `u64`, `u128`, Primitive representations can only be applied to enumerations and have different behavior whether the enum has fields or no fields. It is an error -for [zero-variant enumerations] to have a primitive representation. +for [zero-variant enumerations] to have a primitive representation. Combining +two primitive representations together is an error. -Combining two primitive representations together is unspecified. +Combining the `repr(C)` +and a primitive representation is only defined for enums with fields. The +primitive representation modifies the `repr(C)` by changing the representation +of the discriminant enum to have the representation of the chosen primitive +representation. So, if you chose the `u8` representation, then the discriminant +enum would have a size and alignment of 1 byte. -Combining the `C` representation and a primitive representation is described -[above](#c-primitive-representation). +> Note: Primitive representations are primarily intended for Rust code that +> wants to interoperate with the idioms of preexisting C/C++ codebases. If you +> have control over both the Rust and C code, such as using C as FFI glue +> between Rust and some third language, then you should use a +> [primitive representation](#primitive-representation-of-enums-with-fields) +> instead. #### Primitive Representation of Field-less Enums @@ -408,9 +404,6 @@ and the remaining fields are the fields of that variant. > to follow The Exact Word Of The Standard the tag member should be wrapped in > a `struct`). -Because unions with non-`Copy` fields aren't allowed, this representation can -only be expressed in Rust if every field is also [`Copy`]. - ```rust // This enum has the same layout as ... #[repr(u8)] @@ -452,6 +445,8 @@ struct MyVariantC { tag: MyEnumDiscriminant, x: u32, y: u8 } struct MyVariantD(MyEnumDiscriminant); ``` +> Note: `union`s with non-`Copy` fields are unstable, see [55149]. + ### The alignment modifiers The `align` and `packed` modifiers can be used to respectively raise or lower From 5a46f81223625ae1c058ab8cfa3879ae684465e7 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Mon, 24 Aug 2020 22:33:50 +0200 Subject: [PATCH 03/10] Fix nits from review, move a paragraph to a better place --- src/type-layout.md | 55 +++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index c2c1ad1ba..bec4fa456 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -304,11 +304,11 @@ two fields, also called a "tagged union" in C: - a `repr(C)` union of `repr(C)` structs for the fields of each variant that had them ("the payload") -> Note: due to the representation of `repr(C)` structs and unions, if a variant +> Note: Due to the representation of `repr(C)` structs and unions, if a variant > has a single field there is no difference between putting that field directly > in the union or wrapping it in a struct; any system which wishes to manipulate > such an `enum`'s representation may therefore use whichever form is more -> convenient/consistent for them +> convenient or consistent for them ```rust // This Enum has the same layout as ... @@ -334,10 +334,10 @@ enum MyEnumDiscriminant { A, B, C, D } // This is the variant union. #[repr(C)] union MyEnumFields { - A: MyAFields, - B: MyBFields, - C: MyCFields, - D: MyDFields, + A: MyAFields, + B: MyBFields, + C: MyCFields, + D: MyDFields, } #[repr(C)] @@ -372,13 +372,6 @@ different behavior whether the enum has fields or no fields. It is an error for [zero-variant enumerations] to have a primitive representation. Combining two primitive representations together is an error. -Combining the `repr(C)` -and a primitive representation is only defined for enums with fields. The -primitive representation modifies the `repr(C)` by changing the representation -of the discriminant enum to have the representation of the chosen primitive -representation. So, if you chose the `u8` representation, then the discriminant -enum would have a size and alignment of 1 byte. - > Note: Primitive representations are primarily intended for Rust code that > wants to interoperate with the idioms of preexisting C/C++ codebases. If you > have control over both the Rust and C code, such as using C as FFI glue @@ -388,21 +381,21 @@ enum would have a size and alignment of 1 byte. #### Primitive Representation of Field-less Enums -For [field-less enums], they set the size and alignment to be the same as -the primitive type of the same name. For example, a field-less enum with -a `u8` representation can only have discriminants between 0 and 255 inclusive. +For [field-less enums], primitive representations set the size and alignment to +be the same as the primitive type of the same name. For example, a field-less +enum with a `u8` representation can only have discriminants between 0 and 255 +inclusive. #### Primitive Representation of Enums With Fields -The representation of a `repr(int)` enum is a `repr(C)` union of `repr(C)` -structs for each variant with a field. The first field of each struct in the -union is a `repr(int)` version of the enum with all fields removed ("the tag") -and the remaining fields are the fields of that variant. +The representation of a primitive representation enum is a `repr(C)` union of +`repr(C)` structs for each variant with a field. The first field of each struct +in the union is the primitive representation version of the enum with all fields +removed ("the tag") and the remaining fields are the fields of that variant. -> Note: this representation is unchanged if the tag is given its own member in -> the union, should that make manipulation more clear for you (although in C++, -> to follow The Exact Word Of The Standard the tag member should be wrapped in -> a `struct`). +> Note: 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`). ```rust // This enum has the same layout as ... @@ -417,10 +410,10 @@ enum MyEnum { // ... this union. #[repr(C)] union MyEnumRepr { - A: MyVariantA, - B: MyVariantB, - C: MyVariantC, - D: MyVariantD, + A: MyVariantA, + B: MyVariantB, + C: MyVariantC, + D: MyVariantD, } // This is the discriminant enum. @@ -447,6 +440,12 @@ struct MyVariantD(MyEnumDiscriminant); > Note: `union`s with non-`Copy` fields are unstable, see [55149]. +Combining the `repr(C)` and a primitive representation is only defined for enums +with fields. The primitive representation modifies the `repr(C)` by changing the +representation of the discriminant enum to have the representation of the chosen +primitive representation. So, if you chose the `u8` representation, then the +discriminant enum would have a size and alignment of 1 byte. + ### The alignment modifiers The `align` and `packed` modifiers can be used to respectively raise or lower From ddb2fe9c6812f0cb14868629fe8df669ce0c6ce1 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Tue, 25 Aug 2020 21:56:06 +0200 Subject: [PATCH 04/10] Add a link and reword the start of a paragraph --- src/type-layout.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index bec4fa456..c234daa36 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -440,12 +440,14 @@ struct MyVariantD(MyEnumDiscriminant); > Note: `union`s with non-`Copy` fields are unstable, see [55149]. -Combining the `repr(C)` and a primitive representation is only defined for enums -with fields. The primitive representation modifies the `repr(C)` by changing the +For enums with fields, it is also possible to combining `repr(C)` and a +primitive representation. This modifies the [`repr(C)`] by changing the representation of the discriminant enum to have the representation of the chosen primitive representation. So, if you chose the `u8` representation, then the discriminant enum would have a size and alignment of 1 byte. +[`repr(C)`]: #reprc-enums-with-fields + ### The alignment modifiers The `align` and `packed` modifiers can be used to respectively raise or lower From 75708a27699ee6d949cd662228b9653ea25a639d Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Thu, 27 Aug 2020 00:10:20 +0200 Subject: [PATCH 05/10] Address concerns from review and add a subsection about combining primitive repr and repr(C) --- src/type-layout.md | 52 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index c234daa36..64c6ce240 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -440,12 +440,56 @@ struct MyVariantD(MyEnumDiscriminant); > Note: `union`s with non-`Copy` fields are unstable, see [55149]. -For enums with fields, it is also possible to combining `repr(C)` and a -primitive representation. This modifies the [`repr(C)`] by changing the -representation of the discriminant enum to have the representation of the chosen -primitive representation. So, if you chose the `u8` representation, then the +#### Combining primitive representations of enums with fields and \#[repr(C)] + +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 have the representation +of the chosen primitive . So, if you chose the `u8` representation, then the discriminant enum would have a size and alignment of 1 byte. +The discriminant enum from the example [earlier][`repr(C)`] then becomes: + +```rust +#[repr(u8)] // Instead of `#[repr(C)]` +enum MyEnumDiscriminant { A, B, C, D } +``` + +For example, with a `repr(C, u8)` enum it is not possible to have 257 unique +discriminants ("tags") whereas the same enum with only a `repr(C)` attribute +will compile without any problems. + +Using a primitive representation in addition to `repr(C)` can change the size of +an enum from the `repr(C)` form: + +```rust +#[repr(C)] +enum EnumC { + Variant0(u8), + Variant1, +} + +#[repr(C, u8)] +enum Enum8 { + Variant0(u8), + Variant1, +} + +#[repr(C, u16)] +enum Enum16 { + Variant0(u8), + Variant1, +} + +// One byte for the discriminant and one byte for the value in Enum8::Variant0 +assert_eq!(std::mem::size_of::(), 8); +// One byte for the discriminant and one byte for the value in Enum8::Variant0 +assert_eq!(std::mem::size_of::(), 2); +// Two bytes for the discriminant and one byte for the value in Enum16::Variant0 +// plus one byte of padding. +assert_eq!(std::mem::size_of::(), 4); +``` + [`repr(C)`]: #reprc-enums-with-fields ### The alignment modifiers From 4d133a0756618bc1b3ba55ce7fdbf3b27e0b5943 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Thu, 27 Aug 2020 23:38:27 +0200 Subject: [PATCH 06/10] Improve the section Combining primitive representations of enums with fields and #[repr(C)] --- src/type-layout.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 64c6ce240..13877241c 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -444,15 +444,27 @@ struct MyVariantD(MyEnumDiscriminant); 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 have the representation -of the chosen primitive . So, if you chose the `u8` representation, then the -discriminant enum would have a size and alignment of 1 byte. +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. The discriminant enum from the example [earlier][`repr(C)`] then becomes: ```rust -#[repr(u8)] // Instead of `#[repr(C)]` +#[repr(C, u8)] // `u8` was added +enum MyEnum { + A(u32), + B(f32, u64), + C { x: u32, y: u8 }, + D, + } + +// ... + +#[repr(u8)] // So `u8` is used here instead of `C` enum MyEnumDiscriminant { A, B, C, D } + +// ... ``` For example, with a `repr(C, u8)` enum it is not possible to have 257 unique From 6808c5eb5f2238c82fd5459e5635049bb2a695c2 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Mon, 31 Aug 2020 21:53:38 +0200 Subject: [PATCH 07/10] Fix nits --- 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 13877241c..4b954f840 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -308,7 +308,7 @@ two fields, also called a "tagged union" in C: > has a single field there is no difference between putting that field directly > in the union or wrapping it in a struct; any system which wishes to manipulate > such an `enum`'s representation may therefore use whichever form is more -> convenient or consistent for them +> convenient or consistent for them. ```rust // This Enum has the same layout as ... @@ -493,7 +493,7 @@ enum Enum16 { Variant1, } -// One byte for the discriminant and one byte for the value in Enum8::Variant0 +// The C representation, with the size of the discriminant unspecified assert_eq!(std::mem::size_of::(), 8); // One byte for the discriminant and one byte for the value in Enum8::Variant0 assert_eq!(std::mem::size_of::(), 2); From 73eae8e3112701fc93b361eb157cb84ce438d3e5 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Tue, 1 Sep 2020 18:33:42 +0200 Subject: [PATCH 08/10] Fix nits 2 --- src/type-layout.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 4b954f840..1234754c6 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -373,11 +373,7 @@ for [zero-variant enumerations] to have a primitive representation. Combining two primitive representations together is an error. > Note: Primitive representations are primarily intended for Rust code that -> wants to interoperate with the idioms of preexisting C/C++ codebases. If you -> have control over both the Rust and C code, such as using C as FFI glue -> between Rust and some third language, then you should use a -> [primitive representation](#primitive-representation-of-enums-with-fields) -> instead. +> wants to interoperate with the idioms of preexisting C/C++ codebases. #### Primitive Representation of Field-less Enums @@ -493,7 +489,7 @@ enum Enum16 { Variant1, } -// The C representation, with the size of the discriminant unspecified +// The size of the C representation is platform dependant assert_eq!(std::mem::size_of::(), 8); // One byte for the discriminant and one byte for the value in Enum8::Variant0 assert_eq!(std::mem::size_of::(), 2); From 6e8d93a47693c19dea35cde12174fc07b4561fe5 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Tue, 1 Sep 2020 23:15:55 +0200 Subject: [PATCH 09/10] Remove the note altogether --- src/type-layout.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 1234754c6..5a2f536a2 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -372,9 +372,6 @@ different behavior whether the enum has fields or no fields. It is an error for [zero-variant enumerations] to have a primitive representation. Combining two primitive representations together is an error. -> Note: Primitive representations are primarily intended for Rust code that -> wants to interoperate with the idioms of preexisting C/C++ codebases. - #### Primitive Representation of Field-less Enums For [field-less enums], primitive representations set the size and alignment to From eb4ec258035b148fd9ace2df373dd2086b8e0278 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Wed, 2 Sep 2020 23:04:56 +0200 Subject: [PATCH 10/10] Use 'representation' instead of 'layout' in the examples --- 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 5a2f536a2..c450195b4 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -311,7 +311,7 @@ two fields, also called a "tagged union" in C: > convenient or consistent for them. ```rust -// This Enum has the same layout as ... +// This Enum has the same representation as ... #[repr(C)] enum MyEnum { A(u32), @@ -391,7 +391,7 @@ removed ("the tag") and the remaining fields are the fields of that variant. > follow the C++ standard the tag member should be wrapped in a `struct`). ```rust -// This enum has the same layout as ... +// This enum has the same representation as ... #[repr(u8)] enum MyEnum { A(u32),