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