-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Implement RFC 2645 (transparent enums and unions) #60463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.