@@ -4668,8 +4668,8 @@ fn mut_from_prefix_suffix<T: FromBytes + KnownLayout + ?Sized>(
46684668///
46694669/// This derive analyzes, at compile time, whether the annotated type satisfies
46704670/// the [safety conditions] of `IntoBytes` and implements `IntoBytes` if it is
4671- /// sound to do so. This derive can be applied to structs, enums, and unions;
4672- /// e.g.:
4671+ /// sound to do so. This derive can be applied to structs and enums (see below
4672+ /// for union support); e.g.:
46734673///
46744674/// ```
46754675/// # use zerocopy_derive::{IntoBytes};
@@ -4689,15 +4689,6 @@ fn mut_from_prefix_suffix<T: FromBytes + KnownLayout + ?Sized>(
46894689/// ...
46904690/// # */
46914691/// }
4692- ///
4693- /// #[derive(IntoBytes)]
4694- /// #[repr(C)]
4695- /// union MyUnion {
4696- /// # variant: u8,
4697- /// # /*
4698- /// ...
4699- /// # */
4700- /// }
47014692/// ```
47024693///
47034694/// [safety conditions]: trait@IntoBytes#safety
@@ -4727,6 +4718,31 @@ fn mut_from_prefix_suffix<T: FromBytes + KnownLayout + ?Sized>(
47274718///
47284719/// [type layout]: https://doc.rust-lang.org/reference/type-layout.html
47294720///
4721+ /// # Unions
4722+ ///
4723+ /// Currently, union bit validity is [up in the air][union-validity], and so
4724+ /// zerocopy does not support `#[derive(IntoBytes)]` on unions by default.
4725+ /// However, implementing `IntoBytes` on a union type is likely sound on all
4726+ /// existing Rust toolchains - it's just that it may become unsound in the
4727+ /// future. You can opt-in to `#[derive(IntoBytes)]` support on unions by
4728+ /// passing the unstable `zerocopy_derive_union_into_bytes` cfg:
4729+ ///
4730+ /// ```shell
4731+ /// $ RUSTFLAGS='--cfg zerocopy_derive_union_into_bytes' cargo build
4732+ /// ```
4733+ ///
4734+ /// We make no stability guarantees regarding this cfg, and may remove it at any
4735+ /// point.
4736+ ///
4737+ /// We are actively working with Rust to stabilize the necessary language
4738+ /// guarantees to support this in a forwards-compatible way, which will enable
4739+ /// us to remove the cfg gate. As part of this effort, we need to know how much
4740+ /// demand there is for this feature. If you would like to use `IntoBytes` on
4741+ /// unions, [please let us know][discussion].
4742+ ///
4743+ /// [union-validity]: https://github.com/rust-lang/unsafe-code-guidelines/issues/438
4744+ /// [discussion]: https://github.com/google/zerocopy/discussions/1802
4745+ ///
47304746/// # Analysis
47314747///
47324748/// *This section describes, roughly, the analysis performed by this derive to
@@ -4790,15 +4806,6 @@ pub use zerocopy_derive::IntoBytes;
47904806/// ...
47914807/// # */
47924808/// }
4793- ///
4794- /// #[derive(IntoBytes)]
4795- /// #[repr(C)]
4796- /// union MyUnion {
4797- /// # variant: u8,
4798- /// # /*
4799- /// ...
4800- /// # */
4801- /// }
48024809/// ```
48034810///
48044811/// This derive performs a sophisticated, compile-time safety analysis to
0 commit comments