Skip to content

Commit 8e6aa34

Browse files
authored
Merge pull request #376 from JohnTitor/update-repr-transparent
Update the `repr(transparent)` section to reflect the current state
2 parents 7378da1 + 7df6275 commit 8e6aa34

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/other-reprs.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,26 @@ compiled as normal.)
5656

5757
## repr(transparent)
5858

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+
`#[repr(transparent)]` can only be used on a struct or single-variant enum that has a single non-zero-sized field (there may be additional zero-sized fields).
60+
The effect is that the layout and ABI of the whole struct/enum is guaranteed to be the same as that one field.
61+
62+
> NOTE: There's a `transparent_unions` nightly feature to apply `repr(transparent)` to unions,
63+
> but it hasn't been stabilized due to design concerns. See the [tracking issue][issue-60405] for more details.
6264
6365
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
66+
struct/enum. An example of that is [`UnsafeCell`], which can be transmuted into
6567
the type it wraps ([`UnsafeCell`] also uses the unstable [no_niche][no-niche-pull],
6668
so its ABI is not actually guaranteed to be the same when nested in other types).
6769

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`.
70+
Also, passing the struct/enum through FFI where the inner field type is expected on
71+
the other side is guaranteed to work. In particular, this is necessary for
72+
`struct Foo(f32)` or `enum Foo { Bar(f32) }` to always have the same ABI as `f32`.
7173

7274
This repr is only considered part of the public ABI of a type if either the single
7375
field is `pub`, or if its layout is documented in prose. Otherwise, the layout should
7476
not be relied upon by other crates.
7577

76-
More details are in the [RFC][rfc-transparent].
78+
More details are in the [RFC 1758][rfc-transparent] and the [RFC 2645][rfc-transparent-unions-enums].
7779

7880
## repr(u*), repr(i*)
7981

@@ -153,8 +155,10 @@ This is a modifier on `repr(C)` and `repr(Rust)`. It is incompatible with
153155
[unsafe code guidelines]: https://rust-lang.github.io/unsafe-code-guidelines/layout.html
154156
[drop flags]: drop-flags.html
155157
[ub loads]: https://github.com/rust-lang/rust/issues/27060
158+
[issue-60405]: https://github.com/rust-lang/rust/issues/60405
156159
[`UnsafeCell`]: ../std/cell/struct.UnsafeCell.html
157160
[rfc-transparent]: https://github.com/rust-lang/rfcs/blob/master/text/1758-repr-transparent.md
161+
[rfc-transparent-unions-enums]: https://rust-lang.github.io/rfcs/2645-transparent-unions.html
158162
[really-tagged]: https://github.com/rust-lang/rfcs/blob/master/text/2195-really-tagged-unions.md
159163
[rust-bindgen]: https://rust-lang.github.io/rust-bindgen/
160164
[cbindgen]: https://github.com/eqrion/cbindgen

0 commit comments

Comments
 (0)