@@ -56,24 +56,27 @@ compiled as normal.)
56
56
57
57
## repr(transparent)
58
58
59
- This can only be used on structs with a single non-zero-sized field (there may
60
- be additional zero-sized fields). The effect is that the layout and ABI of the
61
- whole struct is guaranteed to be the same as that one field.
59
+ This can only be used on structs with a single non-zero-sized field
60
+ or enums with only one non-zero-sized field (there may be additional zero-sized fields).
61
+ The effect is that the layout and ABI of the whole struct/enum is guaranteed to be the same as that one field.
62
+
63
+ > NOTE: There's a ` transparent_unions ` nightly feature to apply ` repr(transparent) ` to unions,
64
+ > but it hasn't been stabilized due to design concerns. See the [ tracking issue] [ issue-60405 ] for more details.
62
65
63
66
The goal is to make it possible to transmute between the single field and the
64
- struct. An example of that is [ ` UnsafeCell ` ] , which can be transmuted into
67
+ struct/enum . An example of that is [ ` UnsafeCell ` ] , which can be transmuted into
65
68
the type it wraps ([ ` UnsafeCell ` ] also uses the unstable [ no_niche] [ no-niche-pull ] ,
66
69
so its ABI is not actually guaranteed to be the same when nested in other types).
67
70
68
- Also, passing the struct through FFI where the inner field type is expected on
69
- the other side is guaranteed to work. In particular, this is necessary for `struct
70
- Foo(f32)` to always have the same ABI as ` f32`.
71
+ Also, passing the struct/enum through FFI where the inner field type is expected on
72
+ the other side is guaranteed to work. In particular, this is necessary for
73
+ ` struct Foo(f32)` or ` enum Foo { Bar(f32) } ` to always have the same ABI as ` f32 ` .
71
74
72
75
This repr is only considered part of the public ABI of a type if either the single
73
76
field is ` pub ` , or if its layout is documented in prose. Otherwise, the layout should
74
77
not be relied upon by other crates.
75
78
76
- More details are in the [ RFC] [ rfc-transparent ] .
79
+ More details are in the [ RFC 1758 ] [ rfc-transparent ] and the [ RFC 2645 ] [ rfc-transparent-unions-enums ] .
77
80
78
81
## repr(u* ), repr(i* )
79
82
@@ -153,8 +156,10 @@ This is a modifier on `repr(C)` and `repr(Rust)`. It is incompatible with
153
156
[ unsafe code guidelines ] : https://rust-lang.github.io/unsafe-code-guidelines/layout.html
154
157
[ drop flags ] : drop-flags.html
155
158
[ ub loads ] : https://github.com/rust-lang/rust/issues/27060
159
+ [ issue-60405 ] : https://github.com/rust-lang/rust/issues/60405
156
160
[ `UnsafeCell` ] : ../std/cell/struct.UnsafeCell.html
157
161
[ rfc-transparent ] : https://github.com/rust-lang/rfcs/blob/master/text/1758-repr-transparent.md
162
+ [ rfc-transparent-unions-enums ] : https://rust-lang.github.io/rfcs/2645-transparent-unions.html
158
163
[ really-tagged ] : https://github.com/rust-lang/rfcs/blob/master/text/2195-really-tagged-unions.md
159
164
[ rust-bindgen ] : https://rust-lang.github.io/rust-bindgen/
160
165
[ cbindgen ] : https://github.com/eqrion/cbindgen
0 commit comments