]> git.lizzy.rs Git - rust.git/blob - src/doc/unstable-book/src/language-features/transparent-unions.md
Update const_forget.rs
[rust.git] / src / doc / unstable-book / src / language-features / transparent-unions.md
1 # `transparent_unions`
2
3 The tracking issue for this feature is [#60405]
4
5 [60405]: https://github.com/rust-lang/rust/issues/60405
6
7 ----
8
9 The `transparent_unions` feature allows you mark `union`s as
10 `#[repr(transparent)]`. A `union` may be `#[repr(transparent)]` in exactly the
11 same conditions in which a `struct` may be `#[repr(transparent)]` (generally,
12 this means the `union` must have exactly one non-zero-sized field). Some
13 concrete illustrations follow.
14
15 ```rust
16 #![feature(transparent_unions)]
17
18 // This union has the same representation as `f32`.
19 #[repr(transparent)]
20 union SingleFieldUnion {
21     field: f32,
22 }
23
24 // This union has the same representation as `usize`.
25 #[repr(transparent)]
26 union MultiFieldUnion {
27     field: usize,
28     nothing: (),
29 }
30 ```
31
32 For consistency with transparent `struct`s, `union`s must have exactly one
33 non-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:
40 pub 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
51 The 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)]
59 pub 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.
65 pub const THIS_IS_OKAY: GenericUnion<()> = GenericUnion { field: () };
66 ```
67
68 Like transarent `struct`s, a transparent `union` of type `U` has the same
69 layout, 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
71 it has the same layout and ABI as `T` (even if `T` is a ZST when monomorphized).
72
73 Like transparent `struct`s, transparent `union`s are FFI-safe if and only if
74 their underlying representation type is also FFI-safe.
75
76 A `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
79 example, 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
81 it is transparent). The Rust compiler is free to perform this optimization if
82 possible, but is not required to, and different compiler versions may differ in
83 their application of these optimizations.