-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #60463 - mjbshaw:transparent, r=varkor,rkruppe
Implement RFC 2645 (transparent enums and unions) Tracking issue: #60405
- Loading branch information
Showing
34 changed files
with
730 additions
and
208 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
src/doc/unstable-book/src/language-features/transparent-enums.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# `transparent_enums` | ||
|
||
The tracking issue for this feature is [#60405] | ||
|
||
[60405]: https://github.com/rust-lang/rust/issues/60405 | ||
|
||
---- | ||
|
||
The `transparent_enums` feature allows you mark `enum`s as | ||
`#[repr(transparent)]`. An `enum` may be `#[repr(transparent)]` if it has | ||
exactly one variant, and that variant matches the same conditions which `struct` | ||
requires for transparency. Some concrete illustrations follow. | ||
|
||
```rust | ||
#![feature(transparent_enums)] | ||
|
||
// This enum has the same representation as `f32`. | ||
#[repr(transparent)] | ||
enum SingleFieldEnum { | ||
Variant(f32) | ||
} | ||
|
||
// This enum has the same representation as `usize`. | ||
#[repr(transparent)] | ||
enum MultiFieldEnum { | ||
Variant { field: usize, nothing: () }, | ||
} | ||
``` | ||
|
||
For consistency with transparent `struct`s, `enum`s must have exactly one | ||
non-zero-sized field. If all fields are zero-sized, the `enum` must not be | ||
`#[repr(transparent)]`: | ||
|
||
```rust | ||
#![feature(transparent_enums)] | ||
|
||
// This (non-transparent) enum is already valid in stable Rust: | ||
pub enum GoodEnum { | ||
Nothing, | ||
} | ||
|
||
// Error: transparent enum needs exactly one non-zero-sized field, but has 0 | ||
// #[repr(transparent)] | ||
// pub enum BadEnum { | ||
// Nothing(()), | ||
// } | ||
|
||
// Error: transparent enum needs exactly one non-zero-sized field, but has 0 | ||
// #[repr(transparent)] | ||
// pub enum BadEmptyEnum { | ||
// Nothing, | ||
// } | ||
``` | ||
|
||
The one exception is if the `enum` is generic over `T` and has a field of type | ||
`T`, it may be `#[repr(transparent)]` even if `T` is a zero-sized type: | ||
|
||
```rust | ||
#![feature(transparent_enums)] | ||
|
||
// This enum has the same representation as `T`. | ||
#[repr(transparent)] | ||
pub enum GenericEnum<T> { | ||
Variant(T, ()), | ||
} | ||
|
||
// This is okay even though `()` is a zero-sized type. | ||
pub const THIS_IS_OKAY: GenericEnum<()> = GenericEnum::Variant((), ()); | ||
``` | ||
|
||
Transparent `enum`s require exactly one variant: | ||
|
||
```rust | ||
// Error: transparent enum needs exactly one variant, but has 0 | ||
// #[repr(transparent)] | ||
// pub enum TooFewVariants { | ||
// } | ||
|
||
// Error: transparent enum needs exactly one variant, but has 2 | ||
// #[repr(transparent)] | ||
// pub enum TooManyVariants { | ||
// First(usize), | ||
// Second, | ||
// } | ||
``` | ||
|
||
Like transarent `struct`s, a transparent `enum` of type `E` has the same layout, | ||
size, and ABI as its single non-ZST field. If it is generic over a type `T`, and | ||
all its fields are ZSTs except for exactly one field of type `T`, then it has | ||
the same layout and ABI as `T` (even if `T` is a ZST when monomorphized). | ||
|
||
Like transparent `struct`s, transparent `enum`s are FFI-safe if and only if | ||
their underlying representation type is also FFI-safe. |
83 changes: 83 additions & 0 deletions
83
src/doc/unstable-book/src/language-features/transparent-unions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# `transparent_unions` | ||
|
||
The tracking issue for this feature is [#60405] | ||
|
||
[60405]: https://github.com/rust-lang/rust/issues/60405 | ||
|
||
---- | ||
|
||
The `transparent_unions` feature allows you mark `union`s as | ||
`#[repr(transparent)]`. A `union` may be `#[repr(transparent)]` in exactly the | ||
same conditions in which a `struct` may be `#[repr(transparent)]` (generally, | ||
this means the `union` must have exactly one non-zero-sized field). Some | ||
concrete illustrations follow. | ||
|
||
```rust | ||
#![feature(transparent_unions)] | ||
|
||
// This union has the same representation as `f32`. | ||
#[repr(transparent)] | ||
union SingleFieldUnion { | ||
field: f32, | ||
} | ||
|
||
// This union has the same representation as `usize`. | ||
#[repr(transparent)] | ||
union MultiFieldUnion { | ||
field: usize, | ||
nothing: (), | ||
} | ||
``` | ||
|
||
For consistency with transparent `struct`s, `union`s must have exactly one | ||
non-zero-sized field. If all fields are zero-sized, the `union` must not be | ||
`#[repr(transparent)]`: | ||
|
||
```rust | ||
#![feature(transparent_unions)] | ||
|
||
// This (non-transparent) union is already valid in stable Rust: | ||
pub union GoodUnion { | ||
pub nothing: (), | ||
} | ||
|
||
// Error: transparent union needs exactly one non-zero-sized field, but has 0 | ||
// #[repr(transparent)] | ||
// pub union BadUnion { | ||
// pub nothing: (), | ||
// } | ||
``` | ||
|
||
The one exception is if the `union` is generic over `T` and has a field of type | ||
`T`, it may be `#[repr(transparent)]` even if `T` is a zero-sized type: | ||
|
||
```rust | ||
#![feature(transparent_unions)] | ||
|
||
// This union has the same representation as `T`. | ||
#[repr(transparent)] | ||
pub union GenericUnion<T: Copy> { // Unions with non-`Copy` fields are unstable. | ||
pub field: T, | ||
pub nothing: (), | ||
} | ||
|
||
// This is okay even though `()` is a zero-sized type. | ||
pub const THIS_IS_OKAY: GenericUnion<()> = GenericUnion { field: () }; | ||
``` | ||
|
||
Like transarent `struct`s, a transparent `union` of type `U` has the same | ||
layout, size, and ABI as its single non-ZST field. If it is generic over a type | ||
`T`, and all its fields are ZSTs except for exactly one field of type `T`, then | ||
it has the same layout and ABI as `T` (even if `T` is a ZST when monomorphized). | ||
|
||
Like transparent `struct`s, transparent `union`s are FFI-safe if and only if | ||
their underlying representation type is also FFI-safe. | ||
|
||
A `union` may not be eligible for the same nonnull-style optimizations that a | ||
`struct` or `enum` (with the same fields) are eligible for. Adding | ||
`#[repr(transparent)]` to `union` does not change this. To give a more concrete | ||
example, it is unspecified whether `size_of::<T>()` is equal to | ||
`size_of::<Option<T>>()`, where `T` is a `union` (regardless of whether or not | ||
it is transparent). The Rust compiler is free to perform this optimization if | ||
possible, but is not required to, and different compiler versions may differ in | ||
their application of these optimizations. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.