You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/other-reprs.md
+12-8Lines changed: 12 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -56,24 +56,26 @@ 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
+
`#[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.
62
64
63
65
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
65
67
the type it wraps ([`UnsafeCell`] also uses the unstable [no_niche][no-niche-pull],
66
68
so its ABI is not actually guaranteed to be the same when nested in other types).
67
69
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`.
71
73
72
74
This repr is only considered part of the public ABI of a type if either the single
73
75
field is `pub`, or if its layout is documented in prose. Otherwise, the layout should
74
76
not be relied upon by other crates.
75
77
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].
77
79
78
80
## repr(u*), repr(i*)
79
81
@@ -153,8 +155,10 @@ This is a modifier on `repr(C)` and `repr(Rust)`. It is incompatible with
0 commit comments