zerocopy/
impls.rs

1// Copyright 2024 The Fuchsia Authors
2//
3// Licensed under the 2-Clause BSD License <LICENSE-BSD or
4// https://opensource.org/license/bsd-2-clause>, Apache License, Version 2.0
5// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
6// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
7// This file may not be copied, modified, or distributed except according to
8// those terms.
9
10use core::mem::MaybeUninit as CoreMaybeUninit;
11
12use super::*;
13
14safety_comment! {
15    /// SAFETY:
16    /// Per the reference [1], "the unit tuple (`()`) ... is guaranteed as a
17    /// zero-sized type to have a size of 0 and an alignment of 1."
18    /// - `Immutable`: `()` self-evidently does not contain any `UnsafeCell`s.
19    /// - `TryFromBytes` (with no validator), `FromZeros`, `FromBytes`: There is
20    ///   only one possible sequence of 0 bytes, and `()` is inhabited.
21    /// - `IntoBytes`: Since `()` has size 0, it contains no padding bytes.
22    /// - `Unaligned`: `()` has alignment 1.
23    ///
24    /// [1] https://doc.rust-lang.org/1.81.0/reference/type-layout.html#tuple-layout
25    unsafe_impl!((): Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
26    assert_unaligned!(());
27}
28
29safety_comment! {
30    /// SAFETY:
31    /// - `Immutable`: These types self-evidently do not contain any
32    ///   `UnsafeCell`s.
33    /// - `TryFromBytes` (with no validator), `FromZeros`, `FromBytes`: all bit
34    ///   patterns are valid for numeric types [1]
35    /// - `IntoBytes`: numeric types have no padding bytes [1]
36    /// - `Unaligned` (`u8` and `i8` only): The reference [2] specifies the size
37    ///   of `u8` and `i8` as 1 byte. We also know that:
38    ///   - Alignment is >= 1 [3]
39    ///   - Size is an integer multiple of alignment [4]
40    ///   - The only value >= 1 for which 1 is an integer multiple is 1
41    ///   Therefore, the only possible alignment for `u8` and `i8` is 1.
42    ///
43    /// [1] Per https://doc.rust-lang.org/1.81.0/reference/types/numeric.html#bit-validity:
44    ///
45    ///     For every numeric type, `T`, the bit validity of `T` is equivalent to
46    ///     the bit validity of `[u8; size_of::<T>()]`. An uninitialized byte is
47    ///     not a valid `u8`.
48    ///
49    /// [2] https://doc.rust-lang.org/1.81.0/reference/type-layout.html#primitive-data-layout
50    ///
51    /// [3] Per https://doc.rust-lang.org/1.81.0/reference/type-layout.html#size-and-alignment:
52    ///
53    ///     Alignment is measured in bytes, and must be at least 1.
54    ///
55    /// [4] Per https://doc.rust-lang.org/1.81.0/reference/type-layout.html#size-and-alignment:
56    ///
57    ///     The size of a value is always a multiple of its alignment.
58    ///
59    /// TODO(#278): Once we've updated the trait docs to refer to `u8`s rather
60    /// than bits or bytes, update this comment, especially the reference to
61    /// [1].
62    unsafe_impl!(u8: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
63    unsafe_impl!(i8: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
64    assert_unaligned!(u8, i8);
65    unsafe_impl!(u16: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
66    unsafe_impl!(i16: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
67    unsafe_impl!(u32: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
68    unsafe_impl!(i32: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
69    unsafe_impl!(u64: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
70    unsafe_impl!(i64: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
71    unsafe_impl!(u128: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
72    unsafe_impl!(i128: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
73    unsafe_impl!(usize: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
74    unsafe_impl!(isize: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
75    unsafe_impl!(f32: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
76    unsafe_impl!(f64: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
77    #[cfg(feature = "float-nightly")]
78    unsafe_impl!(#[cfg_attr(doc_cfg, doc(cfg(feature = "float-nightly")))] f16: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
79    #[cfg(feature = "float-nightly")]
80    unsafe_impl!(#[cfg_attr(doc_cfg, doc(cfg(feature = "float-nightly")))] f128: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
81}
82
83safety_comment! {
84    /// SAFETY:
85    /// - `Immutable`: `bool` self-evidently does not contain any `UnsafeCell`s.
86    /// - `FromZeros`: Valid since "[t]he value false has the bit pattern 0x00"
87    ///   [1].
88    /// - `IntoBytes`: Since "the boolean type has a size and alignment of 1
89    ///   each" and "The value false has the bit pattern 0x00 and the value true
90    ///   has the bit pattern 0x01" [1]. Thus, the only byte of the bool is
91    ///   always initialized.
92    /// - `Unaligned`: Per the reference [1], "[a]n object with the boolean type
93    ///   has a size and alignment of 1 each."
94    ///
95    /// [1] https://doc.rust-lang.org/1.81.0/reference/types/boolean.html
96    unsafe_impl!(bool: Immutable, FromZeros, IntoBytes, Unaligned);
97    assert_unaligned!(bool);
98    /// SAFETY:
99    /// - The safety requirements for `unsafe_impl!` with an `is_bit_valid`
100    ///   closure:
101    ///   - Given `t: *mut bool` and `let r = *mut u8`, `r` refers to an object
102    ///     of the same size as that referred to by `t`. This is true because
103    ///     `bool` and `u8` have the same size (1 byte) [1]. Neither `r` nor `t`
104    ///     contain `UnsafeCell`s because neither `bool` nor `u8` do [4].
105    ///   - Since the closure takes a `&u8` argument, given a `Maybe<'a,
106    ///     bool>` which satisfies the preconditions of
107    ///     `TryFromBytes::<bool>::is_bit_valid`, it must be guaranteed that the
108    ///     memory referenced by that `MaybeValid` always contains a valid `u8`.
109    ///     Since `bool`'s single byte is always initialized, `is_bit_valid`'s
110    ///     precondition requires that the same is true of its argument. Since
111    ///     `u8`'s only bit validity invariant is that its single byte must be
112    ///     initialized, this memory is guaranteed to contain a valid `u8`.
113    ///   - The impl must only return `true` for its argument if the original
114    ///     `Maybe<bool>` refers to a valid `bool`. We only return true if
115    ///     the `u8` value is 0 or 1, and both of these are valid values for
116    ///     `bool`. [3]
117    ///
118    /// [1] Per https://doc.rust-lang.org/1.81.0/reference/type-layout.html#primitive-data-layout:
119    ///
120    ///   The size of most primitives is given in this table.
121    ///
122    ///   | Type      | `size_of::<Type>() ` |
123    ///   |-----------|----------------------|
124    ///   | `bool`    | 1                    |
125    ///   | `u8`/`i8` | 1                    |
126    ///
127    /// [2] Per https://doc.rust-lang.org/1.81.0/reference/type-layout.html#size-and-alignment:
128    ///
129    ///   The size of a value is always a multiple of its alignment.
130    ///
131    /// [3] Per https://doc.rust-lang.org/1.81.0/reference/types/boolean.html:
132    ///
133    ///   The value false has the bit pattern 0x00 and the value true has the
134    ///   bit pattern 0x01.
135    ///
136    /// [4] TODO(#429): Justify this claim.
137    unsafe_impl!(bool: TryFromBytes; |byte: MaybeAligned<u8>| *byte.unaligned_as_ref() < 2);
138}
139safety_comment! {
140    /// SAFETY:
141    /// - `Immutable`: `char` self-evidently does not contain any `UnsafeCell`s.
142    /// - `FromZeros`: Per reference [1], "[a] value of type char is a Unicode
143    ///   scalar value (i.e. a code point that is not a surrogate), represented
144    ///   as a 32-bit unsigned word in the 0x0000 to 0xD7FF or 0xE000 to
145    ///   0x10FFFF range" which contains 0x0000.
146    /// - `IntoBytes`: `char` is per reference [1] "represented as a 32-bit
147    ///   unsigned word" (`u32`) which is `IntoBytes`. Note that unlike `u32`,
148    ///   not all bit patterns are valid for `char`.
149    ///
150    /// [1] https://doc.rust-lang.org/1.81.0/reference/types/textual.html
151    unsafe_impl!(char: Immutable, FromZeros, IntoBytes);
152    /// SAFETY:
153    /// - The safety requirements for `unsafe_impl!` with an `is_bit_valid`
154    ///   closure:
155    ///   - Given `t: *mut char` and `let r = *mut u32`, `r` refers to an object
156    ///     of the same size as that referred to by `t`. This is true because
157    ///     `char` and `u32` have the same size [1]. Neither `r` nor `t` contain
158    ///     `UnsafeCell`s because neither `char` nor `u32` do [4].
159    ///   - Since the closure takes a `&u32` argument, given a `Maybe<'a,
160    ///     char>` which satisfies the preconditions of
161    ///     `TryFromBytes::<char>::is_bit_valid`, it must be guaranteed that the
162    ///     memory referenced by that `MaybeValid` always contains a valid
163    ///     `u32`. Since `char`'s bytes are always initialized [2],
164    ///     `is_bit_valid`'s precondition requires that the same is true of its
165    ///     argument. Since `u32`'s only bit validity invariant is that its
166    ///     bytes must be initialized, this memory is guaranteed to contain a
167    ///     valid `u32`.
168    ///   - The impl must only return `true` for its argument if the original
169    ///     `Maybe<char>` refers to a valid `char`. `char::from_u32`
170    ///     guarantees that it returns `None` if its input is not a valid
171    ///     `char`. [3]
172    ///
173    /// [1] Per https://doc.rust-lang.org/nightly/reference/types/textual.html#layout-and-bit-validity:
174    ///
175    ///   `char` is guaranteed to have the same size and alignment as `u32` on
176    ///   all platforms.
177    ///
178    /// [2] Per https://doc.rust-lang.org/core/primitive.char.html#method.from_u32:
179    ///
180    ///   Every byte of a `char` is guaranteed to be initialized.
181    ///
182    /// [3] Per https://doc.rust-lang.org/core/primitive.char.html#method.from_u32:
183    ///
184    ///   `from_u32()` will return `None` if the input is not a valid value for
185    ///   a `char`.
186    ///
187    /// [4] TODO(#429): Justify this claim.
188    unsafe_impl!(char: TryFromBytes; |candidate: MaybeAligned<u32>| {
189        let candidate = candidate.read_unaligned::<BecauseImmutable>();
190        char::from_u32(candidate).is_some()
191    });
192}
193safety_comment! {
194    /// SAFETY:
195    /// Per the Reference [1], `str` has the same layout as `[u8]`.
196    /// - `Immutable`: `[u8]` does not contain any `UnsafeCell`s.
197    /// - `FromZeros`, `IntoBytes`, `Unaligned`: `[u8]` is `FromZeros`,
198    ///   `IntoBytes`, and `Unaligned`.
199    ///
200    /// Note that we don't `assert_unaligned!(str)` because `assert_unaligned!`
201    /// uses `align_of`, which only works for `Sized` types.
202    ///
203    /// TODO(#429):
204    /// - Add quotes from documentation.
205    /// - Improve safety proof for `FromZeros` and `IntoBytes`; having the same
206    ///   layout as `[u8]` isn't sufficient.
207    ///
208    /// [1] https://doc.rust-lang.org/1.81.0/reference/type-layout.html#str-layout
209    unsafe_impl!(str: Immutable, FromZeros, IntoBytes, Unaligned);
210    /// SAFETY:
211    /// - The safety requirements for `unsafe_impl!` with an `is_bit_valid`
212    ///   closure:
213    ///   - Given `t: *mut str` and `let r = *mut [u8]`, `r` refers to an object
214    ///     of the same size as that referred to by `t`. This is true because
215    ///     `str` and `[u8]` have the same representation. [1] Neither `t` nor
216    ///     `r` contain `UnsafeCell`s because `[u8]` doesn't, and both `t` and
217    ///     `r` have that representation.
218    ///   - Since the closure takes a `&[u8]` argument, given a `Maybe<'a,
219    ///     str>` which satisfies the preconditions of
220    ///     `TryFromBytes::<str>::is_bit_valid`, it must be guaranteed that the
221    ///     memory referenced by that `MaybeValid` always contains a valid
222    ///     `[u8]`. Since `str`'s bytes are always initialized [1],
223    ///     `is_bit_valid`'s precondition requires that the same is true of its
224    ///     argument. Since `[u8]`'s only bit validity invariant is that its
225    ///     bytes must be initialized, this memory is guaranteed to contain a
226    ///     valid `[u8]`.
227    ///   - The impl must only return `true` for its argument if the original
228    ///     `Maybe<str>` refers to a valid `str`. `str::from_utf8`
229    ///     guarantees that it returns `Err` if its input is not a valid `str`.
230    ///     [2]
231    ///
232    /// [1] Per https://doc.rust-lang.org/1.81.0/reference/types/textual.html:
233    ///
234    ///   A value of type `str` is represented the same was as `[u8]`.
235    ///
236    /// [2] Per https://doc.rust-lang.org/core/str/fn.from_utf8.html#errors:
237    ///
238    ///   Returns `Err` if the slice is not UTF-8.
239    unsafe_impl!(str: TryFromBytes; |candidate: MaybeAligned<[u8]>| {
240        let candidate = candidate.unaligned_as_ref();
241        core::str::from_utf8(candidate).is_ok()
242    });
243}
244
245safety_comment! {
246    // `NonZeroXxx` is `IntoBytes`, but not `FromZeros` or `FromBytes`.
247    //
248    /// SAFETY:
249    /// - `IntoBytes`: `NonZeroXxx` has the same layout as its associated
250    ///    primitive. Since it is the same size, this guarantees it has no
251    ///    padding - integers have no padding, and there's no room for padding
252    ///    if it can represent all of the same values except 0.
253    /// - `Unaligned`: `NonZeroU8` and `NonZeroI8` document that
254    ///   `Option<NonZeroU8>` and `Option<NonZeroI8>` both have size 1. [1] [2]
255    ///   This is worded in a way that makes it unclear whether it's meant as a
256    ///   guarantee, but given the purpose of those types, it's virtually
257    ///   unthinkable that that would ever change. `Option` cannot be smaller
258    ///   than its contained type, which implies that, and `NonZeroX8` are of
259    ///   size 1 or 0. `NonZeroX8` can represent multiple states, so they cannot
260    ///   be 0 bytes, which means that they must be 1 byte. The only valid
261    ///   alignment for a 1-byte type is 1.
262    ///
263    /// TODO(#429):
264    /// - Add quotes from documentation.
265    /// - Add safety comment for `Immutable`. How can we prove that `NonZeroXxx`
266    ///   doesn't contain any `UnsafeCell`s? It's obviously true, but it's not
267    ///   clear how we'd prove it short of adding text to the stdlib docs that
268    ///   says so explicitly, which likely wouldn't be accepted.
269    ///
270    /// [1] https://doc.rust-lang.org/1.81.0/std/num/type.NonZeroU8.html
271    ///
272    ///     `NonZeroU8` is guaranteed to have the same layout and bit validity as `u8` with
273    ///     the exception that 0 is not a valid instance
274    ///
275    /// [2] https://doc.rust-lang.org/1.81.0/std/num/type.NonZeroI8.html
276    /// TODO(https://github.com/rust-lang/rust/pull/104082): Cite documentation
277    /// that layout is the same as primitive layout.
278    unsafe_impl!(NonZeroU8: Immutable, IntoBytes, Unaligned);
279    unsafe_impl!(NonZeroI8: Immutable, IntoBytes, Unaligned);
280    assert_unaligned!(NonZeroU8, NonZeroI8);
281    unsafe_impl!(NonZeroU16: Immutable, IntoBytes);
282    unsafe_impl!(NonZeroI16: Immutable, IntoBytes);
283    unsafe_impl!(NonZeroU32: Immutable, IntoBytes);
284    unsafe_impl!(NonZeroI32: Immutable, IntoBytes);
285    unsafe_impl!(NonZeroU64: Immutable, IntoBytes);
286    unsafe_impl!(NonZeroI64: Immutable, IntoBytes);
287    unsafe_impl!(NonZeroU128: Immutable, IntoBytes);
288    unsafe_impl!(NonZeroI128: Immutable, IntoBytes);
289    unsafe_impl!(NonZeroUsize: Immutable, IntoBytes);
290    unsafe_impl!(NonZeroIsize: Immutable, IntoBytes);
291    /// SAFETY:
292    /// - The safety requirements for `unsafe_impl!` with an `is_bit_valid`
293    ///   closure:
294    ///   - Given `t: *mut NonZeroXxx` and `let r = *mut xxx`, `r` refers to an
295    ///     object of the same size as that referred to by `t`. This is true
296    ///     because `NonZeroXxx` and `xxx` have the same size. [1] Neither `r`
297    ///     nor `t` refer to any `UnsafeCell`s because neither `NonZeroXxx` [2]
298    ///     nor `xxx` do.
299    ///   - Since the closure takes a `&xxx` argument, given a `Maybe<'a,
300    ///     NonZeroXxx>` which satisfies the preconditions of
301    ///     `TryFromBytes::<NonZeroXxx>::is_bit_valid`, it must be guaranteed
302    ///     that the memory referenced by that `MabyeValid` always contains a
303    ///     valid `xxx`. Since `NonZeroXxx`'s bytes are always initialized [1],
304    ///     `is_bit_valid`'s precondition requires that the same is true of its
305    ///     argument. Since `xxx`'s only bit validity invariant is that its
306    ///     bytes must be initialized, this memory is guaranteed to contain a
307    ///     valid `xxx`.
308    ///   - The impl must only return `true` for its argument if the original
309    ///     `Maybe<NonZeroXxx>` refers to a valid `NonZeroXxx`. The only
310    ///     `xxx` which is not also a valid `NonZeroXxx` is 0. [1]
311    ///
312    /// [1] Per https://doc.rust-lang.org/1.81.0/core/num/type.NonZeroU16.html:
313    ///
314    ///   `NonZeroU16` is guaranteed to have the same layout and bit validity as
315    ///   `u16` with the exception that `0` is not a valid instance.
316    ///
317    /// [2] `NonZeroXxx` self-evidently does not contain `UnsafeCell`s. This is
318    ///     not a proof, but we are accepting this as a known risk per #1358.
319    unsafe_impl!(NonZeroU8: TryFromBytes; |n: MaybeAligned<u8>| NonZeroU8::new(n.read_unaligned::<BecauseImmutable>()).is_some());
320    unsafe_impl!(NonZeroI8: TryFromBytes; |n: MaybeAligned<i8>| NonZeroI8::new(n.read_unaligned::<BecauseImmutable>()).is_some());
321    unsafe_impl!(NonZeroU16: TryFromBytes; |n: MaybeAligned<u16>| NonZeroU16::new(n.read_unaligned::<BecauseImmutable>()).is_some());
322    unsafe_impl!(NonZeroI16: TryFromBytes; |n: MaybeAligned<i16>| NonZeroI16::new(n.read_unaligned::<BecauseImmutable>()).is_some());
323    unsafe_impl!(NonZeroU32: TryFromBytes; |n: MaybeAligned<u32>| NonZeroU32::new(n.read_unaligned::<BecauseImmutable>()).is_some());
324    unsafe_impl!(NonZeroI32: TryFromBytes; |n: MaybeAligned<i32>| NonZeroI32::new(n.read_unaligned::<BecauseImmutable>()).is_some());
325    unsafe_impl!(NonZeroU64: TryFromBytes; |n: MaybeAligned<u64>| NonZeroU64::new(n.read_unaligned::<BecauseImmutable>()).is_some());
326    unsafe_impl!(NonZeroI64: TryFromBytes; |n: MaybeAligned<i64>| NonZeroI64::new(n.read_unaligned::<BecauseImmutable>()).is_some());
327    unsafe_impl!(NonZeroU128: TryFromBytes; |n: MaybeAligned<u128>| NonZeroU128::new(n.read_unaligned::<BecauseImmutable>()).is_some());
328    unsafe_impl!(NonZeroI128: TryFromBytes; |n: MaybeAligned<i128>| NonZeroI128::new(n.read_unaligned::<BecauseImmutable>()).is_some());
329    unsafe_impl!(NonZeroUsize: TryFromBytes; |n: MaybeAligned<usize>| NonZeroUsize::new(n.read_unaligned::<BecauseImmutable>()).is_some());
330    unsafe_impl!(NonZeroIsize: TryFromBytes; |n: MaybeAligned<isize>| NonZeroIsize::new(n.read_unaligned::<BecauseImmutable>()).is_some());
331}
332safety_comment! {
333    /// SAFETY:
334    /// - `TryFromBytes` (with no validator), `FromZeros`, `FromBytes`,
335    ///   `IntoBytes`: The Rust compiler reuses `0` value to represent `None`,
336    ///   so `size_of::<Option<NonZeroXxx>>() == size_of::<xxx>()`; see
337    ///   `NonZeroXxx` documentation.
338    /// - `Unaligned`: `NonZeroU8` and `NonZeroI8` document that
339    ///   `Option<NonZeroU8>` and `Option<NonZeroI8>` both have size 1. [1] [2]
340    ///   This is worded in a way that makes it unclear whether it's meant as a
341    ///   guarantee, but given the purpose of those types, it's virtually
342    ///   unthinkable that that would ever change. The only valid alignment for
343    ///   a 1-byte type is 1.
344    ///
345    /// TODO(#429): Add quotes from documentation.
346    ///
347    /// [1] https://doc.rust-lang.org/stable/std/num/struct.NonZeroU8.html
348    /// [2] https://doc.rust-lang.org/stable/std/num/struct.NonZeroI8.html
349    ///
350    /// TODO(https://github.com/rust-lang/rust/pull/104082): Cite documentation
351    /// for layout guarantees.
352    unsafe_impl!(Option<NonZeroU8>: TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
353    unsafe_impl!(Option<NonZeroI8>: TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
354    assert_unaligned!(Option<NonZeroU8>, Option<NonZeroI8>);
355    unsafe_impl!(Option<NonZeroU16>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
356    unsafe_impl!(Option<NonZeroI16>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
357    unsafe_impl!(Option<NonZeroU32>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
358    unsafe_impl!(Option<NonZeroI32>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
359    unsafe_impl!(Option<NonZeroU64>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
360    unsafe_impl!(Option<NonZeroI64>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
361    unsafe_impl!(Option<NonZeroU128>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
362    unsafe_impl!(Option<NonZeroI128>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
363    unsafe_impl!(Option<NonZeroUsize>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
364    unsafe_impl!(Option<NonZeroIsize>: TryFromBytes, FromZeros, FromBytes, IntoBytes);
365}
366
367safety_comment! {
368    /// SAFETY:
369    /// While it's not fully documented, the consensus is that `Box<T>` does not
370    /// contain any `UnsafeCell`s for `T: Sized` [1]. This is not a complete
371    /// proof, but we are accepting this as a known risk per #1358.
372    ///
373    /// [1] https://github.com/rust-lang/unsafe-code-guidelines/issues/492
374    #[cfg(feature = "alloc")]
375    unsafe_impl!(
376        #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
377        T: Sized => Immutable for Box<T>
378    );
379}
380
381safety_comment! {
382    /// SAFETY:
383    /// The following types can be transmuted from `[0u8; size_of::<T>()]`. [1]
384    ///
385    /// [1] Per https://doc.rust-lang.org/nightly/core/option/index.html#representation:
386    ///
387    ///   Rust guarantees to optimize the following types `T` such that
388    ///   [`Option<T>`] has the same size and alignment as `T`. In some of these
389    ///   cases, Rust further guarantees that `transmute::<_, Option<T>>([0u8;
390    ///   size_of::<T>()])` is sound and produces `Option::<T>::None`. These
391    ///   cases are identified by the second column:
392    ///
393    ///   | `T`                   | `transmute::<_, Option<T>>([0u8; size_of::<T>()])` sound? |
394    ///   |-----------------------|-----------------------------------------------------------|
395    ///   | [`Box<U>`]            | when `U: Sized`                                           |
396    ///   | `&U`                  | when `U: Sized`                                           |
397    ///   | `&mut U`              | when `U: Sized`                                           |
398    ///   | [`ptr::NonNull<U>`]   | when `U: Sized`                                           |
399    ///   | `fn`, `extern "C" fn` | always                                                    |
400    ///
401    /// TODO(#429), TODO(https://github.com/rust-lang/rust/pull/115333): Cite
402    /// the Stable docs once they're available.
403    #[cfg(feature = "alloc")]
404    unsafe_impl!(
405        #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
406        T => TryFromBytes for Option<Box<T>>;
407        |c: Maybe<Option<Box<T>>>| pointer::is_zeroed(c)
408    );
409    #[cfg(feature = "alloc")]
410    unsafe_impl!(
411        #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
412        T => FromZeros for Option<Box<T>>
413    );
414    unsafe_impl!(
415        T => TryFromBytes for Option<&'_ T>;
416        |c: Maybe<Option<&'_ T>>| pointer::is_zeroed(c)
417    );
418    unsafe_impl!(T => FromZeros for Option<&'_ T>);
419    unsafe_impl!(
420            T => TryFromBytes for Option<&'_ mut T>;
421            |c: Maybe<Option<&'_ mut T>>| pointer::is_zeroed(c)
422    );
423    unsafe_impl!(T => FromZeros for Option<&'_ mut T>);
424    unsafe_impl!(
425        T => TryFromBytes for Option<NonNull<T>>;
426        |c: Maybe<Option<NonNull<T>>>| pointer::is_zeroed(c)
427    );
428    unsafe_impl!(T => FromZeros for Option<NonNull<T>>);
429    unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => FromZeros for opt_fn!(...));
430    unsafe_impl_for_power_set!(
431        A, B, C, D, E, F, G, H, I, J, K, L -> M => TryFromBytes for opt_fn!(...);
432        |c: Maybe<Self>| pointer::is_zeroed(c)
433    );
434    unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => FromZeros for opt_extern_c_fn!(...));
435    unsafe_impl_for_power_set!(
436        A, B, C, D, E, F, G, H, I, J, K, L -> M => TryFromBytes for opt_extern_c_fn!(...);
437        |c: Maybe<Self>| pointer::is_zeroed(c)
438    );
439}
440
441safety_comment! {
442    /// SAFETY:
443    /// `fn()` and `extern "C" fn()` self-evidently do not contain
444    /// `UnsafeCell`s. This is not a proof, but we are accepting this as a known
445    /// risk per #1358.
446    unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => Immutable for opt_fn!(...));
447    unsafe_impl_for_power_set!(A, B, C, D, E, F, G, H, I, J, K, L -> M => Immutable for opt_extern_c_fn!(...));
448}
449
450#[cfg(all(
451    zerocopy_target_has_atomics_1_60_0,
452    any(
453        target_has_atomic = "8",
454        target_has_atomic = "16",
455        target_has_atomic = "32",
456        target_has_atomic = "64",
457        target_has_atomic = "ptr"
458    )
459))]
460#[cfg_attr(doc_cfg, doc(cfg(rust = "1.60.0")))]
461mod atomics {
462    use super::*;
463
464    macro_rules! impl_traits_for_atomics {
465        ($($atomics:ident),* $(,)?) => {
466            $(
467                impl_known_layout!($atomics);
468                impl_for_transparent_wrapper!(=> TryFromBytes for $atomics);
469                impl_for_transparent_wrapper!(=> FromZeros for $atomics);
470                impl_for_transparent_wrapper!(=> FromBytes for $atomics);
471                impl_for_transparent_wrapper!(=> IntoBytes for $atomics);
472            )*
473        };
474    }
475
476    #[cfg(target_has_atomic = "8")]
477    #[cfg_attr(doc_cfg, doc(cfg(target_has_atomic = "8")))]
478    mod atomic_8 {
479        use core::sync::atomic::{AtomicBool, AtomicI8, AtomicU8};
480
481        use super::*;
482
483        impl_traits_for_atomics!(AtomicU8, AtomicI8);
484
485        impl_known_layout!(AtomicBool);
486
487        impl_for_transparent_wrapper!(=> TryFromBytes for AtomicBool);
488        impl_for_transparent_wrapper!(=> FromZeros for AtomicBool);
489        impl_for_transparent_wrapper!(=> IntoBytes for AtomicBool);
490
491        safety_comment! {
492            /// SAFETY:
493            /// Per [1], `AtomicBool`, `AtomicU8`, and `AtomicI8` have the same
494            /// size as `bool`, `u8`, and `i8` respectively. Since a type's
495            /// alignment cannot be smaller than 1 [2], and since its alignment
496            /// cannot be greater than its size [3], the only possible value for
497            /// the alignment is 1. Thus, it is sound to implement `Unaligned`.
498            ///
499            /// [1] Per (for example) https://doc.rust-lang.org/1.81.0/std/sync/atomic/struct.AtomicU8.html:
500            ///
501            ///   This type has the same size, alignment, and bit validity as
502            ///   the underlying integer type
503            ///
504            /// [2] Per https://doc.rust-lang.org/1.81.0/reference/type-layout.html#size-and-alignment:
505            ///
506            ///     Alignment is measured in bytes, and must be at least 1.
507            ///
508            /// [3] Per https://doc.rust-lang.org/1.81.0/reference/type-layout.html#size-and-alignment:
509            ///
510            ///     The size of a value is always a multiple of its alignment.
511            unsafe_impl!(AtomicBool: Unaligned);
512            unsafe_impl!(AtomicU8: Unaligned);
513            unsafe_impl!(AtomicI8: Unaligned);
514            assert_unaligned!(AtomicBool, AtomicU8, AtomicI8);
515
516            /// SAFETY:
517            /// All of these pass an atomic type and that type's native equivalent, as
518            /// required by the macro safety preconditions.
519            unsafe_impl_transparent_wrapper_for_atomic!(AtomicU8 [u8], AtomicI8 [i8], AtomicBool [bool]);
520        }
521    }
522
523    #[cfg(target_has_atomic = "16")]
524    #[cfg_attr(doc_cfg, doc(cfg(target_has_atomic = "16")))]
525    mod atomic_16 {
526        use core::sync::atomic::{AtomicI16, AtomicU16};
527
528        use super::*;
529
530        impl_traits_for_atomics!(AtomicU16, AtomicI16);
531
532        safety_comment! {
533            /// SAFETY:
534            /// All of these pass an atomic type and that type's native equivalent, as
535            /// required by the macro safety preconditions.
536            unsafe_impl_transparent_wrapper_for_atomic!(AtomicU16 [u16], AtomicI16 [i16]);
537        }
538    }
539
540    #[cfg(target_has_atomic = "32")]
541    #[cfg_attr(doc_cfg, doc(cfg(target_has_atomic = "32")))]
542    mod atomic_32 {
543        use core::sync::atomic::{AtomicI32, AtomicU32};
544
545        use super::*;
546
547        impl_traits_for_atomics!(AtomicU32, AtomicI32);
548
549        safety_comment! {
550            /// SAFETY:
551            /// All of these pass an atomic type and that type's native equivalent, as
552            /// required by the macro safety preconditions.
553            unsafe_impl_transparent_wrapper_for_atomic!(AtomicU32 [u32], AtomicI32 [i32]);
554        }
555    }
556
557    #[cfg(target_has_atomic = "64")]
558    #[cfg_attr(doc_cfg, doc(cfg(target_has_atomic = "64")))]
559    mod atomic_64 {
560        use core::sync::atomic::{AtomicI64, AtomicU64};
561
562        use super::*;
563
564        impl_traits_for_atomics!(AtomicU64, AtomicI64);
565
566        safety_comment! {
567            /// SAFETY:
568            /// All of these pass an atomic type and that type's native equivalent, as
569            /// required by the macro safety preconditions.
570            unsafe_impl_transparent_wrapper_for_atomic!(AtomicU64 [u64], AtomicI64 [i64]);
571        }
572    }
573
574    #[cfg(target_has_atomic = "ptr")]
575    #[cfg_attr(doc_cfg, doc(cfg(target_has_atomic = "ptr")))]
576    mod atomic_ptr {
577        use core::sync::atomic::{AtomicIsize, AtomicPtr, AtomicUsize};
578
579        use super::*;
580
581        impl_traits_for_atomics!(AtomicUsize, AtomicIsize);
582
583        impl_known_layout!(T => AtomicPtr<T>);
584
585        // TODO(#170): Implement `FromBytes` and `IntoBytes` once we implement
586        // those traits for `*mut T`.
587        impl_for_transparent_wrapper!(T => TryFromBytes for AtomicPtr<T>);
588        impl_for_transparent_wrapper!(T => FromZeros for AtomicPtr<T>);
589
590        safety_comment! {
591            /// SAFETY:
592            /// This passes an atomic type and that type's native equivalent, as
593            /// required by the macro safety preconditions.
594            unsafe_impl_transparent_wrapper_for_atomic!(AtomicUsize [usize], AtomicIsize [isize]);
595            unsafe_impl_transparent_wrapper_for_atomic!(T => AtomicPtr<T> [*mut T]);
596        }
597    }
598}
599
600safety_comment! {
601    /// SAFETY:
602    /// Per reference [1]:
603    /// "For all T, the following are guaranteed:
604    /// size_of::<PhantomData<T>>() == 0
605    /// align_of::<PhantomData<T>>() == 1".
606    /// This gives:
607    /// - `Immutable`: `PhantomData` has no fields.
608    /// - `TryFromBytes` (with no validator), `FromZeros`, `FromBytes`: There is
609    ///   only one possible sequence of 0 bytes, and `PhantomData` is inhabited.
610    /// - `IntoBytes`: Since `PhantomData` has size 0, it contains no padding
611    ///   bytes.
612    /// - `Unaligned`: Per the preceding reference, `PhantomData` has alignment
613    ///   1.
614    ///
615    /// [1] https://doc.rust-lang.org/1.81.0/std/marker/struct.PhantomData.html#layout-1
616    unsafe_impl!(T: ?Sized => Immutable for PhantomData<T>);
617    unsafe_impl!(T: ?Sized => TryFromBytes for PhantomData<T>);
618    unsafe_impl!(T: ?Sized => FromZeros for PhantomData<T>);
619    unsafe_impl!(T: ?Sized => FromBytes for PhantomData<T>);
620    unsafe_impl!(T: ?Sized => IntoBytes for PhantomData<T>);
621    unsafe_impl!(T: ?Sized => Unaligned for PhantomData<T>);
622    assert_unaligned!(PhantomData<()>, PhantomData<u8>, PhantomData<u64>);
623}
624
625impl_for_transparent_wrapper!(T: Immutable => Immutable for Wrapping<T>);
626impl_for_transparent_wrapper!(T: TryFromBytes => TryFromBytes for Wrapping<T>);
627impl_for_transparent_wrapper!(T: FromZeros => FromZeros for Wrapping<T>);
628impl_for_transparent_wrapper!(T: FromBytes => FromBytes for Wrapping<T>);
629impl_for_transparent_wrapper!(T: IntoBytes => IntoBytes for Wrapping<T>);
630impl_for_transparent_wrapper!(T: Unaligned => Unaligned for Wrapping<T>);
631assert_unaligned!(Wrapping<()>, Wrapping<u8>);
632
633safety_comment! {
634    /// SAFETY:
635    /// `TryFromBytes` (with no validator), `FromZeros`, `FromBytes`:
636    /// `MaybeUninit<T>` has no restrictions on its contents.
637    unsafe_impl!(T => TryFromBytes for CoreMaybeUninit<T>);
638    unsafe_impl!(T => FromZeros for CoreMaybeUninit<T>);
639    unsafe_impl!(T => FromBytes for CoreMaybeUninit<T>);
640}
641
642impl_for_transparent_wrapper!(T: Immutable => Immutable for CoreMaybeUninit<T>);
643impl_for_transparent_wrapper!(T: Unaligned => Unaligned for CoreMaybeUninit<T>);
644assert_unaligned!(CoreMaybeUninit<()>, CoreMaybeUninit<u8>);
645
646impl_for_transparent_wrapper!(T: ?Sized + Immutable => Immutable for ManuallyDrop<T>);
647impl_for_transparent_wrapper!(T: ?Sized + TryFromBytes => TryFromBytes for ManuallyDrop<T>);
648impl_for_transparent_wrapper!(T: ?Sized + FromZeros => FromZeros for ManuallyDrop<T>);
649impl_for_transparent_wrapper!(T: ?Sized + FromBytes => FromBytes for ManuallyDrop<T>);
650impl_for_transparent_wrapper!(T: ?Sized + IntoBytes => IntoBytes for ManuallyDrop<T>);
651impl_for_transparent_wrapper!(T: ?Sized + Unaligned => Unaligned for ManuallyDrop<T>);
652assert_unaligned!(ManuallyDrop<()>, ManuallyDrop<u8>);
653
654impl_for_transparent_wrapper!(T: ?Sized + FromZeros => FromZeros for UnsafeCell<T>);
655impl_for_transparent_wrapper!(T: ?Sized + FromBytes => FromBytes for UnsafeCell<T>);
656impl_for_transparent_wrapper!(T: ?Sized + IntoBytes => IntoBytes for UnsafeCell<T>);
657impl_for_transparent_wrapper!(T: ?Sized + Unaligned => Unaligned for UnsafeCell<T>);
658assert_unaligned!(UnsafeCell<()>, UnsafeCell<u8>);
659
660// SAFETY: See safety comment in `is_bit_valid` impl.
661unsafe impl<T: TryFromBytes + ?Sized> TryFromBytes for UnsafeCell<T> {
662    #[allow(clippy::missing_inline_in_public_items)]
663    fn only_derive_is_allowed_to_implement_this_trait()
664    where
665        Self: Sized,
666    {
667    }
668
669    #[inline]
670    fn is_bit_valid<A: invariant::Reference>(candidate: Maybe<'_, Self, A>) -> bool {
671        // The only way to implement this function is using an exclusive-aliased
672        // pointer. `UnsafeCell`s cannot be read via shared-aliased pointers
673        // (other than by using `unsafe` code, which we can't use since we can't
674        // guarantee how our users are accessing or modifying the `UnsafeCell`).
675        //
676        // `is_bit_valid` is documented as panicking or failing to monomorphize
677        // if called with a shared-aliased pointer on a type containing an
678        // `UnsafeCell`. In practice, it will always be a monorphization error.
679        // Since `is_bit_valid` is `#[doc(hidden)]` and only called directly
680        // from this crate, we only need to worry about our own code incorrectly
681        // calling `UnsafeCell::is_bit_valid`. The post-monomorphization error
682        // makes it easier to test that this is truly the case, and also means
683        // that if we make a mistake, it will cause downstream code to fail to
684        // compile, which will immediately surface the mistake and give us a
685        // chance to fix it quickly.
686        let c = candidate.into_exclusive_or_post_monomorphization_error();
687
688        // SAFETY: Since `UnsafeCell<T>` and `T` have the same layout and bit
689        // validity, `UnsafeCell<T>` is bit-valid exactly when its wrapped `T`
690        // is. Thus, this is a sound implementation of
691        // `UnsafeCell::is_bit_valid`.
692        T::is_bit_valid(c.get_mut())
693    }
694}
695
696safety_comment! {
697    /// SAFETY:
698    /// Per the reference [1]:
699    ///
700    ///   An array of `[T; N]` has a size of `size_of::<T>() * N` and the same
701    ///   alignment of `T`. Arrays are laid out so that the zero-based `nth`
702    ///   element of the array is offset from the start of the array by `n *
703    ///   size_of::<T>()` bytes.
704    ///
705    ///   ...
706    ///
707    ///   Slices have the same layout as the section of the array they slice.
708    ///
709    /// In other words, the layout of a `[T]` or `[T; N]` is a sequence of `T`s
710    /// laid out back-to-back with no bytes in between. Therefore, `[T]` or `[T;
711    /// N]` are `Immutable`, `TryFromBytes`, `FromZeros`, `FromBytes`, and
712    /// `IntoBytes` if `T` is (respectively). Furthermore, since an array/slice
713    /// has "the same alignment of `T`", `[T]` and `[T; N]` are `Unaligned` if
714    /// `T` is.
715    ///
716    /// Note that we don't `assert_unaligned!` for slice types because
717    /// `assert_unaligned!` uses `align_of`, which only works for `Sized` types.
718    ///
719    /// [1] https://doc.rust-lang.org/1.81.0/reference/type-layout.html#array-layout
720    unsafe_impl!(const N: usize, T: Immutable => Immutable for [T; N]);
721    unsafe_impl!(const N: usize, T: TryFromBytes => TryFromBytes for [T; N]; |c: Maybe<[T; N]>| {
722        // Note that this call may panic, but it would still be sound even if it
723        // did. `is_bit_valid` does not promise that it will not panic (in fact,
724        // it explicitly warns that it's a possibility), and we have not
725        // violated any safety invariants that we must fix before returning.
726        <[T] as TryFromBytes>::is_bit_valid(c.as_slice())
727    });
728    unsafe_impl!(const N: usize, T: FromZeros => FromZeros for [T; N]);
729    unsafe_impl!(const N: usize, T: FromBytes => FromBytes for [T; N]);
730    unsafe_impl!(const N: usize, T: IntoBytes => IntoBytes for [T; N]);
731    unsafe_impl!(const N: usize, T: Unaligned => Unaligned for [T; N]);
732    assert_unaligned!([(); 0], [(); 1], [u8; 0], [u8; 1]);
733    unsafe_impl!(T: Immutable => Immutable for [T]);
734    unsafe_impl!(T: TryFromBytes => TryFromBytes for [T]; |c: Maybe<[T]>| {
735        // SAFETY: Per the reference [1]:
736        //
737        //   An array of `[T; N]` has a size of `size_of::<T>() * N` and the
738        //   same alignment of `T`. Arrays are laid out so that the zero-based
739        //   `nth` element of the array is offset from the start of the array by
740        //   `n * size_of::<T>()` bytes.
741        //
742        //   ...
743        //
744        //   Slices have the same layout as the section of the array they slice.
745        //
746        // In other words, the layout of a `[T] is a sequence of `T`s laid out
747        // back-to-back with no bytes in between. If all elements in `candidate`
748        // are `is_bit_valid`, so too is `candidate`.
749        //
750        // Note that any of the below calls may panic, but it would still be
751        // sound even if it did. `is_bit_valid` does not promise that it will
752        // not panic (in fact, it explicitly warns that it's a possibility), and
753        // we have not violated any safety invariants that we must fix before
754        // returning.
755        c.iter().all(<T as TryFromBytes>::is_bit_valid)
756    });
757    unsafe_impl!(T: FromZeros => FromZeros for [T]);
758    unsafe_impl!(T: FromBytes => FromBytes for [T]);
759    unsafe_impl!(T: IntoBytes => IntoBytes for [T]);
760    unsafe_impl!(T: Unaligned => Unaligned for [T]);
761}
762safety_comment! {
763    /// SAFETY:
764    /// - `Immutable`: Raw pointers do not contain any `UnsafeCell`s.
765    /// - `FromZeros`: For thin pointers (note that `T: Sized`), the zero
766    ///   pointer is considered "null". [1] No operations which require
767    ///   provenance are legal on null pointers, so this is not a footgun.
768    /// - `TryFromBytes`: By the same reasoning as for `FromZeroes`, we can
769    ///   implement `TryFromBytes` for thin pointers provided that
770    ///   [`TryFromByte::is_bit_valid`] only produces `true` for zeroed bytes.
771    ///
772    /// NOTE(#170): Implementing `FromBytes` and `IntoBytes` for raw pointers
773    /// would be sound, but carries provenance footguns. We want to support
774    /// `FromBytes` and `IntoBytes` for raw pointers eventually, but we are
775    /// holding off until we can figure out how to address those footguns.
776    ///
777    /// [1] TODO(https://github.com/rust-lang/rust/pull/116988): Cite the
778    /// documentation once this PR lands.
779    unsafe_impl!(T: ?Sized => Immutable for *const T);
780    unsafe_impl!(T: ?Sized => Immutable for *mut T);
781    unsafe_impl!(T => TryFromBytes for *const T; |c: Maybe<*const T>| {
782        pointer::is_zeroed(c)
783    });
784    unsafe_impl!(T => FromZeros for *const T);
785    unsafe_impl!(T => TryFromBytes for *mut T; |c: Maybe<*const T>| {
786        pointer::is_zeroed(c)
787    });
788    unsafe_impl!(T => FromZeros for *mut T);
789}
790
791safety_comment! {
792    /// SAFETY:
793    /// `NonNull<T>` self-evidently does not contain `UnsafeCell`s. This is not
794    /// a proof, but we are accepting this as a known risk per #1358.
795    unsafe_impl!(T: ?Sized => Immutable for NonNull<T>);
796}
797
798safety_comment! {
799    /// SAFETY:
800    /// Reference types do not contain any `UnsafeCell`s.
801    unsafe_impl!(T: ?Sized => Immutable for &'_ T);
802    unsafe_impl!(T: ?Sized => Immutable for &'_ mut T);
803}
804
805safety_comment! {
806    /// SAFETY:
807    /// `Option` is not `#[non_exhaustive]` [1], which means that the types in
808    /// its variants cannot change, and no new variants can be added.
809    /// `Option<T>` does not contain any `UnsafeCell`s outside of `T`. [1]
810    ///
811    /// [1] https://doc.rust-lang.org/core/option/enum.Option.html
812    unsafe_impl!(T: Immutable => Immutable for Option<T>);
813}
814
815// SIMD support
816//
817// Per the Unsafe Code Guidelines Reference [1]:
818//
819//   Packed SIMD vector types are `repr(simd)` homogeneous tuple-structs
820//   containing `N` elements of type `T` where `N` is a power-of-two and the
821//   size and alignment requirements of `T` are equal:
822//
823//   ```rust
824//   #[repr(simd)]
825//   struct Vector<T, N>(T_0, ..., T_(N - 1));
826//   ```
827//
828//   ...
829//
830//   The size of `Vector` is `N * size_of::<T>()` and its alignment is an
831//   implementation-defined function of `T` and `N` greater than or equal to
832//   `align_of::<T>()`.
833//
834//   ...
835//
836//   Vector elements are laid out in source field order, enabling random access
837//   to vector elements by reinterpreting the vector as an array:
838//
839//   ```rust
840//   union U {
841//      vec: Vector<T, N>,
842//      arr: [T; N]
843//   }
844//
845//   assert_eq!(size_of::<Vector<T, N>>(), size_of::<[T; N]>());
846//   assert!(align_of::<Vector<T, N>>() >= align_of::<[T; N]>());
847//
848//   unsafe {
849//     let u = U { vec: Vector<T, N>(t_0, ..., t_(N - 1)) };
850//
851//     assert_eq!(u.vec.0, u.arr[0]);
852//     // ...
853//     assert_eq!(u.vec.(N - 1), u.arr[N - 1]);
854//   }
855//   ```
856//
857// Given this background, we can observe that:
858// - The size and bit pattern requirements of a SIMD type are equivalent to the
859//   equivalent array type. Thus, for any SIMD type whose primitive `T` is
860//   `Immutable`, `TryFromBytes`, `FromZeros`, `FromBytes`, or `IntoBytes`, that
861//   SIMD type is also `Immutable`, `TryFromBytes`, `FromZeros`, `FromBytes`, or
862//   `IntoBytes` respectively.
863// - Since no upper bound is placed on the alignment, no SIMD type can be
864//   guaranteed to be `Unaligned`.
865//
866// Also per [1]:
867//
868//   This chapter represents the consensus from issue #38. The statements in
869//   here are not (yet) "guaranteed" not to change until an RFC ratifies them.
870//
871// See issue #38 [2]. While this behavior is not technically guaranteed, the
872// likelihood that the behavior will change such that SIMD types are no longer
873// `TryFromBytes`, `FromZeros`, `FromBytes`, or `IntoBytes` is next to zero, as
874// that would defeat the entire purpose of SIMD types. Nonetheless, we put this
875// behavior behind the `simd` Cargo feature, which requires consumers to opt
876// into this stability hazard.
877//
878// [1] https://rust-lang.github.io/unsafe-code-guidelines/layout/packed-simd-vectors.html
879// [2] https://github.com/rust-lang/unsafe-code-guidelines/issues/38
880#[cfg(feature = "simd")]
881#[cfg_attr(doc_cfg, doc(cfg(feature = "simd")))]
882mod simd {
883    /// Defines a module which implements `TryFromBytes`, `FromZeros`,
884    /// `FromBytes`, and `IntoBytes` for a set of types from a module in
885    /// `core::arch`.
886    ///
887    /// `$arch` is both the name of the defined module and the name of the
888    /// module in `core::arch`, and `$typ` is the list of items from that module
889    /// to implement `FromZeros`, `FromBytes`, and `IntoBytes` for.
890    #[allow(unused_macros)] // `allow(unused_macros)` is needed because some
891                            // target/feature combinations don't emit any impls
892                            // and thus don't use this macro.
893    macro_rules! simd_arch_mod {
894        (#[cfg $cfg:tt] $arch:ident, $mod:ident, $($typ:ident),*) => {
895            #[cfg $cfg]
896            #[cfg_attr(doc_cfg, doc(cfg $cfg))]
897            mod $mod {
898                use core::arch::$arch::{$($typ),*};
899
900                use crate::*;
901                impl_known_layout!($($typ),*);
902                safety_comment! {
903                    /// SAFETY:
904                    /// See comment on module definition for justification.
905                    $( unsafe_impl!($typ: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes); )*
906                }
907            }
908        };
909    }
910
911    #[rustfmt::skip]
912    const _: () = {
913        simd_arch_mod!(
914            #[cfg(target_arch = "x86")]
915            x86, x86, __m128, __m128d, __m128i, __m256, __m256d, __m256i
916        );
917        simd_arch_mod!(
918            #[cfg(all(feature = "simd-nightly", target_arch = "x86"))]
919            x86, x86_nightly, __m512bh, __m512, __m512d, __m512i
920        );
921        simd_arch_mod!(
922            #[cfg(target_arch = "x86_64")]
923            x86_64, x86_64, __m128, __m128d, __m128i, __m256, __m256d, __m256i
924        );
925        simd_arch_mod!(
926            #[cfg(all(feature = "simd-nightly", target_arch = "x86_64"))]
927            x86_64, x86_64_nightly, __m512bh, __m512, __m512d, __m512i
928        );
929        simd_arch_mod!(
930            #[cfg(target_arch = "wasm32")]
931            wasm32, wasm32, v128
932        );
933        simd_arch_mod!(
934            #[cfg(all(feature = "simd-nightly", target_arch = "powerpc"))]
935            powerpc, powerpc, vector_bool_long, vector_double, vector_signed_long, vector_unsigned_long
936        );
937        simd_arch_mod!(
938            #[cfg(all(feature = "simd-nightly", target_arch = "powerpc64"))]
939            powerpc64, powerpc64, vector_bool_long, vector_double, vector_signed_long, vector_unsigned_long
940        );
941        #[cfg(zerocopy_aarch64_simd_1_59_0)]
942        #[cfg_attr(doc_cfg, doc(cfg(rust = "1.59.0")))]
943        simd_arch_mod!(
944            // NOTE(https://github.com/rust-lang/stdarch/issues/1484): NEON intrinsics are currently
945            // broken on big-endian platforms.
946            #[cfg(all(target_arch = "aarch64", target_endian = "little"))]
947            aarch64, aarch64, float32x2_t, float32x4_t, float64x1_t, float64x2_t, int8x8_t, int8x8x2_t,
948            int8x8x3_t, int8x8x4_t, int8x16_t, int8x16x2_t, int8x16x3_t, int8x16x4_t, int16x4_t,
949            int16x8_t, int32x2_t, int32x4_t, int64x1_t, int64x2_t, poly8x8_t, poly8x8x2_t, poly8x8x3_t,
950            poly8x8x4_t, poly8x16_t, poly8x16x2_t, poly8x16x3_t, poly8x16x4_t, poly16x4_t, poly16x8_t,
951            poly64x1_t, poly64x2_t, uint8x8_t, uint8x8x2_t, uint8x8x3_t, uint8x8x4_t, uint8x16_t,
952            uint8x16x2_t, uint8x16x3_t, uint8x16x4_t, uint16x4_t, uint16x8_t, uint32x2_t, uint32x4_t,
953            uint64x1_t, uint64x2_t
954        );
955        simd_arch_mod!(
956            #[cfg(all(feature = "simd-nightly", target_arch = "arm"))]
957            arm, arm, int8x4_t, uint8x4_t
958        );
959    };
960}
961
962#[cfg(test)]
963mod tests {
964    use super::*;
965    use crate::pointer::invariant;
966
967    #[test]
968    fn test_impls() {
969        // A type that can supply test cases for testing
970        // `TryFromBytes::is_bit_valid`. All types passed to `assert_impls!`
971        // must implement this trait; that macro uses it to generate runtime
972        // tests for `TryFromBytes` impls.
973        //
974        // All `T: FromBytes` types are provided with a blanket impl. Other
975        // types must implement `TryFromBytesTestable` directly (ie using
976        // `impl_try_from_bytes_testable!`).
977        trait TryFromBytesTestable {
978            fn with_passing_test_cases<F: Fn(Box<Self>)>(f: F);
979            fn with_failing_test_cases<F: Fn(&mut [u8])>(f: F);
980        }
981
982        impl<T: FromBytes> TryFromBytesTestable for T {
983            fn with_passing_test_cases<F: Fn(Box<Self>)>(f: F) {
984                // Test with a zeroed value.
985                f(Self::new_box_zeroed().unwrap());
986
987                let ffs = {
988                    let mut t = Self::new_zeroed();
989                    let ptr: *mut T = &mut t;
990                    // SAFETY: `T: FromBytes`
991                    unsafe { ptr::write_bytes(ptr.cast::<u8>(), 0xFF, mem::size_of::<T>()) };
992                    t
993                };
994
995                // Test with a value initialized with 0xFF.
996                f(Box::new(ffs));
997            }
998
999            fn with_failing_test_cases<F: Fn(&mut [u8])>(_f: F) {}
1000        }
1001
1002        macro_rules! impl_try_from_bytes_testable_for_null_pointer_optimization {
1003            ($($tys:ty),*) => {
1004                $(
1005                    impl TryFromBytesTestable for Option<$tys> {
1006                        fn with_passing_test_cases<F: Fn(Box<Self>)>(f: F) {
1007                            // Test with a zeroed value.
1008                            f(Box::new(None));
1009                        }
1010
1011                        fn with_failing_test_cases<F: Fn(&mut [u8])>(f: F) {
1012                            for pos in 0..mem::size_of::<Self>() {
1013                                let mut bytes = [0u8; mem::size_of::<Self>()];
1014                                bytes[pos] = 0x01;
1015                                f(&mut bytes[..]);
1016                            }
1017                        }
1018                    }
1019                )*
1020            };
1021        }
1022
1023        // Implements `TryFromBytesTestable`.
1024        macro_rules! impl_try_from_bytes_testable {
1025            // Base case for recursion (when the list of types has run out).
1026            (=> @success $($success_case:expr),* $(, @failure $($failure_case:expr),*)?) => {};
1027            // Implements for type(s) with no type parameters.
1028            ($ty:ty $(,$tys:ty)* => @success $($success_case:expr),* $(, @failure $($failure_case:expr),*)?) => {
1029                impl TryFromBytesTestable for $ty {
1030                    impl_try_from_bytes_testable!(
1031                        @methods     @success $($success_case),*
1032                                 $(, @failure $($failure_case),*)?
1033                    );
1034                }
1035                impl_try_from_bytes_testable!($($tys),* => @success $($success_case),* $(, @failure $($failure_case),*)?);
1036            };
1037            // Implements for multiple types with no type parameters.
1038            ($($($ty:ty),* => @success $($success_case:expr), * $(, @failure $($failure_case:expr),*)?;)*) => {
1039                $(
1040                    impl_try_from_bytes_testable!($($ty),* => @success $($success_case),* $(, @failure $($failure_case),*)*);
1041                )*
1042            };
1043            // Implements only the methods; caller must invoke this from inside
1044            // an impl block.
1045            (@methods @success $($success_case:expr),* $(, @failure $($failure_case:expr),*)?) => {
1046                fn with_passing_test_cases<F: Fn(Box<Self>)>(_f: F) {
1047                    $(
1048                        _f(Box::<Self>::from($success_case));
1049                    )*
1050                }
1051
1052                fn with_failing_test_cases<F: Fn(&mut [u8])>(_f: F) {
1053                    $($(
1054                        let mut case = $failure_case;
1055                        _f(case.as_mut_bytes());
1056                    )*)?
1057                }
1058            };
1059        }
1060
1061        impl_try_from_bytes_testable_for_null_pointer_optimization!(
1062            Box<UnsafeCell<NotZerocopy>>,
1063            &'static UnsafeCell<NotZerocopy>,
1064            &'static mut UnsafeCell<NotZerocopy>,
1065            NonNull<UnsafeCell<NotZerocopy>>,
1066            fn(),
1067            FnManyArgs,
1068            extern "C" fn(),
1069            ECFnManyArgs
1070        );
1071
1072        macro_rules! bx {
1073            ($e:expr) => {
1074                Box::new($e)
1075            };
1076        }
1077
1078        // Note that these impls are only for types which are not `FromBytes`.
1079        // `FromBytes` types are covered by a preceding blanket impl.
1080        impl_try_from_bytes_testable!(
1081            bool => @success true, false,
1082                    @failure 2u8, 3u8, 0xFFu8;
1083            char => @success '\u{0}', '\u{D7FF}', '\u{E000}', '\u{10FFFF}',
1084                    @failure 0xD800u32, 0xDFFFu32, 0x110000u32;
1085            str  => @success "", "hello", "❤️🧡💛💚💙💜",
1086                    @failure [0, 159, 146, 150];
1087            [u8] => @success vec![].into_boxed_slice(), vec![0, 1, 2].into_boxed_slice();
1088            NonZeroU8, NonZeroI8, NonZeroU16, NonZeroI16, NonZeroU32,
1089            NonZeroI32, NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128,
1090            NonZeroUsize, NonZeroIsize
1091                => @success Self::new(1).unwrap(),
1092                   // Doing this instead of `0` ensures that we always satisfy
1093                   // the size and alignment requirements of `Self` (whereas `0`
1094                   // may be any integer type with a different size or alignment
1095                   // than some `NonZeroXxx` types).
1096                   @failure Option::<Self>::None;
1097            [bool; 0] => @success [];
1098            [bool; 1]
1099                => @success [true], [false],
1100                   @failure [2u8], [3u8], [0xFFu8];
1101            [bool]
1102                => @success vec![true, false].into_boxed_slice(), vec![false, true].into_boxed_slice(),
1103                    @failure [2u8], [3u8], [0xFFu8], [0u8, 1u8, 2u8];
1104            Unalign<bool>
1105                => @success Unalign::new(false), Unalign::new(true),
1106                   @failure 2u8, 0xFFu8;
1107            ManuallyDrop<bool>
1108                => @success ManuallyDrop::new(false), ManuallyDrop::new(true),
1109                   @failure 2u8, 0xFFu8;
1110            ManuallyDrop<[u8]>
1111                => @success bx!(ManuallyDrop::new([])), bx!(ManuallyDrop::new([0u8])), bx!(ManuallyDrop::new([0u8, 1u8]));
1112            ManuallyDrop<[bool]>
1113                => @success bx!(ManuallyDrop::new([])), bx!(ManuallyDrop::new([false])), bx!(ManuallyDrop::new([false, true])),
1114                   @failure [2u8], [3u8], [0xFFu8], [0u8, 1u8, 2u8];
1115            ManuallyDrop<[UnsafeCell<u8>]>
1116                => @success bx!(ManuallyDrop::new([UnsafeCell::new(0)])), bx!(ManuallyDrop::new([UnsafeCell::new(0), UnsafeCell::new(1)]));
1117            ManuallyDrop<[UnsafeCell<bool>]>
1118                => @success bx!(ManuallyDrop::new([UnsafeCell::new(false)])), bx!(ManuallyDrop::new([UnsafeCell::new(false), UnsafeCell::new(true)])),
1119                @failure [2u8], [3u8], [0xFFu8], [0u8, 1u8, 2u8];
1120            Wrapping<bool>
1121                => @success Wrapping(false), Wrapping(true),
1122                    @failure 2u8, 0xFFu8;
1123            *const NotZerocopy
1124                => @success ptr::null::<NotZerocopy>(),
1125                   @failure [0x01; mem::size_of::<*const NotZerocopy>()];
1126            *mut NotZerocopy
1127                => @success ptr::null_mut::<NotZerocopy>(),
1128                   @failure [0x01; mem::size_of::<*mut NotZerocopy>()];
1129        );
1130
1131        // Use the trick described in [1] to allow us to call methods
1132        // conditional on certain trait bounds.
1133        //
1134        // In all of these cases, methods return `Option<R>`, where `R` is the
1135        // return type of the method we're conditionally calling. The "real"
1136        // implementations (the ones defined in traits using `&self`) return
1137        // `Some`, and the default implementations (the ones defined as inherent
1138        // methods using `&mut self`) return `None`.
1139        //
1140        // [1] https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md
1141        mod autoref_trick {
1142            use super::*;
1143
1144            pub(super) struct AutorefWrapper<T: ?Sized>(pub(super) PhantomData<T>);
1145
1146            pub(super) trait TestIsBitValidShared<T: ?Sized> {
1147                #[allow(clippy::needless_lifetimes)]
1148                fn test_is_bit_valid_shared<'ptr, A: invariant::Reference>(
1149                    &self,
1150                    candidate: Maybe<'ptr, T, A>,
1151                ) -> Option<bool>;
1152            }
1153
1154            impl<T: TryFromBytes + Immutable + ?Sized> TestIsBitValidShared<T> for AutorefWrapper<T> {
1155                #[allow(clippy::needless_lifetimes)]
1156                fn test_is_bit_valid_shared<'ptr, A: invariant::Reference>(
1157                    &self,
1158                    candidate: Maybe<'ptr, T, A>,
1159                ) -> Option<bool> {
1160                    Some(T::is_bit_valid(candidate))
1161                }
1162            }
1163
1164            pub(super) trait TestTryFromRef<T: ?Sized> {
1165                #[allow(clippy::needless_lifetimes)]
1166                fn test_try_from_ref<'bytes>(
1167                    &self,
1168                    bytes: &'bytes [u8],
1169                ) -> Option<Option<&'bytes T>>;
1170            }
1171
1172            impl<T: TryFromBytes + Immutable + KnownLayout + ?Sized> TestTryFromRef<T> for AutorefWrapper<T> {
1173                #[allow(clippy::needless_lifetimes)]
1174                fn test_try_from_ref<'bytes>(
1175                    &self,
1176                    bytes: &'bytes [u8],
1177                ) -> Option<Option<&'bytes T>> {
1178                    Some(T::try_ref_from_bytes(bytes).ok())
1179                }
1180            }
1181
1182            pub(super) trait TestTryFromMut<T: ?Sized> {
1183                #[allow(clippy::needless_lifetimes)]
1184                fn test_try_from_mut<'bytes>(
1185                    &self,
1186                    bytes: &'bytes mut [u8],
1187                ) -> Option<Option<&'bytes mut T>>;
1188            }
1189
1190            impl<T: TryFromBytes + IntoBytes + KnownLayout + ?Sized> TestTryFromMut<T> for AutorefWrapper<T> {
1191                #[allow(clippy::needless_lifetimes)]
1192                fn test_try_from_mut<'bytes>(
1193                    &self,
1194                    bytes: &'bytes mut [u8],
1195                ) -> Option<Option<&'bytes mut T>> {
1196                    Some(T::try_mut_from_bytes(bytes).ok())
1197                }
1198            }
1199
1200            pub(super) trait TestTryReadFrom<T> {
1201                fn test_try_read_from(&self, bytes: &[u8]) -> Option<Option<T>>;
1202            }
1203
1204            impl<T: TryFromBytes> TestTryReadFrom<T> for AutorefWrapper<T> {
1205                fn test_try_read_from(&self, bytes: &[u8]) -> Option<Option<T>> {
1206                    Some(T::try_read_from_bytes(bytes).ok())
1207                }
1208            }
1209
1210            pub(super) trait TestAsBytes<T: ?Sized> {
1211                #[allow(clippy::needless_lifetimes)]
1212                fn test_as_bytes<'slf, 't>(&'slf self, t: &'t T) -> Option<&'t [u8]>;
1213            }
1214
1215            impl<T: IntoBytes + Immutable + ?Sized> TestAsBytes<T> for AutorefWrapper<T> {
1216                #[allow(clippy::needless_lifetimes)]
1217                fn test_as_bytes<'slf, 't>(&'slf self, t: &'t T) -> Option<&'t [u8]> {
1218                    Some(t.as_bytes())
1219                }
1220            }
1221        }
1222
1223        use autoref_trick::*;
1224
1225        // Asserts that `$ty` is one of a list of types which are allowed to not
1226        // provide a "real" implementation for `$fn_name`. Since the
1227        // `autoref_trick` machinery fails silently, this allows us to ensure
1228        // that the "default" impls are only being used for types which we
1229        // expect.
1230        //
1231        // Note that, since this is a runtime test, it is possible to have an
1232        // allowlist which is too restrictive if the function in question is
1233        // never called for a particular type. For example, if `as_bytes` is not
1234        // supported for a particular type, and so `test_as_bytes` returns
1235        // `None`, methods such as `test_try_from_ref` may never be called for
1236        // that type. As a result, it's possible that, for example, adding
1237        // `as_bytes` support for a type would cause other allowlist assertions
1238        // to fail. This means that allowlist assertion failures should not
1239        // automatically be taken as a sign of a bug.
1240        macro_rules! assert_on_allowlist {
1241            ($fn_name:ident($ty:ty) $(: $($tys:ty),*)?) => {{
1242                use core::any::TypeId;
1243
1244                let allowlist: &[TypeId] = &[ $($(TypeId::of::<$tys>()),*)? ];
1245                let allowlist_names: &[&str] = &[ $($(stringify!($tys)),*)? ];
1246
1247                let id = TypeId::of::<$ty>();
1248                assert!(allowlist.contains(&id), "{} is not on allowlist for {}: {:?}", stringify!($ty), stringify!($fn_name), allowlist_names);
1249            }};
1250        }
1251
1252        // Asserts that `$ty` implements any `$trait` and doesn't implement any
1253        // `!$trait`. Note that all `$trait`s must come before any `!$trait`s.
1254        //
1255        // For `T: TryFromBytes`, uses `TryFromBytesTestable` to test success
1256        // and failure cases.
1257        macro_rules! assert_impls {
1258            ($ty:ty: TryFromBytes) => {
1259                // "Default" implementations that match the "real"
1260                // implementations defined in the `autoref_trick` module above.
1261                #[allow(unused, non_local_definitions)]
1262                impl AutorefWrapper<$ty> {
1263                    #[allow(clippy::needless_lifetimes)]
1264                    fn test_is_bit_valid_shared<'ptr, A: invariant::Reference>(
1265                        &mut self,
1266                        candidate: Maybe<'ptr, $ty, A>,
1267                    ) -> Option<bool> {
1268                        assert_on_allowlist!(
1269                            test_is_bit_valid_shared($ty):
1270                            ManuallyDrop<UnsafeCell<()>>,
1271                            ManuallyDrop<[UnsafeCell<u8>]>,
1272                            ManuallyDrop<[UnsafeCell<bool>]>,
1273                            CoreMaybeUninit<NotZerocopy>,
1274                            CoreMaybeUninit<UnsafeCell<()>>,
1275                            Wrapping<UnsafeCell<()>>
1276                        );
1277
1278                        None
1279                    }
1280
1281                    #[allow(clippy::needless_lifetimes)]
1282                    fn test_try_from_ref<'bytes>(&mut self, _bytes: &'bytes [u8]) -> Option<Option<&'bytes $ty>> {
1283                        assert_on_allowlist!(
1284                            test_try_from_ref($ty):
1285                            ManuallyDrop<[UnsafeCell<bool>]>
1286                        );
1287
1288                        None
1289                    }
1290
1291                    #[allow(clippy::needless_lifetimes)]
1292                    fn test_try_from_mut<'bytes>(&mut self, _bytes: &'bytes mut [u8]) -> Option<Option<&'bytes mut $ty>> {
1293                        assert_on_allowlist!(
1294                            test_try_from_mut($ty):
1295                            Option<Box<UnsafeCell<NotZerocopy>>>,
1296                            Option<&'static UnsafeCell<NotZerocopy>>,
1297                            Option<&'static mut UnsafeCell<NotZerocopy>>,
1298                            Option<NonNull<UnsafeCell<NotZerocopy>>>,
1299                            Option<fn()>,
1300                            Option<FnManyArgs>,
1301                            Option<extern "C" fn()>,
1302                            Option<ECFnManyArgs>,
1303                            *const NotZerocopy,
1304                            *mut NotZerocopy
1305                        );
1306
1307                        None
1308                    }
1309
1310                    fn test_try_read_from(&mut self, _bytes: &[u8]) -> Option<Option<&$ty>> {
1311                        assert_on_allowlist!(
1312                            test_try_read_from($ty):
1313                            str,
1314                            ManuallyDrop<[u8]>,
1315                            ManuallyDrop<[bool]>,
1316                            ManuallyDrop<[UnsafeCell<bool>]>,
1317                            [u8],
1318                            [bool]
1319                        );
1320
1321                        None
1322                    }
1323
1324                    fn test_as_bytes(&mut self, _t: &$ty) -> Option<&[u8]> {
1325                        assert_on_allowlist!(
1326                            test_as_bytes($ty):
1327                            Option<&'static UnsafeCell<NotZerocopy>>,
1328                            Option<&'static mut UnsafeCell<NotZerocopy>>,
1329                            Option<NonNull<UnsafeCell<NotZerocopy>>>,
1330                            Option<Box<UnsafeCell<NotZerocopy>>>,
1331                            Option<fn()>,
1332                            Option<FnManyArgs>,
1333                            Option<extern "C" fn()>,
1334                            Option<ECFnManyArgs>,
1335                            CoreMaybeUninit<u8>,
1336                            CoreMaybeUninit<NotZerocopy>,
1337                            CoreMaybeUninit<UnsafeCell<()>>,
1338                            ManuallyDrop<UnsafeCell<()>>,
1339                            ManuallyDrop<[UnsafeCell<u8>]>,
1340                            ManuallyDrop<[UnsafeCell<bool>]>,
1341                            Wrapping<UnsafeCell<()>>,
1342                            *const NotZerocopy,
1343                            *mut NotZerocopy
1344                        );
1345
1346                        None
1347                    }
1348                }
1349
1350                <$ty as TryFromBytesTestable>::with_passing_test_cases(|mut val| {
1351                    // TODO(#494): These tests only get exercised for types
1352                    // which are `IntoBytes`. Once we implement #494, we should
1353                    // be able to support non-`IntoBytes` types by zeroing
1354                    // padding.
1355
1356                    // We define `w` and `ww` since, in the case of the inherent
1357                    // methods, Rust thinks they're both borrowed mutably at the
1358                    // same time (given how we use them below). If we just
1359                    // defined a single `w` and used it for multiple operations,
1360                    // this would conflict.
1361                    //
1362                    // We `#[allow(unused_mut]` for the cases where the "real"
1363                    // impls are used, which take `&self`.
1364                    #[allow(unused_mut)]
1365                    let (mut w, mut ww) = (AutorefWrapper::<$ty>(PhantomData), AutorefWrapper::<$ty>(PhantomData));
1366
1367                    let c = Ptr::from_ref(&*val);
1368                    let c = c.forget_aligned();
1369                    // SAFETY: TODO(#899): This is unsound. `$ty` is not
1370                    // necessarily `IntoBytes`, but that's the corner we've
1371                    // backed ourselves into by using `Ptr::from_ref`.
1372                    let c = unsafe { c.assume_initialized() };
1373                    let res = w.test_is_bit_valid_shared(c);
1374                    if let Some(res) = res {
1375                        assert!(res, "{}::is_bit_valid({:?}) (shared `Ptr`): got false, expected true", stringify!($ty), val);
1376                    }
1377
1378                    let c = Ptr::from_mut(&mut *val);
1379                    let c = c.forget_aligned();
1380                    // SAFETY: TODO(#899): This is unsound. `$ty` is not
1381                    // necessarily `IntoBytes`, but that's the corner we've
1382                    // backed ourselves into by using `Ptr::from_ref`.
1383                    let c = unsafe { c.assume_initialized() };
1384                    let res = <$ty as TryFromBytes>::is_bit_valid(c);
1385                    assert!(res, "{}::is_bit_valid({:?}) (exclusive `Ptr`): got false, expected true", stringify!($ty), val);
1386
1387                    // `bytes` is `Some(val.as_bytes())` if `$ty: IntoBytes +
1388                    // Immutable` and `None` otherwise.
1389                    let bytes = w.test_as_bytes(&*val);
1390
1391                    // The inner closure returns
1392                    // `Some($ty::try_ref_from_bytes(bytes))` if `$ty:
1393                    // Immutable` and `None` otherwise.
1394                    let res = bytes.and_then(|bytes| ww.test_try_from_ref(bytes));
1395                    if let Some(res) = res {
1396                        assert!(res.is_some(), "{}::try_ref_from_bytes({:?}): got `None`, expected `Some`", stringify!($ty), val);
1397                    }
1398
1399                    if let Some(bytes) = bytes {
1400                        // We need to get a mutable byte slice, and so we clone
1401                        // into a `Vec`. However, we also need these bytes to
1402                        // satisfy `$ty`'s alignment requirement, which isn't
1403                        // guaranteed for `Vec<u8>`. In order to get around
1404                        // this, we create a `Vec` which is twice as long as we
1405                        // need. There is guaranteed to be an aligned byte range
1406                        // of size `size_of_val(val)` within that range.
1407                        let val = &*val;
1408                        let size = mem::size_of_val(val);
1409                        let align = mem::align_of_val(val);
1410
1411                        let mut vec = bytes.to_vec();
1412                        vec.extend(bytes);
1413                        let slc = vec.as_slice();
1414                        let offset = slc.as_ptr().align_offset(align);
1415                        let bytes_mut = &mut vec.as_mut_slice()[offset..offset+size];
1416                        bytes_mut.copy_from_slice(bytes);
1417
1418                        let res = ww.test_try_from_mut(bytes_mut);
1419                        if let Some(res) = res {
1420                            assert!(res.is_some(), "{}::try_mut_from_bytes({:?}): got `None`, expected `Some`", stringify!($ty), val);
1421                        }
1422                    }
1423
1424                    let res = bytes.and_then(|bytes| ww.test_try_read_from(bytes));
1425                    if let Some(res) = res {
1426                        assert!(res.is_some(), "{}::try_read_from_bytes({:?}): got `None`, expected `Some`", stringify!($ty), val);
1427                    }
1428                });
1429                #[allow(clippy::as_conversions)]
1430                <$ty as TryFromBytesTestable>::with_failing_test_cases(|c| {
1431                    #[allow(unused_mut)] // For cases where the "real" impls are used, which take `&self`.
1432                    let mut w = AutorefWrapper::<$ty>(PhantomData);
1433
1434                    // This is `Some($ty::try_ref_from_bytes(c))` if `$ty:
1435                    // Immutable` and `None` otherwise.
1436                    let res = w.test_try_from_ref(c);
1437                    if let Some(res) = res {
1438                        assert!(res.is_none(), "{}::try_ref_from_bytes({:?}): got Some, expected None", stringify!($ty), c);
1439                    }
1440
1441                    let res = w.test_try_from_mut(c);
1442                    if let Some(res) = res {
1443                        assert!(res.is_none(), "{}::try_mut_from_bytes({:?}): got Some, expected None", stringify!($ty), c);
1444                    }
1445
1446
1447                    let res = w.test_try_read_from(c);
1448                    if let Some(res) = res {
1449                        assert!(res.is_none(), "{}::try_read_from_bytes({:?}): got Some, expected None", stringify!($ty), c);
1450                    }
1451                });
1452
1453                #[allow(dead_code)]
1454                const _: () = { static_assertions::assert_impl_all!($ty: TryFromBytes); };
1455            };
1456            ($ty:ty: $trait:ident) => {
1457                #[allow(dead_code)]
1458                const _: () = { static_assertions::assert_impl_all!($ty: $trait); };
1459            };
1460            ($ty:ty: !$trait:ident) => {
1461                #[allow(dead_code)]
1462                const _: () = { static_assertions::assert_not_impl_any!($ty: $trait); };
1463            };
1464            ($ty:ty: $($trait:ident),* $(,)? $(!$negative_trait:ident),*) => {
1465                $(
1466                    assert_impls!($ty: $trait);
1467                )*
1468
1469                $(
1470                    assert_impls!($ty: !$negative_trait);
1471                )*
1472            };
1473        }
1474
1475        // NOTE: The negative impl assertions here are not necessarily
1476        // prescriptive. They merely serve as change detectors to make sure
1477        // we're aware of what trait impls are getting added with a given
1478        // change. Of course, some impls would be invalid (e.g., `bool:
1479        // FromBytes`), and so this change detection is very important.
1480
1481        assert_impls!(
1482            (): KnownLayout,
1483            Immutable,
1484            TryFromBytes,
1485            FromZeros,
1486            FromBytes,
1487            IntoBytes,
1488            Unaligned
1489        );
1490        assert_impls!(
1491            u8: KnownLayout,
1492            Immutable,
1493            TryFromBytes,
1494            FromZeros,
1495            FromBytes,
1496            IntoBytes,
1497            Unaligned
1498        );
1499        assert_impls!(
1500            i8: KnownLayout,
1501            Immutable,
1502            TryFromBytes,
1503            FromZeros,
1504            FromBytes,
1505            IntoBytes,
1506            Unaligned
1507        );
1508        assert_impls!(
1509            u16: KnownLayout,
1510            Immutable,
1511            TryFromBytes,
1512            FromZeros,
1513            FromBytes,
1514            IntoBytes,
1515            !Unaligned
1516        );
1517        assert_impls!(
1518            i16: KnownLayout,
1519            Immutable,
1520            TryFromBytes,
1521            FromZeros,
1522            FromBytes,
1523            IntoBytes,
1524            !Unaligned
1525        );
1526        assert_impls!(
1527            u32: KnownLayout,
1528            Immutable,
1529            TryFromBytes,
1530            FromZeros,
1531            FromBytes,
1532            IntoBytes,
1533            !Unaligned
1534        );
1535        assert_impls!(
1536            i32: KnownLayout,
1537            Immutable,
1538            TryFromBytes,
1539            FromZeros,
1540            FromBytes,
1541            IntoBytes,
1542            !Unaligned
1543        );
1544        assert_impls!(
1545            u64: KnownLayout,
1546            Immutable,
1547            TryFromBytes,
1548            FromZeros,
1549            FromBytes,
1550            IntoBytes,
1551            !Unaligned
1552        );
1553        assert_impls!(
1554            i64: KnownLayout,
1555            Immutable,
1556            TryFromBytes,
1557            FromZeros,
1558            FromBytes,
1559            IntoBytes,
1560            !Unaligned
1561        );
1562        assert_impls!(
1563            u128: KnownLayout,
1564            Immutable,
1565            TryFromBytes,
1566            FromZeros,
1567            FromBytes,
1568            IntoBytes,
1569            !Unaligned
1570        );
1571        assert_impls!(
1572            i128: KnownLayout,
1573            Immutable,
1574            TryFromBytes,
1575            FromZeros,
1576            FromBytes,
1577            IntoBytes,
1578            !Unaligned
1579        );
1580        assert_impls!(
1581            usize: KnownLayout,
1582            Immutable,
1583            TryFromBytes,
1584            FromZeros,
1585            FromBytes,
1586            IntoBytes,
1587            !Unaligned
1588        );
1589        assert_impls!(
1590            isize: KnownLayout,
1591            Immutable,
1592            TryFromBytes,
1593            FromZeros,
1594            FromBytes,
1595            IntoBytes,
1596            !Unaligned
1597        );
1598        #[cfg(feature = "float-nightly")]
1599        assert_impls!(
1600            f16: KnownLayout,
1601            Immutable,
1602            TryFromBytes,
1603            FromZeros,
1604            FromBytes,
1605            IntoBytes,
1606            !Unaligned
1607        );
1608        assert_impls!(
1609            f32: KnownLayout,
1610            Immutable,
1611            TryFromBytes,
1612            FromZeros,
1613            FromBytes,
1614            IntoBytes,
1615            !Unaligned
1616        );
1617        assert_impls!(
1618            f64: KnownLayout,
1619            Immutable,
1620            TryFromBytes,
1621            FromZeros,
1622            FromBytes,
1623            IntoBytes,
1624            !Unaligned
1625        );
1626        #[cfg(feature = "float-nightly")]
1627        assert_impls!(
1628            f128: KnownLayout,
1629            Immutable,
1630            TryFromBytes,
1631            FromZeros,
1632            FromBytes,
1633            IntoBytes,
1634            !Unaligned
1635        );
1636        assert_impls!(
1637            bool: KnownLayout,
1638            Immutable,
1639            TryFromBytes,
1640            FromZeros,
1641            IntoBytes,
1642            Unaligned,
1643            !FromBytes
1644        );
1645        assert_impls!(
1646            char: KnownLayout,
1647            Immutable,
1648            TryFromBytes,
1649            FromZeros,
1650            IntoBytes,
1651            !FromBytes,
1652            !Unaligned
1653        );
1654        assert_impls!(
1655            str: KnownLayout,
1656            Immutable,
1657            TryFromBytes,
1658            FromZeros,
1659            IntoBytes,
1660            Unaligned,
1661            !FromBytes
1662        );
1663
1664        assert_impls!(
1665            NonZeroU8: KnownLayout,
1666            Immutable,
1667            TryFromBytes,
1668            IntoBytes,
1669            Unaligned,
1670            !FromZeros,
1671            !FromBytes
1672        );
1673        assert_impls!(
1674            NonZeroI8: KnownLayout,
1675            Immutable,
1676            TryFromBytes,
1677            IntoBytes,
1678            Unaligned,
1679            !FromZeros,
1680            !FromBytes
1681        );
1682        assert_impls!(
1683            NonZeroU16: KnownLayout,
1684            Immutable,
1685            TryFromBytes,
1686            IntoBytes,
1687            !FromBytes,
1688            !Unaligned
1689        );
1690        assert_impls!(
1691            NonZeroI16: KnownLayout,
1692            Immutable,
1693            TryFromBytes,
1694            IntoBytes,
1695            !FromBytes,
1696            !Unaligned
1697        );
1698        assert_impls!(
1699            NonZeroU32: KnownLayout,
1700            Immutable,
1701            TryFromBytes,
1702            IntoBytes,
1703            !FromBytes,
1704            !Unaligned
1705        );
1706        assert_impls!(
1707            NonZeroI32: KnownLayout,
1708            Immutable,
1709            TryFromBytes,
1710            IntoBytes,
1711            !FromBytes,
1712            !Unaligned
1713        );
1714        assert_impls!(
1715            NonZeroU64: KnownLayout,
1716            Immutable,
1717            TryFromBytes,
1718            IntoBytes,
1719            !FromBytes,
1720            !Unaligned
1721        );
1722        assert_impls!(
1723            NonZeroI64: KnownLayout,
1724            Immutable,
1725            TryFromBytes,
1726            IntoBytes,
1727            !FromBytes,
1728            !Unaligned
1729        );
1730        assert_impls!(
1731            NonZeroU128: KnownLayout,
1732            Immutable,
1733            TryFromBytes,
1734            IntoBytes,
1735            !FromBytes,
1736            !Unaligned
1737        );
1738        assert_impls!(
1739            NonZeroI128: KnownLayout,
1740            Immutable,
1741            TryFromBytes,
1742            IntoBytes,
1743            !FromBytes,
1744            !Unaligned
1745        );
1746        assert_impls!(
1747            NonZeroUsize: KnownLayout,
1748            Immutable,
1749            TryFromBytes,
1750            IntoBytes,
1751            !FromBytes,
1752            !Unaligned
1753        );
1754        assert_impls!(
1755            NonZeroIsize: KnownLayout,
1756            Immutable,
1757            TryFromBytes,
1758            IntoBytes,
1759            !FromBytes,
1760            !Unaligned
1761        );
1762
1763        assert_impls!(Option<NonZeroU8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
1764        assert_impls!(Option<NonZeroI8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
1765        assert_impls!(Option<NonZeroU16>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
1766        assert_impls!(Option<NonZeroI16>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
1767        assert_impls!(Option<NonZeroU32>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
1768        assert_impls!(Option<NonZeroI32>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
1769        assert_impls!(Option<NonZeroU64>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
1770        assert_impls!(Option<NonZeroI64>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
1771        assert_impls!(Option<NonZeroU128>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
1772        assert_impls!(Option<NonZeroI128>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
1773        assert_impls!(Option<NonZeroUsize>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
1774        assert_impls!(Option<NonZeroIsize>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned);
1775
1776        // Implements none of the ZC traits.
1777        struct NotZerocopy;
1778
1779        #[rustfmt::skip]
1780        type FnManyArgs = fn(
1781            NotZerocopy, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8,
1782        ) -> (NotZerocopy, NotZerocopy);
1783
1784        // Allowed, because we're not actually using this type for FFI.
1785        #[allow(improper_ctypes_definitions)]
1786        #[rustfmt::skip]
1787        type ECFnManyArgs = extern "C" fn(
1788            NotZerocopy, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8,
1789        ) -> (NotZerocopy, NotZerocopy);
1790
1791        #[cfg(feature = "alloc")]
1792        assert_impls!(Option<Box<UnsafeCell<NotZerocopy>>>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1793        assert_impls!(Option<Box<[UnsafeCell<NotZerocopy>]>>: KnownLayout, !Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1794        assert_impls!(Option<&'static UnsafeCell<NotZerocopy>>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1795        assert_impls!(Option<&'static [UnsafeCell<NotZerocopy>]>: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1796        assert_impls!(Option<&'static mut UnsafeCell<NotZerocopy>>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1797        assert_impls!(Option<&'static mut [UnsafeCell<NotZerocopy>]>: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1798        assert_impls!(Option<NonNull<UnsafeCell<NotZerocopy>>>: KnownLayout, TryFromBytes, FromZeros, Immutable, !FromBytes, !IntoBytes, !Unaligned);
1799        assert_impls!(Option<NonNull<[UnsafeCell<NotZerocopy>]>>: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1800        assert_impls!(Option<fn()>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1801        assert_impls!(Option<FnManyArgs>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1802        assert_impls!(Option<extern "C" fn()>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1803        assert_impls!(Option<ECFnManyArgs>: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1804
1805        assert_impls!(PhantomData<NotZerocopy>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
1806        assert_impls!(PhantomData<UnsafeCell<()>>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
1807        assert_impls!(PhantomData<[u8]>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
1808
1809        assert_impls!(ManuallyDrop<u8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
1810        // This test is important because it allows us to test our hand-rolled
1811        // implementation of `<ManuallyDrop<T> as TryFromBytes>::is_bit_valid`.
1812        assert_impls!(ManuallyDrop<bool>: KnownLayout, Immutable, TryFromBytes, FromZeros, IntoBytes, Unaligned, !FromBytes);
1813        assert_impls!(ManuallyDrop<[u8]>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
1814        // This test is important because it allows us to test our hand-rolled
1815        // implementation of `<ManuallyDrop<T> as TryFromBytes>::is_bit_valid`.
1816        assert_impls!(ManuallyDrop<[bool]>: KnownLayout, Immutable, TryFromBytes, FromZeros, IntoBytes, Unaligned, !FromBytes);
1817        assert_impls!(ManuallyDrop<NotZerocopy>: !Immutable, !TryFromBytes, !KnownLayout, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1818        assert_impls!(ManuallyDrop<[NotZerocopy]>: KnownLayout, !Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1819        assert_impls!(ManuallyDrop<UnsafeCell<()>>: KnownLayout, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned, !Immutable);
1820        assert_impls!(ManuallyDrop<[UnsafeCell<u8>]>: KnownLayout, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned, !Immutable);
1821        assert_impls!(ManuallyDrop<[UnsafeCell<bool>]>: KnownLayout, TryFromBytes, FromZeros, IntoBytes, Unaligned, !Immutable, !FromBytes);
1822
1823        assert_impls!(CoreMaybeUninit<u8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, Unaligned, !IntoBytes);
1824        assert_impls!(CoreMaybeUninit<NotZerocopy>: KnownLayout, TryFromBytes, FromZeros, FromBytes, !Immutable, !IntoBytes, !Unaligned);
1825        assert_impls!(CoreMaybeUninit<UnsafeCell<()>>: KnownLayout, TryFromBytes, FromZeros, FromBytes, Unaligned, !Immutable, !IntoBytes);
1826
1827        assert_impls!(Wrapping<u8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
1828        // This test is important because it allows us to test our hand-rolled
1829        // implementation of `<Wrapping<T> as TryFromBytes>::is_bit_valid`.
1830        assert_impls!(Wrapping<bool>: KnownLayout, Immutable, TryFromBytes, FromZeros, IntoBytes, Unaligned, !FromBytes);
1831        assert_impls!(Wrapping<NotZerocopy>: KnownLayout, !Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1832        assert_impls!(Wrapping<UnsafeCell<()>>: KnownLayout, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned, !Immutable);
1833
1834        assert_impls!(Unalign<u8>: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, Unaligned);
1835        // This test is important because it allows us to test our hand-rolled
1836        // implementation of `<Unalign<T> as TryFromBytes>::is_bit_valid`.
1837        assert_impls!(Unalign<bool>: KnownLayout, Immutable, TryFromBytes, FromZeros, IntoBytes, Unaligned, !FromBytes);
1838        assert_impls!(Unalign<NotZerocopy>: KnownLayout, Unaligned, !Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes);
1839
1840        assert_impls!(
1841            [u8]: KnownLayout,
1842            Immutable,
1843            TryFromBytes,
1844            FromZeros,
1845            FromBytes,
1846            IntoBytes,
1847            Unaligned
1848        );
1849        assert_impls!(
1850            [bool]: KnownLayout,
1851            Immutable,
1852            TryFromBytes,
1853            FromZeros,
1854            IntoBytes,
1855            Unaligned,
1856            !FromBytes
1857        );
1858        assert_impls!([NotZerocopy]: KnownLayout, !Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1859        assert_impls!(
1860            [u8; 0]: KnownLayout,
1861            Immutable,
1862            TryFromBytes,
1863            FromZeros,
1864            FromBytes,
1865            IntoBytes,
1866            Unaligned,
1867        );
1868        assert_impls!(
1869            [NotZerocopy; 0]: KnownLayout,
1870            !Immutable,
1871            !TryFromBytes,
1872            !FromZeros,
1873            !FromBytes,
1874            !IntoBytes,
1875            !Unaligned
1876        );
1877        assert_impls!(
1878            [u8; 1]: KnownLayout,
1879            Immutable,
1880            TryFromBytes,
1881            FromZeros,
1882            FromBytes,
1883            IntoBytes,
1884            Unaligned,
1885        );
1886        assert_impls!(
1887            [NotZerocopy; 1]: KnownLayout,
1888            !Immutable,
1889            !TryFromBytes,
1890            !FromZeros,
1891            !FromBytes,
1892            !IntoBytes,
1893            !Unaligned
1894        );
1895
1896        assert_impls!(*const NotZerocopy: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1897        assert_impls!(*mut NotZerocopy: KnownLayout, Immutable, TryFromBytes, FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1898        assert_impls!(*const [NotZerocopy]: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1899        assert_impls!(*mut [NotZerocopy]: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1900        assert_impls!(*const dyn Debug: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1901        assert_impls!(*mut dyn Debug: KnownLayout, Immutable, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
1902
1903        #[cfg(feature = "simd")]
1904        {
1905            #[allow(unused_macros)]
1906            macro_rules! test_simd_arch_mod {
1907                ($arch:ident, $($typ:ident),*) => {
1908                    {
1909                        use core::arch::$arch::{$($typ),*};
1910                        use crate::*;
1911                        $( assert_impls!($typ: KnownLayout, Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes, !Unaligned); )*
1912                    }
1913                };
1914            }
1915            #[cfg(target_arch = "x86")]
1916            test_simd_arch_mod!(x86, __m128, __m128d, __m128i, __m256, __m256d, __m256i);
1917
1918            #[cfg(all(feature = "simd-nightly", target_arch = "x86"))]
1919            test_simd_arch_mod!(x86, __m512bh, __m512, __m512d, __m512i);
1920
1921            #[cfg(target_arch = "x86_64")]
1922            test_simd_arch_mod!(x86_64, __m128, __m128d, __m128i, __m256, __m256d, __m256i);
1923
1924            #[cfg(all(feature = "simd-nightly", target_arch = "x86_64"))]
1925            test_simd_arch_mod!(x86_64, __m512bh, __m512, __m512d, __m512i);
1926
1927            #[cfg(target_arch = "wasm32")]
1928            test_simd_arch_mod!(wasm32, v128);
1929
1930            #[cfg(all(feature = "simd-nightly", target_arch = "powerpc"))]
1931            test_simd_arch_mod!(
1932                powerpc,
1933                vector_bool_long,
1934                vector_double,
1935                vector_signed_long,
1936                vector_unsigned_long
1937            );
1938
1939            #[cfg(all(feature = "simd-nightly", target_arch = "powerpc64"))]
1940            test_simd_arch_mod!(
1941                powerpc64,
1942                vector_bool_long,
1943                vector_double,
1944                vector_signed_long,
1945                vector_unsigned_long
1946            );
1947            #[cfg(all(target_arch = "aarch64", zerocopy_aarch64_simd_1_59_0))]
1948            #[rustfmt::skip]
1949            test_simd_arch_mod!(
1950                aarch64, float32x2_t, float32x4_t, float64x1_t, float64x2_t, int8x8_t, int8x8x2_t,
1951                int8x8x3_t, int8x8x4_t, int8x16_t, int8x16x2_t, int8x16x3_t, int8x16x4_t, int16x4_t,
1952                int16x8_t, int32x2_t, int32x4_t, int64x1_t, int64x2_t, poly8x8_t, poly8x8x2_t, poly8x8x3_t,
1953                poly8x8x4_t, poly8x16_t, poly8x16x2_t, poly8x16x3_t, poly8x16x4_t, poly16x4_t, poly16x8_t,
1954                poly64x1_t, poly64x2_t, uint8x8_t, uint8x8x2_t, uint8x8x3_t, uint8x8x4_t, uint8x16_t,
1955                uint8x16x2_t, uint8x16x3_t, uint8x16x4_t, uint16x4_t, uint16x8_t, uint32x2_t, uint32x4_t,
1956                uint64x1_t, uint64x2_t
1957            );
1958            #[cfg(all(feature = "simd-nightly", target_arch = "arm"))]
1959            #[rustfmt::skip]
1960            test_simd_arch_mod!(arm, int8x4_t, uint8x4_t);
1961        }
1962    }
1963}