• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# `transparent_unions`
2
3The tracking issue for this feature is [#60405]
4
5[#60405]: https://github.com/rust-lang/rust/issues/60405
6
7----
8
9The `transparent_unions` feature allows you mark `union`s as
10`#[repr(transparent)]`. A `union` may be `#[repr(transparent)]` in exactly the
11same conditions in which a `struct` may be `#[repr(transparent)]` (generally,
12this means the `union` must have exactly one non-zero-sized field). Some
13concrete illustrations follow.
14
15```rust
16#![feature(transparent_unions)]
17
18// This union has the same representation as `f32`.
19#[repr(transparent)]
20union SingleFieldUnion {
21    field: f32,
22}
23
24// This union has the same representation as `usize`.
25#[repr(transparent)]
26union MultiFieldUnion {
27    field: usize,
28    nothing: (),
29}
30```
31
32For consistency with transparent `struct`s, `union`s must have exactly one
33non-zero-sized field. If all fields are zero-sized, the `union` must not be
34`#[repr(transparent)]`:
35
36```rust
37#![feature(transparent_unions)]
38
39// This (non-transparent) union is already valid in stable Rust:
40pub union GoodUnion {
41    pub nothing: (),
42}
43
44// Error: transparent union needs exactly one non-zero-sized field, but has 0
45// #[repr(transparent)]
46// pub union BadUnion {
47//     pub nothing: (),
48// }
49```
50
51The one exception is if the `union` is generic over `T` and has a field of type
52`T`, it may be `#[repr(transparent)]` even if `T` is a zero-sized type:
53
54```rust
55#![feature(transparent_unions)]
56
57// This union has the same representation as `T`.
58#[repr(transparent)]
59pub union GenericUnion<T: Copy> { // Unions with non-`Copy` fields are unstable.
60    pub field: T,
61    pub nothing: (),
62}
63
64// This is okay even though `()` is a zero-sized type.
65pub const THIS_IS_OKAY: GenericUnion<()> = GenericUnion { field: () };
66```
67
68Like transparent `struct`s, a transparent `union` of type `U` has the same
69layout, size, and ABI as its single non-ZST field. If it is generic over a type
70`T`, and all its fields are ZSTs except for exactly one field of type `T`, then
71it has the same layout and ABI as `T` (even if `T` is a ZST when monomorphized).
72
73Like transparent `struct`s, transparent `union`s are FFI-safe if and only if
74their underlying representation type is also FFI-safe.
75
76A `union` may not be eligible for the same nonnull-style optimizations that a
77`struct` or `enum` (with the same fields) are eligible for. Adding
78`#[repr(transparent)]` to  `union` does not change this. To give a more concrete
79example, it is unspecified whether `size_of::<T>()` is equal to
80`size_of::<Option<T>>()`, where `T` is a `union` (regardless of whether or not
81it is transparent). The Rust compiler is free to perform this optimization if
82possible, but is not required to, and different compiler versions may differ in
83their application of these optimizations.
84