zerocopy/
macros.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
10/// Safely transmutes a value of one type to a value of another type of the same
11/// size.
12///
13/// This macro behaves like an invocation of this function:
14///
15/// ```ignore
16/// const fn transmute<Src, Dst>(src: Src) -> Dst
17/// where
18///     Src: IntoBytes,
19///     Dst: FromBytes,
20///     size_of::<Src>() == size_of::<Dst>(),
21/// {
22/// # /*
23///     ...
24/// # */
25/// }
26/// ```
27///
28/// However, unlike a function, this macro can only be invoked when the types of
29/// `Src` and `Dst` are completely concrete. The types `Src` and `Dst` are
30/// inferred from the calling context; they cannot be explicitly specified in
31/// the macro invocation.
32///
33/// Note that the `Src` produced by the expression `$e` will *not* be dropped.
34/// Semantically, its bits will be copied into a new value of type `Dst`, the
35/// original `Src` will be forgotten, and the value of type `Dst` will be
36/// returned.
37///
38/// # `#![allow(shrink)]`
39///
40/// If `#![allow(shrink)]` is provided, `transmute!` additionally supports
41/// transmutations that shrink the size of the value; e.g.:
42///
43/// ```
44/// # use zerocopy::transmute;
45/// let u: u32 = transmute!(#![allow(shrink)] 0u64);
46/// assert_eq!(u, 0u32);
47/// ```
48///
49/// # Examples
50///
51/// ```
52/// # use zerocopy::transmute;
53/// let one_dimensional: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
54///
55/// let two_dimensional: [[u8; 4]; 2] = transmute!(one_dimensional);
56///
57/// assert_eq!(two_dimensional, [[0, 1, 2, 3], [4, 5, 6, 7]]);
58/// ```
59///
60/// # Use in `const` contexts
61///
62/// This macro can be invoked in `const` contexts.
63#[macro_export]
64macro_rules! transmute {
65    // NOTE: This must be a macro (rather than a function with trait bounds)
66    // because there's no way, in a generic context, to enforce that two types
67    // have the same size. `core::mem::transmute` uses compiler magic to enforce
68    // this so long as the types are concrete.
69    (#![allow(shrink)] $e:expr) => {{
70        let mut e = $e;
71        if false {
72            // This branch, though never taken, ensures that the type of `e` is
73            // `IntoBytes` and that the type of the  outer macro invocation
74            // expression is `FromBytes`.
75
76            fn transmute<Src, Dst>(src: Src) -> Dst
77            where
78                Src: $crate::IntoBytes,
79                Dst: $crate::FromBytes,
80            {
81                let _ = src;
82                loop {}
83            }
84            loop {}
85            #[allow(unreachable_code)]
86            transmute(e)
87        } else {
88            use $crate::util::macro_util::core_reexport::mem::ManuallyDrop;
89
90            // NOTE: `repr(packed)` is important! It ensures that the size of
91            // `Transmute` won't be rounded up to accommodate `Src`'s or `Dst`'s
92            // alignment, which would break the size comparison logic below.
93            //
94            // As an example of why this is problematic, consider `Src = [u8;
95            // 5]`, `Dst = u32`. The total size of `Transmute<Src, Dst>` would
96            // be 8, and so we would reject a `[u8; 5]` to `u32` transmute as
97            // being size-increasing, which it isn't.
98            #[repr(C, packed)]
99            union Transmute<Src, Dst> {
100                src: ManuallyDrop<Src>,
101                dst: ManuallyDrop<Dst>,
102            }
103
104            // SAFETY: `Transmute` is a `repr(C)` union whose `src` field has
105            // type `ManuallyDrop<Src>`. Thus, the `src` field starts at byte
106            // offset 0 within `Transmute` [1]. `ManuallyDrop<T>` has the same
107            // layout and bit validity as `T`, so it is sound to transmute `Src`
108            // to `Transmute`.
109            //
110            // [1] https://doc.rust-lang.org/1.85.0/reference/type-layout.html#reprc-unions
111            //
112            // [2] Per https://doc.rust-lang.org/1.85.0/std/mem/struct.ManuallyDrop.html:
113            //
114            //   `ManuallyDrop<T>` is guaranteed to have the same layout and bit
115            //   validity as `T`
116            let u: Transmute<_, _> = unsafe {
117                // Clippy: We can't annotate the types; this macro is designed
118                // to infer the types from the calling context.
119                #[allow(clippy::missing_transmute_annotations)]
120                $crate::util::macro_util::core_reexport::mem::transmute(e)
121            };
122
123            if false {
124                // SAFETY: This code is never executed.
125                e = ManuallyDrop::into_inner(unsafe { u.src });
126                // Suppress the `unused_assignments` lint on the previous line.
127                let _ = e;
128                loop {}
129            } else {
130                // SAFETY: Per the safety comment on `let u` above, the `dst`
131                // field in `Transmute` starts at byte offset 0, and has the
132                // same layout and bit validity as `Dst`.
133                //
134                // Transmuting `Src` to `Transmute<Src, Dst>` above using
135                // `core::mem::transmute` ensures that `size_of::<Src>() ==
136                // size_of::<Transmute<Src, Dst>>()`. A `#[repr(C, packed)]`
137                // union has the maximum size of all of its fields [1], so this
138                // is equivalent to `size_of::<Src>() >= size_of::<Dst>()`.
139                //
140                // The outer `if`'s `false` branch ensures that `Src: IntoBytes`
141                // and `Dst: FromBytes`. This, combined with the size bound,
142                // ensures that this transmute is sound.
143                //
144                // [1] Per https://doc.rust-lang.org/1.85.0/reference/type-layout.html#reprc-unions:
145                //
146                //   The union will have a size of the maximum size of all of
147                //   its fields rounded to its alignment
148                let dst = unsafe { u.dst };
149                $crate::util::macro_util::must_use(ManuallyDrop::into_inner(dst))
150            }
151        }
152    }};
153    ($e:expr) => {{
154        let e = $e;
155        if false {
156            // This branch, though never taken, ensures that the type of `e` is
157            // `IntoBytes` and that the type of the  outer macro invocation
158            // expression is `FromBytes`.
159
160            fn transmute<Src, Dst>(src: Src) -> Dst
161            where
162                Src: $crate::IntoBytes,
163                Dst: $crate::FromBytes,
164            {
165                let _ = src;
166                loop {}
167            }
168            loop {}
169            #[allow(unreachable_code)]
170            transmute(e)
171        } else {
172            // SAFETY: `core::mem::transmute` ensures that the type of `e` and
173            // the type of this macro invocation expression have the same size.
174            // We know this transmute is safe thanks to the `IntoBytes` and
175            // `FromBytes` bounds enforced by the `false` branch.
176            let u = unsafe {
177                // Clippy: We can't annotate the types; this macro is designed
178                // to infer the types from the calling context.
179                #[allow(clippy::missing_transmute_annotations, unnecessary_transmutes)]
180                $crate::util::macro_util::core_reexport::mem::transmute(e)
181            };
182            $crate::util::macro_util::must_use(u)
183        }
184    }};
185}
186
187/// Safely transmutes a mutable or immutable reference of one type to an
188/// immutable reference of another type of the same size and compatible
189/// alignment.
190///
191/// This macro behaves like an invocation of this function:
192///
193/// ```ignore
194/// fn transmute_ref<'src, 'dst, Src, Dst>(src: &'src Src) -> &'dst Dst
195/// where
196///     'src: 'dst,
197///     Src: IntoBytes + Immutable + ?Sized,
198///     Dst: FromBytes + Immutable + ?Sized,
199///     align_of::<Src>() >= align_of::<Dst>(),
200///     size_compatible::<Src, Dst>(),
201/// {
202/// # /*
203///     ...
204/// # */
205/// }
206/// ```
207///
208/// The types `Src` and `Dst` are inferred from the calling context; they cannot
209/// be explicitly specified in the macro invocation.
210///
211/// # Size compatibility
212///
213/// `transmute_ref!` supports transmuting between `Sized` types, between unsized
214/// (i.e., `?Sized`) types, and from a `Sized` type to an unsized type. It
215/// supports any transmutation that preserves the number of bytes of the
216/// referent, even if doing so requires updating the metadata stored in an
217/// unsized "fat" reference:
218///
219/// ```
220/// # use zerocopy::transmute_ref;
221/// # use core::mem::size_of_val; // Not in the prelude on our MSRV
222/// let src: &[[u8; 2]] = &[[0, 1], [2, 3]][..];
223/// let dst: &[u8] = transmute_ref!(src);
224///
225/// assert_eq!(src.len(), 2);
226/// assert_eq!(dst.len(), 4);
227/// assert_eq!(dst, [0, 1, 2, 3]);
228/// assert_eq!(size_of_val(src), size_of_val(dst));
229/// ```
230///
231/// # Errors
232///
233/// Violations of the alignment and size compatibility checks are detected
234/// *after* the compiler performs monomorphization. This has two important
235/// consequences.
236///
237/// First, it means that generic code will *never* fail these conditions:
238///
239/// ```
240/// # use zerocopy::{transmute_ref, FromBytes, IntoBytes, Immutable};
241/// fn transmute_ref<Src, Dst>(src: &Src) -> &Dst
242/// where
243///     Src: IntoBytes + Immutable,
244///     Dst: FromBytes + Immutable,
245/// {
246///     transmute_ref!(src)
247/// }
248/// ```
249///
250/// Instead, failures will only be detected once generic code is instantiated
251/// with concrete types:
252///
253/// ```compile_fail,E0080
254/// # use zerocopy::{transmute_ref, FromBytes, IntoBytes, Immutable};
255/// #
256/// # fn transmute_ref<Src, Dst>(src: &Src) -> &Dst
257/// # where
258/// #     Src: IntoBytes + Immutable,
259/// #     Dst: FromBytes + Immutable,
260/// # {
261/// #     transmute_ref!(src)
262/// # }
263/// let src: &u16 = &0;
264/// let dst: &u8 = transmute_ref(src);
265/// ```
266///
267/// Second, the fact that violations are detected after monomorphization means
268/// that `cargo check` will usually not detect errors, even when types are
269/// concrete. Instead, `cargo build` must be used to detect such errors.
270///
271/// # Examples
272///
273/// Transmuting between `Sized` types:
274///
275/// ```
276/// # use zerocopy::transmute_ref;
277/// let one_dimensional: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
278///
279/// let two_dimensional: &[[u8; 4]; 2] = transmute_ref!(&one_dimensional);
280///
281/// assert_eq!(two_dimensional, &[[0, 1, 2, 3], [4, 5, 6, 7]]);
282/// ```
283///
284/// Transmuting between unsized types:
285///
286/// ```
287/// # use {zerocopy::*, zerocopy_derive::*};
288/// # type u16 = zerocopy::byteorder::native_endian::U16;
289/// # type u32 = zerocopy::byteorder::native_endian::U32;
290/// #[derive(KnownLayout, FromBytes, IntoBytes, Immutable)]
291/// #[repr(C)]
292/// struct SliceDst<T, U> {
293///     t: T,
294///     u: [U],
295/// }
296///
297/// type Src = SliceDst<u32, u16>;
298/// type Dst = SliceDst<u16, u8>;
299///
300/// let src = Src::ref_from_bytes(&[0, 1, 2, 3, 4, 5, 6, 7]).unwrap();
301/// let dst: &Dst = transmute_ref!(src);
302///
303/// assert_eq!(src.t.as_bytes(), [0, 1, 2, 3]);
304/// assert_eq!(src.u.len(), 2);
305/// assert_eq!(src.u.as_bytes(), [4, 5, 6, 7]);
306///
307/// assert_eq!(dst.t.as_bytes(), [0, 1]);
308/// assert_eq!(dst.u, [2, 3, 4, 5, 6, 7]);
309/// ```
310///
311/// # Use in `const` contexts
312///
313/// This macro can be invoked in `const` contexts only when `Src: Sized` and
314/// `Dst: Sized`.
315#[macro_export]
316macro_rules! transmute_ref {
317    ($e:expr) => {{
318        // NOTE: This must be a macro (rather than a function with trait bounds)
319        // because there's no way, in a generic context, to enforce that two
320        // types have the same size or alignment.
321
322        // Ensure that the source type is a reference or a mutable reference
323        // (note that mutable references are implicitly reborrowed here).
324        let e: &_ = $e;
325
326        #[allow(unused, clippy::diverging_sub_expression)]
327        if false {
328            // This branch, though never taken, ensures that the type of `e` is
329            // `&T` where `T: IntoBytes + Immutable`, and that the type of this
330            // macro expression is `&U` where `U: FromBytes + Immutable`.
331
332            struct AssertSrcIsIntoBytes<'a, T: ?::core::marker::Sized + $crate::IntoBytes>(&'a T);
333            struct AssertSrcIsImmutable<'a, T: ?::core::marker::Sized + $crate::Immutable>(&'a T);
334            struct AssertDstIsFromBytes<'a, U: ?::core::marker::Sized + $crate::FromBytes>(&'a U);
335            struct AssertDstIsImmutable<'a, T: ?::core::marker::Sized + $crate::Immutable>(&'a T);
336
337            let _ = AssertSrcIsIntoBytes(e);
338            let _ = AssertSrcIsImmutable(e);
339
340            if true {
341                #[allow(unused, unreachable_code)]
342                let u = AssertDstIsFromBytes(loop {});
343                u.0
344            } else {
345                #[allow(unused, unreachable_code)]
346                let u = AssertDstIsImmutable(loop {});
347                u.0
348            }
349        } else {
350            use $crate::util::macro_util::TransmuteRefDst;
351            let t = $crate::util::macro_util::Wrap::new(e);
352
353            if false {
354                // This branch exists solely to force the compiler to infer the
355                // type of `Dst` *before* it attempts to resolve the method call
356                // to `transmute_ref` in the `else` branch.
357                //
358                // Without this, if `Src` is `Sized` but `Dst` is `!Sized`, the
359                // compiler will eagerly select the inherent impl of
360                // `transmute_ref` (which requires `Dst: Sized`) because inherent
361                // methods take priority over trait methods. It does this before
362                // it realizes `Dst` is `!Sized`, leading to a compile error when
363                // it checks the bounds later.
364                //
365                // By calling this helper (which returns `&Dst`), we force `Dst`
366                // to be fully resolved. By the time it gets to the `else`
367                // branch, the compiler knows `Dst` is `!Sized`, properly
368                // disqualifies the inherent method, and falls back to the trait
369                // implementation.
370                t.transmute_ref_inference_helper()
371            } else {
372                // SAFETY: The outer `if false` branch ensures that:
373                // - `Src: IntoBytes + Immutable`
374                // - `Dst: FromBytes + Immutable`
375                unsafe {
376                    t.transmute_ref()
377                }
378            }
379        }
380    }}
381}
382
383/// Safely transmutes a mutable reference of one type to a mutable reference of
384/// another type of the same size and compatible alignment.
385///
386/// This macro behaves like an invocation of this function:
387///
388/// ```ignore
389/// const fn transmute_mut<'src, 'dst, Src, Dst>(src: &'src mut Src) -> &'dst mut Dst
390/// where
391///     'src: 'dst,
392///     Src: FromBytes + IntoBytes + ?Sized,
393///     Dst: FromBytes + IntoBytes + ?Sized,
394///     align_of::<Src>() >= align_of::<Dst>(),
395///     size_compatible::<Src, Dst>(),
396/// {
397/// # /*
398///     ...
399/// # */
400/// }
401/// ```
402///
403/// The types `Src` and `Dst` are inferred from the calling context; they cannot
404/// be explicitly specified in the macro invocation.
405///
406/// # Size compatibility
407///
408/// `transmute_mut!` supports transmuting between `Sized` types, between unsized
409/// (i.e., `?Sized`) types, and from a `Sized` type to an unsized type. It
410/// supports any transmutation that preserves the number of bytes of the
411/// referent, even if doing so requires updating the metadata stored in an
412/// unsized "fat" reference:
413///
414/// ```
415/// # use zerocopy::transmute_mut;
416/// # use core::mem::size_of_val; // Not in the prelude on our MSRV
417/// let src: &mut [[u8; 2]] = &mut [[0, 1], [2, 3]][..];
418/// let dst: &mut [u8] = transmute_mut!(src);
419///
420/// assert_eq!(dst.len(), 4);
421/// assert_eq!(dst, [0, 1, 2, 3]);
422/// let dst_size = size_of_val(dst);
423/// assert_eq!(src.len(), 2);
424/// assert_eq!(size_of_val(src), dst_size);
425/// ```
426///
427/// # Errors
428///
429/// Violations of the alignment and size compatibility checks are detected
430/// *after* the compiler performs monomorphization. This has two important
431/// consequences.
432///
433/// First, it means that generic code will *never* fail these conditions:
434///
435/// ```
436/// # use zerocopy::{transmute_mut, FromBytes, IntoBytes, Immutable};
437/// fn transmute_mut<Src, Dst>(src: &mut Src) -> &mut Dst
438/// where
439///     Src: FromBytes + IntoBytes,
440///     Dst: FromBytes + IntoBytes,
441/// {
442///     transmute_mut!(src)
443/// }
444/// ```
445///
446/// Instead, failures will only be detected once generic code is instantiated
447/// with concrete types:
448///
449/// ```compile_fail,E0080
450/// # use zerocopy::{transmute_mut, FromBytes, IntoBytes, Immutable};
451/// #
452/// # fn transmute_mut<Src, Dst>(src: &mut Src) -> &mut Dst
453/// # where
454/// #     Src: FromBytes + IntoBytes,
455/// #     Dst: FromBytes + IntoBytes,
456/// # {
457/// #     transmute_mut!(src)
458/// # }
459/// let src: &mut u16 = &mut 0;
460/// let dst: &mut u8 = transmute_mut(src);
461/// ```
462///
463/// Second, the fact that violations are detected after monomorphization means
464/// that `cargo check` will usually not detect errors, even when types are
465/// concrete. Instead, `cargo build` must be used to detect such errors.
466///
467///
468/// # Examples
469///
470/// Transmuting between `Sized` types:
471///
472/// ```
473/// # use zerocopy::transmute_mut;
474/// let mut one_dimensional: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
475///
476/// let two_dimensional: &mut [[u8; 4]; 2] = transmute_mut!(&mut one_dimensional);
477///
478/// assert_eq!(two_dimensional, &[[0, 1, 2, 3], [4, 5, 6, 7]]);
479///
480/// two_dimensional.reverse();
481///
482/// assert_eq!(one_dimensional, [4, 5, 6, 7, 0, 1, 2, 3]);
483/// ```
484///
485/// Transmuting between unsized types:
486///
487/// ```
488/// # use {zerocopy::*, zerocopy_derive::*};
489/// # type u16 = zerocopy::byteorder::native_endian::U16;
490/// # type u32 = zerocopy::byteorder::native_endian::U32;
491/// #[derive(KnownLayout, FromBytes, IntoBytes, Immutable)]
492/// #[repr(C)]
493/// struct SliceDst<T, U> {
494///     t: T,
495///     u: [U],
496/// }
497///
498/// type Src = SliceDst<u32, u16>;
499/// type Dst = SliceDst<u16, u8>;
500///
501/// let mut bytes = [0, 1, 2, 3, 4, 5, 6, 7];
502/// let src = Src::mut_from_bytes(&mut bytes[..]).unwrap();
503/// let dst: &mut Dst = transmute_mut!(src);
504///
505/// assert_eq!(dst.t.as_bytes(), [0, 1]);
506/// assert_eq!(dst.u, [2, 3, 4, 5, 6, 7]);
507///
508/// assert_eq!(src.t.as_bytes(), [0, 1, 2, 3]);
509/// assert_eq!(src.u.len(), 2);
510/// assert_eq!(src.u.as_bytes(), [4, 5, 6, 7]);
511///
512/// ```
513#[macro_export]
514macro_rules! transmute_mut {
515    ($e:expr) => {{
516        // NOTE: This must be a macro (rather than a function with trait bounds)
517        // because, for backwards-compatibility on v0.8.x, we use the autoref
518        // specialization trick to dispatch to different `transmute_mut`
519        // implementations: one which doesn't require `Src: KnownLayout + Dst:
520        // KnownLayout` when `Src: Sized + Dst: Sized`, and one which requires
521        // `KnownLayout` bounds otherwise.
522
523        // Ensure that the source type is a mutable reference.
524        let e: &mut _ = $e;
525
526        #[allow(unused)]
527        use $crate::util::macro_util::TransmuteMutDst as _;
528        let t = $crate::util::macro_util::Wrap::new(e);
529        if false {
530            // This branch exists solely to force the compiler to infer the type
531            // of `Dst` *before* it attempts to resolve the method call to
532            // `transmute_mut` in the `else` branch.
533            //
534            // Without this, if `Src` is `Sized` but `Dst` is `!Sized`, the
535            // compiler will eagerly select the inherent impl of `transmute_mut`
536            // (which requires `Dst: Sized`) because inherent methods take
537            // priority over trait methods. It does this before it realizes
538            // `Dst` is `!Sized`, leading to a compile error when it checks the
539            // bounds later.
540            //
541            // By calling this helper (which returns `&mut Dst`), we force `Dst`
542            // to be fully resolved. By the time it gets to the `else` branch,
543            // the compiler knows `Dst` is `!Sized`, properly disqualifies the
544            // inherent method, and falls back to the trait implementation.
545            t.transmute_mut_inference_helper()
546        } else {
547            t.transmute_mut()
548        }
549    }}
550}
551
552/// Conditionally transmutes a value of one type to a value of another type of
553/// the same size.
554///
555/// This macro behaves like an invocation of this function:
556///
557/// ```ignore
558/// fn try_transmute<Src, Dst>(src: Src) -> Result<Dst, ValidityError<Src, Dst>>
559/// where
560///     Src: IntoBytes,
561///     Dst: TryFromBytes,
562///     size_of::<Src>() == size_of::<Dst>(),
563/// {
564/// # /*
565///     ...
566/// # */
567/// }
568/// ```
569///
570/// However, unlike a function, this macro can only be invoked when the types of
571/// `Src` and `Dst` are completely concrete. The types `Src` and `Dst` are
572/// inferred from the calling context; they cannot be explicitly specified in
573/// the macro invocation.
574///
575/// Note that the `Src` produced by the expression `$e` will *not* be dropped.
576/// Semantically, its bits will be copied into a new value of type `Dst`, the
577/// original `Src` will be forgotten, and the value of type `Dst` will be
578/// returned.
579///
580/// # Examples
581///
582/// ```
583/// # use zerocopy::*;
584/// // 0u8 → bool = false
585/// assert_eq!(try_transmute!(0u8), Ok(false));
586///
587/// // 1u8 → bool = true
588///  assert_eq!(try_transmute!(1u8), Ok(true));
589///
590/// // 2u8 → bool = error
591/// assert!(matches!(
592///     try_transmute!(2u8),
593///     Result::<bool, _>::Err(ValidityError { .. })
594/// ));
595/// ```
596#[macro_export]
597macro_rules! try_transmute {
598    ($e:expr) => {{
599        // NOTE: This must be a macro (rather than a function with trait bounds)
600        // because there's no way, in a generic context, to enforce that two
601        // types have the same size. `core::mem::transmute` uses compiler magic
602        // to enforce this so long as the types are concrete.
603
604        let e = $e;
605        if false {
606            // Check that the sizes of the source and destination types are
607            // equal.
608
609            // SAFETY: This code is never executed.
610            Ok(unsafe {
611                // Clippy: We can't annotate the types; this macro is designed
612                // to infer the types from the calling context.
613                #[allow(clippy::missing_transmute_annotations)]
614                $crate::util::macro_util::core_reexport::mem::transmute(e)
615            })
616        } else {
617            $crate::util::macro_util::try_transmute::<_, _>(e)
618        }
619    }}
620}
621
622/// Conditionally transmutes a mutable or immutable reference of one type to an
623/// immutable reference of another type of the same size and compatible
624/// alignment.
625///
626/// *Note that while the **value** of the referent is checked for validity at
627/// runtime, the **size** and **alignment** are checked at compile time. For
628/// conversions which are fallible with respect to size and alignment, see the
629/// methods on [`TryFromBytes`].*
630///
631/// This macro behaves like an invocation of this function:
632///
633/// ```ignore
634/// fn try_transmute_ref<Src, Dst>(src: &Src) -> Result<&Dst, ValidityError<&Src, Dst>>
635/// where
636///     Src: IntoBytes + Immutable + ?Sized,
637///     Dst: TryFromBytes + Immutable + ?Sized,
638///     align_of::<Src>() >= align_of::<Dst>(),
639///     size_compatible::<Src, Dst>(),
640/// {
641/// # /*
642///     ...
643/// # */
644/// }
645/// ```
646///
647/// The types `Src` and `Dst` are inferred from the calling context; they cannot
648/// be explicitly specified in the macro invocation.
649///
650/// [`TryFromBytes`]: crate::TryFromBytes
651///
652/// # Size compatibility
653///
654/// `try_transmute_ref!` supports transmuting between `Sized` types, between
655/// unsized (i.e., `?Sized`) types, and from a `Sized` type to an unsized type.
656/// It supports any transmutation that preserves the number of bytes of the
657/// referent, even if doing so requires updating the metadata stored in an
658/// unsized "fat" reference:
659///
660/// ```
661/// # use zerocopy::try_transmute_ref;
662/// # use core::mem::size_of_val; // Not in the prelude on our MSRV
663/// let src: &[[u8; 2]] = &[[0, 1], [2, 3]][..];
664/// let dst: &[u8] = try_transmute_ref!(src).unwrap();
665///
666/// assert_eq!(src.len(), 2);
667/// assert_eq!(dst.len(), 4);
668/// assert_eq!(dst, [0, 1, 2, 3]);
669/// assert_eq!(size_of_val(src), size_of_val(dst));
670/// ```
671///
672/// # Examples
673///
674/// Transmuting between `Sized` types:
675///
676/// ```
677/// # use zerocopy::*;
678/// // 0u8 → bool = false
679/// assert_eq!(try_transmute_ref!(&0u8), Ok(&false));
680///
681/// // 1u8 → bool = true
682///  assert_eq!(try_transmute_ref!(&1u8), Ok(&true));
683///
684/// // 2u8 → bool = error
685/// assert!(matches!(
686///     try_transmute_ref!(&2u8),
687///     Result::<&bool, _>::Err(ValidityError { .. })
688/// ));
689/// ```
690///
691/// Transmuting between unsized types:
692///
693/// ```
694/// # use {zerocopy::*, zerocopy_derive::*};
695/// # type u16 = zerocopy::byteorder::native_endian::U16;
696/// # type u32 = zerocopy::byteorder::native_endian::U32;
697/// #[derive(KnownLayout, FromBytes, IntoBytes, Immutable)]
698/// #[repr(C)]
699/// struct SliceDst<T, U> {
700///     t: T,
701///     u: [U],
702/// }
703///
704/// type Src = SliceDst<u32, u16>;
705/// type Dst = SliceDst<u16, bool>;
706///
707/// let src = Src::ref_from_bytes(&[0, 1, 0, 1, 0, 1, 0, 1]).unwrap();
708/// let dst: &Dst = try_transmute_ref!(src).unwrap();
709///
710/// assert_eq!(src.t.as_bytes(), [0, 1, 0, 1]);
711/// assert_eq!(src.u.len(), 2);
712/// assert_eq!(src.u.as_bytes(), [0, 1, 0, 1]);
713///
714/// assert_eq!(dst.t.as_bytes(), [0, 1]);
715/// assert_eq!(dst.u, [false, true, false, true, false, true]);
716/// ```
717#[macro_export]
718macro_rules! try_transmute_ref {
719    ($e:expr) => {{
720        // Ensure that the source type is a reference or a mutable reference
721        // (note that mutable references are implicitly reborrowed here).
722        let e: &_ = $e;
723
724        #[allow(unused_imports)]
725        use $crate::util::macro_util::TryTransmuteRefDst as _;
726        let t = $crate::util::macro_util::Wrap::new(e);
727        if false {
728            // This branch exists solely to force the compiler to infer the type
729            // of `Dst` *before* it attempts to resolve the method call to
730            // `try_transmute_ref` in the `else` branch.
731            //
732            // Without this, if `Src` is `Sized` but `Dst` is `!Sized`, the
733            // compiler will eagerly select the inherent impl of
734            // `try_transmute_ref` (which requires `Dst: Sized`) because
735            // inherent methods take priority over trait methods. It does this
736            // before it realizes `Dst` is `!Sized`, leading to a compile error
737            // when it checks the bounds later.
738            //
739            // By calling this helper (which returns `&Dst`), we force `Dst`
740            // to be fully resolved. By the time it gets to the `else`
741            // branch, the compiler knows `Dst` is `!Sized`, properly
742            // disqualifies the inherent method, and falls back to the trait
743            // implementation.
744            Ok(t.transmute_ref_inference_helper())
745        } else {
746            t.try_transmute_ref()
747        }
748    }}
749}
750
751/// Conditionally transmutes a mutable reference of one type to a mutable
752/// reference of another type of the same size and compatible alignment.
753///
754/// *Note that while the **value** of the referent is checked for validity at
755/// runtime, the **size** and **alignment** are checked at compile time. For
756/// conversions which are fallible with respect to size and alignment, see the
757/// methods on [`TryFromBytes`].*
758///
759/// This macro behaves like an invocation of this function:
760///
761/// ```ignore
762/// fn try_transmute_mut<Src, Dst>(src: &mut Src) -> Result<&mut Dst, ValidityError<&mut Src, Dst>>
763/// where
764///     Src: FromBytes + IntoBytes + ?Sized,
765///     Dst: TryFromBytes + IntoBytes + ?Sized,
766///     align_of::<Src>() >= align_of::<Dst>(),
767///     size_compatible::<Src, Dst>(),
768/// {
769/// # /*
770///     ...
771/// # */
772/// }
773/// ```
774///
775/// The types `Src` and `Dst` are inferred from the calling context; they cannot
776/// be explicitly specified in the macro invocation.
777///
778/// [`TryFromBytes`]: crate::TryFromBytes
779///
780/// # Size compatibility
781///
782/// `try_transmute_mut!` supports transmuting between `Sized` types, between
783/// unsized (i.e., `?Sized`) types, and from a `Sized` type to an unsized type.
784/// It supports any transmutation that preserves the number of bytes of the
785/// referent, even if doing so requires updating the metadata stored in an
786/// unsized "fat" reference:
787///
788/// ```
789/// # use zerocopy::try_transmute_mut;
790/// # use core::mem::size_of_val; // Not in the prelude on our MSRV
791/// let src: &mut [[u8; 2]] = &mut [[0, 1], [2, 3]][..];
792/// let dst: &mut [u8] = try_transmute_mut!(src).unwrap();
793///
794/// assert_eq!(dst.len(), 4);
795/// assert_eq!(dst, [0, 1, 2, 3]);
796/// let dst_size = size_of_val(dst);
797/// assert_eq!(src.len(), 2);
798/// assert_eq!(size_of_val(src), dst_size);
799/// ```
800///
801/// # Examples
802///
803/// Transmuting between `Sized` types:
804///
805/// ```
806/// # use zerocopy::*;
807/// // 0u8 → bool = false
808/// let src = &mut 0u8;
809/// assert_eq!(try_transmute_mut!(src), Ok(&mut false));
810///
811/// // 1u8 → bool = true
812/// let src = &mut 1u8;
813///  assert_eq!(try_transmute_mut!(src), Ok(&mut true));
814///
815/// // 2u8 → bool = error
816/// let src = &mut 2u8;
817/// assert!(matches!(
818///     try_transmute_mut!(src),
819///     Result::<&mut bool, _>::Err(ValidityError { .. })
820/// ));
821/// ```
822///
823/// Transmuting between unsized types:
824///
825/// ```
826/// # use {zerocopy::*, zerocopy_derive::*};
827/// # type u16 = zerocopy::byteorder::native_endian::U16;
828/// # type u32 = zerocopy::byteorder::native_endian::U32;
829/// #[derive(KnownLayout, FromBytes, IntoBytes, Immutable)]
830/// #[repr(C)]
831/// struct SliceDst<T, U> {
832///     t: T,
833///     u: [U],
834/// }
835///
836/// type Src = SliceDst<u32, u16>;
837/// type Dst = SliceDst<u16, bool>;
838///
839/// let mut bytes = [0, 1, 0, 1, 0, 1, 0, 1];
840/// let src = Src::mut_from_bytes(&mut bytes).unwrap();
841///
842/// assert_eq!(src.t.as_bytes(), [0, 1, 0, 1]);
843/// assert_eq!(src.u.len(), 2);
844/// assert_eq!(src.u.as_bytes(), [0, 1, 0, 1]);
845///
846/// let dst: &Dst = try_transmute_mut!(src).unwrap();
847///
848/// assert_eq!(dst.t.as_bytes(), [0, 1]);
849/// assert_eq!(dst.u, [false, true, false, true, false, true]);
850/// ```
851#[macro_export]
852macro_rules! try_transmute_mut {
853    ($e:expr) => {{
854        // Ensure that the source type is a mutable reference.
855        let e: &mut _ = $e;
856
857        #[allow(unused_imports)]
858        use $crate::util::macro_util::TryTransmuteMutDst as _;
859        let t = $crate::util::macro_util::Wrap::new(e);
860        if false {
861            // This branch exists solely to force the compiler to infer the type
862            // of `Dst` *before* it attempts to resolve the method call to
863            // `try_transmute_mut` in the `else` branch.
864            //
865            // Without this, if `Src` is `Sized` but `Dst` is `!Sized`, the
866            // compiler will eagerly select the inherent impl of
867            // `try_transmute_mut` (which requires `Dst: Sized`) because
868            // inherent methods take priority over trait methods. It does this
869            // before it realizes `Dst` is `!Sized`, leading to a compile error
870            // when it checks the bounds later.
871            //
872            // By calling this helper (which returns `&Dst`), we force `Dst`
873            // to be fully resolved. By the time it gets to the `else`
874            // branch, the compiler knows `Dst` is `!Sized`, properly
875            // disqualifies the inherent method, and falls back to the trait
876            // implementation.
877            Ok(t.transmute_mut_inference_helper())
878        } else {
879            t.try_transmute_mut()
880        }
881    }}
882}
883
884/// Includes a file and safely transmutes it to a value of an arbitrary type.
885///
886/// The file will be included as a byte array, `[u8; N]`, which will be
887/// transmuted to another type, `T`. `T` is inferred from the calling context,
888/// and must implement [`FromBytes`].
889///
890/// The file is located relative to the current file (similarly to how modules
891/// are found). The provided path is interpreted in a platform-specific way at
892/// compile time. So, for instance, an invocation with a Windows path containing
893/// backslashes `\` would not compile correctly on Unix.
894///
895/// `include_value!` is ignorant of byte order. For byte order-aware types, see
896/// the [`byteorder`] module.
897///
898/// [`FromBytes`]: crate::FromBytes
899/// [`byteorder`]: crate::byteorder
900///
901/// # Examples
902///
903/// Assume there are two files in the same directory with the following
904/// contents:
905///
906/// File `data` (no trailing newline):
907///
908/// ```text
909/// abcd
910/// ```
911///
912/// File `main.rs`:
913///
914/// ```rust
915/// use zerocopy::include_value;
916/// # macro_rules! include_value {
917/// # ($file:expr) => { zerocopy::include_value!(concat!("../testdata/include_value/", $file)) };
918/// # }
919///
920/// fn main() {
921///     let as_u32: u32 = include_value!("data");
922///     assert_eq!(as_u32, u32::from_ne_bytes([b'a', b'b', b'c', b'd']));
923///     let as_i32: i32 = include_value!("data");
924///     assert_eq!(as_i32, i32::from_ne_bytes([b'a', b'b', b'c', b'd']));
925/// }
926/// ```
927///
928/// # Use in `const` contexts
929///
930/// This macro can be invoked in `const` contexts.
931#[doc(alias("include_bytes", "include_data", "include_type"))]
932#[macro_export]
933macro_rules! include_value {
934    ($file:expr $(,)?) => {
935        $crate::transmute!(*::core::include_bytes!($file))
936    };
937}
938
939#[doc(hidden)]
940#[macro_export]
941macro_rules! cryptocorrosion_derive_traits {
942    (
943        #[repr($repr:ident)]
944        $(#[$attr:meta])*
945        $vis:vis struct $name:ident $(<$($tyvar:ident),*>)?
946        $(
947            (
948                $($tuple_field_vis:vis $tuple_field_ty:ty),*
949            );
950        )?
951
952        $(
953            {
954                $($field_vis:vis $field_name:ident: $field_ty:ty,)*
955            }
956        )?
957    ) => {
958        $crate::cryptocorrosion_derive_traits!(@assert_allowed_struct_repr #[repr($repr)]);
959
960        $(#[$attr])*
961        #[repr($repr)]
962        $vis struct $name $(<$($tyvar),*>)?
963        $(
964            (
965                $($tuple_field_vis $tuple_field_ty),*
966            );
967        )?
968
969        $(
970            {
971                $($field_vis $field_name: $field_ty,)*
972            }
973        )?
974
975        // SAFETY: See inline.
976        unsafe impl $(<$($tyvar),*>)? $crate::TryFromBytes for $name$(<$($tyvar),*>)?
977        where
978            $(
979                $($tuple_field_ty: $crate::FromBytes,)*
980            )?
981
982            $(
983                $($field_ty: $crate::FromBytes,)*
984            )?
985        {
986            #[inline(always)]
987            fn is_bit_valid<A>(_: $crate::Maybe<'_, Self, A>) -> bool
988            where
989                A: $crate::invariant::Alignment,
990            {
991                // SAFETY: This macro only accepts `#[repr(C)]` and
992                // `#[repr(transparent)]` structs, and this `impl` block
993                // requires all field types to be `FromBytes`. Thus, all
994                // initialized byte sequences constitutes valid instances of
995                // `Self`.
996                true
997            }
998
999            fn only_derive_is_allowed_to_implement_this_trait() {}
1000        }
1001
1002        // SAFETY: This macro only accepts `#[repr(C)]` and
1003        // `#[repr(transparent)]` structs, and this `impl` block requires all
1004        // field types to be `FromBytes`, which is a sub-trait of `FromZeros`.
1005        unsafe impl $(<$($tyvar),*>)? $crate::FromZeros for $name$(<$($tyvar),*>)?
1006        where
1007            $(
1008                $($tuple_field_ty: $crate::FromBytes,)*
1009            )?
1010
1011            $(
1012                $($field_ty: $crate::FromBytes,)*
1013            )?
1014        {
1015            fn only_derive_is_allowed_to_implement_this_trait() {}
1016        }
1017
1018        // SAFETY: This macro only accepts `#[repr(C)]` and
1019        // `#[repr(transparent)]` structs, and this `impl` block requires all
1020        // field types to be `FromBytes`.
1021        unsafe impl $(<$($tyvar),*>)? $crate::FromBytes for $name$(<$($tyvar),*>)?
1022        where
1023            $(
1024                $($tuple_field_ty: $crate::FromBytes,)*
1025            )?
1026
1027            $(
1028                $($field_ty: $crate::FromBytes,)*
1029            )?
1030        {
1031            fn only_derive_is_allowed_to_implement_this_trait() {}
1032        }
1033
1034        // SAFETY: This macro only accepts `#[repr(C)]` and
1035        // `#[repr(transparent)]` structs, this `impl` block requires all field
1036        // types to be `IntoBytes`, and a padding check is used to ensures that
1037        // there are no padding bytes.
1038        unsafe impl $(<$($tyvar),*>)? $crate::IntoBytes for $name$(<$($tyvar),*>)?
1039        where
1040            $(
1041                $($tuple_field_ty: $crate::IntoBytes,)*
1042            )?
1043
1044            $(
1045                $($field_ty: $crate::IntoBytes,)*
1046            )?
1047
1048            (): $crate::util::macro_util::PaddingFree<
1049                Self,
1050                {
1051                    $crate::cryptocorrosion_derive_traits!(
1052                        @struct_padding_check #[repr($repr)]
1053                        $(($($tuple_field_ty),*))?
1054                        $({$($field_ty),*})?
1055                    )
1056                },
1057            >,
1058        {
1059            fn only_derive_is_allowed_to_implement_this_trait() {}
1060        }
1061
1062        // SAFETY: This macro only accepts `#[repr(C)]` and
1063        // `#[repr(transparent)]` structs, and this `impl` block requires all
1064        // field types to be `Immutable`.
1065        unsafe impl $(<$($tyvar),*>)? $crate::Immutable for $name$(<$($tyvar),*>)?
1066        where
1067            $(
1068                $($tuple_field_ty: $crate::Immutable,)*
1069            )?
1070
1071            $(
1072                $($field_ty: $crate::Immutable,)*
1073            )?
1074        {
1075            fn only_derive_is_allowed_to_implement_this_trait() {}
1076        }
1077    };
1078    (@assert_allowed_struct_repr #[repr(transparent)]) => {};
1079    (@assert_allowed_struct_repr #[repr(C)]) => {};
1080    (@assert_allowed_struct_repr #[$_attr:meta]) => {
1081        compile_error!("repr must be `#[repr(transparent)]` or `#[repr(C)]`");
1082    };
1083    (
1084        @struct_padding_check #[repr(transparent)]
1085        $(($($tuple_field_ty:ty),*))?
1086        $({$($field_ty:ty),*})?
1087    ) => {
1088        // SAFETY: `#[repr(transparent)]` structs cannot have the same layout as
1089        // their single non-zero-sized field, and so cannot have any padding
1090        // outside of that field.
1091        0
1092    };
1093    (
1094        @struct_padding_check #[repr(C)]
1095        $(($($tuple_field_ty:ty),*))?
1096        $({$($field_ty:ty),*})?
1097    ) => {
1098        $crate::struct_padding!(
1099            Self,
1100            [
1101                $($($tuple_field_ty),*)?
1102                $($($field_ty),*)?
1103            ]
1104        )
1105    };
1106    (
1107        #[repr(C)]
1108        $(#[$attr:meta])*
1109        $vis:vis union $name:ident {
1110            $(
1111                $field_name:ident: $field_ty:ty,
1112            )*
1113        }
1114    ) => {
1115        $(#[$attr])*
1116        #[repr(C)]
1117        $vis union $name {
1118            $(
1119                $field_name: $field_ty,
1120            )*
1121        }
1122
1123        // SAFETY: See inline.
1124        unsafe impl $crate::TryFromBytes for $name
1125        where
1126            $(
1127                $field_ty: $crate::FromBytes,
1128            )*
1129        {
1130            #[inline(always)]
1131            fn is_bit_valid<A>(_: $crate::Maybe<'_, Self, A>) -> bool
1132            where
1133                A: $crate::invariant::Alignment,
1134            {
1135                // SAFETY: This macro only accepts `#[repr(C)]` unions, and this
1136                // `impl` block requires all field types to be `FromBytes`.
1137                // Thus, all initialized byte sequences constitutes valid
1138                // instances of `Self`.
1139                true
1140            }
1141
1142            fn only_derive_is_allowed_to_implement_this_trait() {}
1143        }
1144
1145        // SAFETY: This macro only accepts `#[repr(C)]` unions, and this `impl`
1146        // block requires all field types to be `FromBytes`, which is a
1147        // sub-trait of `FromZeros`.
1148        unsafe impl $crate::FromZeros for $name
1149        where
1150            $(
1151                $field_ty: $crate::FromBytes,
1152            )*
1153        {
1154            fn only_derive_is_allowed_to_implement_this_trait() {}
1155        }
1156
1157        // SAFETY: This macro only accepts `#[repr(C)]` unions, and this `impl`
1158        // block requires all field types to be `FromBytes`.
1159        unsafe impl $crate::FromBytes for $name
1160        where
1161            $(
1162                $field_ty: $crate::FromBytes,
1163            )*
1164        {
1165            fn only_derive_is_allowed_to_implement_this_trait() {}
1166        }
1167
1168        // SAFETY: This macro only accepts `#[repr(C)]` unions, this `impl`
1169        // block requires all field types to be `IntoBytes`, and a padding check
1170        // is used to ensures that there are no padding bytes before or after
1171        // any field.
1172        unsafe impl $crate::IntoBytes for $name
1173        where
1174            $(
1175                $field_ty: $crate::IntoBytes,
1176            )*
1177            (): $crate::util::macro_util::PaddingFree<
1178                Self,
1179                {
1180                    $crate::union_padding!(
1181                        Self,
1182                        [$($field_ty),*]
1183                    )
1184                },
1185            >,
1186        {
1187            fn only_derive_is_allowed_to_implement_this_trait() {}
1188        }
1189
1190        // SAFETY: This macro only accepts `#[repr(C)]` unions, and this `impl`
1191        // block requires all field types to be `Immutable`.
1192        unsafe impl $crate::Immutable for $name
1193        where
1194            $(
1195                $field_ty: $crate::Immutable,
1196            )*
1197        {
1198            fn only_derive_is_allowed_to_implement_this_trait() {}
1199        }
1200    };
1201}
1202
1203#[cfg(test)]
1204mod tests {
1205    use crate::{
1206        byteorder::native_endian::{U16, U32},
1207        util::testutil::*,
1208        *,
1209    };
1210
1211    #[derive(KnownLayout, Immutable, FromBytes, IntoBytes, PartialEq, Debug)]
1212    #[repr(C)]
1213    struct SliceDst<T, U> {
1214        a: T,
1215        b: [U],
1216    }
1217
1218    #[test]
1219    fn test_transmute() {
1220        // Test that memory is transmuted as expected.
1221        let array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
1222        let array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
1223        let x: [[u8; 2]; 4] = transmute!(array_of_u8s);
1224        assert_eq!(x, array_of_arrays);
1225        let x: [u8; 8] = transmute!(array_of_arrays);
1226        assert_eq!(x, array_of_u8s);
1227
1228        // Test that memory is transmuted as expected when shrinking.
1229        let x: [[u8; 2]; 3] = transmute!(#![allow(shrink)] array_of_u8s);
1230        assert_eq!(x, [[0u8, 1], [2, 3], [4, 5]]);
1231
1232        // Test that the source expression's value is forgotten rather than
1233        // dropped.
1234        #[derive(IntoBytes)]
1235        #[repr(transparent)]
1236        struct PanicOnDrop(());
1237        impl Drop for PanicOnDrop {
1238            fn drop(&mut self) {
1239                panic!("PanicOnDrop::drop");
1240            }
1241        }
1242        #[allow(clippy::let_unit_value)]
1243        let _: () = transmute!(PanicOnDrop(()));
1244        #[allow(clippy::let_unit_value)]
1245        let _: () = transmute!(#![allow(shrink)] PanicOnDrop(()));
1246
1247        // Test that `transmute!` is legal in a const context.
1248        const ARRAY_OF_U8S: [u8; 8] = [0u8, 1, 2, 3, 4, 5, 6, 7];
1249        const ARRAY_OF_ARRAYS: [[u8; 2]; 4] = [[0, 1], [2, 3], [4, 5], [6, 7]];
1250        const X: [[u8; 2]; 4] = transmute!(ARRAY_OF_U8S);
1251        assert_eq!(X, ARRAY_OF_ARRAYS);
1252        const X_SHRINK: [[u8; 2]; 3] = transmute!(#![allow(shrink)] ARRAY_OF_U8S);
1253        assert_eq!(X_SHRINK, [[0u8, 1], [2, 3], [4, 5]]);
1254
1255        // Test that `transmute!` works with `!Immutable` types.
1256        let x: usize = transmute!(UnsafeCell::new(1usize));
1257        assert_eq!(x, 1);
1258        let x: UnsafeCell<usize> = transmute!(1usize);
1259        assert_eq!(x.into_inner(), 1);
1260        let x: UnsafeCell<isize> = transmute!(UnsafeCell::new(1usize));
1261        assert_eq!(x.into_inner(), 1);
1262    }
1263
1264    // A `Sized` type which doesn't implement `KnownLayout` (it is "not
1265    // `KnownLayout`", or `Nkl`).
1266    //
1267    // This permits us to test that `transmute_ref!` and `transmute_mut!` work
1268    // for types which are `Sized + !KnownLayout`. When we added support for
1269    // slice DSTs in #1924, this new support relied on `KnownLayout`, but we
1270    // need to make sure to remain backwards-compatible with code which uses
1271    // these macros with types which are `!KnownLayout`.
1272    #[derive(FromBytes, IntoBytes, Immutable, PartialEq, Eq, Debug)]
1273    #[repr(transparent)]
1274    struct Nkl<T>(T);
1275
1276    #[test]
1277    fn test_transmute_ref() {
1278        // Test that memory is transmuted as expected.
1279        let array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
1280        let array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
1281        let x: &[[u8; 2]; 4] = transmute_ref!(&array_of_u8s);
1282        assert_eq!(*x, array_of_arrays);
1283        let x: &[u8; 8] = transmute_ref!(&array_of_arrays);
1284        assert_eq!(*x, array_of_u8s);
1285
1286        // Test that `transmute_ref!` is legal in a const context.
1287        const ARRAY_OF_U8S: [u8; 8] = [0u8, 1, 2, 3, 4, 5, 6, 7];
1288        const ARRAY_OF_ARRAYS: [[u8; 2]; 4] = [[0, 1], [2, 3], [4, 5], [6, 7]];
1289        #[allow(clippy::redundant_static_lifetimes)]
1290        const X: &'static [[u8; 2]; 4] = transmute_ref!(&ARRAY_OF_U8S);
1291        assert_eq!(*X, ARRAY_OF_ARRAYS);
1292
1293        // Test sized -> unsized transmutation.
1294        let array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
1295        let array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
1296        let slice_of_arrays = &array_of_arrays[..];
1297        let x: &[[u8; 2]] = transmute_ref!(&array_of_u8s);
1298        assert_eq!(x, slice_of_arrays);
1299
1300        // Before 1.61.0, we can't define the `const fn transmute_ref` function
1301        // that we do on and after 1.61.0.
1302        #[cfg(no_zerocopy_generic_bounds_in_const_fn_1_61_0)]
1303        {
1304            // Test that `transmute_ref!` supports non-`KnownLayout` `Sized`
1305            // types.
1306            const ARRAY_OF_NKL_U8S: Nkl<[u8; 8]> = Nkl([0u8, 1, 2, 3, 4, 5, 6, 7]);
1307            const ARRAY_OF_NKL_ARRAYS: Nkl<[[u8; 2]; 4]> = Nkl([[0, 1], [2, 3], [4, 5], [6, 7]]);
1308            const X_NKL: &Nkl<[[u8; 2]; 4]> = transmute_ref!(&ARRAY_OF_NKL_U8S);
1309            assert_eq!(*X_NKL, ARRAY_OF_NKL_ARRAYS);
1310        }
1311
1312        #[cfg(not(no_zerocopy_generic_bounds_in_const_fn_1_61_0))]
1313        {
1314            // Call through a generic function to make sure our autoref
1315            // specialization trick works even when types are generic.
1316            const fn transmute_ref<T, U>(t: &T) -> &U
1317            where
1318                T: IntoBytes + Immutable,
1319                U: FromBytes + Immutable,
1320            {
1321                transmute_ref!(t)
1322            }
1323
1324            // Test that `transmute_ref!` supports non-`KnownLayout` `Sized`
1325            // types.
1326            const ARRAY_OF_NKL_U8S: Nkl<[u8; 8]> = Nkl([0u8, 1, 2, 3, 4, 5, 6, 7]);
1327            const ARRAY_OF_NKL_ARRAYS: Nkl<[[u8; 2]; 4]> = Nkl([[0, 1], [2, 3], [4, 5], [6, 7]]);
1328            const X_NKL: &Nkl<[[u8; 2]; 4]> = transmute_ref(&ARRAY_OF_NKL_U8S);
1329            assert_eq!(*X_NKL, ARRAY_OF_NKL_ARRAYS);
1330        }
1331
1332        // Test that `transmute_ref!` works on slice DSTs in and that memory is
1333        // transmuted as expected.
1334        let slice_dst_of_u8s =
1335            SliceDst::<U16, [u8; 2]>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
1336        let slice_dst_of_u16s =
1337            SliceDst::<U16, U16>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
1338        let x: &SliceDst<U16, U16> = transmute_ref!(slice_dst_of_u8s);
1339        assert_eq!(x, slice_dst_of_u16s);
1340
1341        let slice_dst_of_u8s =
1342            SliceDst::<U16, u8>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
1343        let x: &[u8] = transmute_ref!(slice_dst_of_u8s);
1344        assert_eq!(x, [0, 1, 2, 3, 4, 5]);
1345
1346        let x: &[u8] = transmute_ref!(slice_dst_of_u16s);
1347        assert_eq!(x, [0, 1, 2, 3, 4, 5]);
1348
1349        let x: &[U16] = transmute_ref!(slice_dst_of_u16s);
1350        let slice_of_u16s: &[U16] = <[U16]>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
1351        assert_eq!(x, slice_of_u16s);
1352
1353        // Test that transmuting from a type with larger trailing slice offset
1354        // and larger trailing slice element works.
1355        let bytes = &[0, 1, 2, 3, 4, 5, 6, 7][..];
1356        let slice_dst_big = SliceDst::<U32, U16>::ref_from_bytes(bytes).unwrap();
1357        let slice_dst_small = SliceDst::<U16, u8>::ref_from_bytes(bytes).unwrap();
1358        let x: &SliceDst<U16, u8> = transmute_ref!(slice_dst_big);
1359        assert_eq!(x, slice_dst_small);
1360
1361        // Test that it's legal to transmute a reference while shrinking the
1362        // lifetime (note that `X` has the lifetime `'static`).
1363        let x: &[u8; 8] = transmute_ref!(X);
1364        assert_eq!(*x, ARRAY_OF_U8S);
1365
1366        // Test that `transmute_ref!` supports decreasing alignment.
1367        let u = AU64(0);
1368        let array = [0, 0, 0, 0, 0, 0, 0, 0];
1369        let x: &[u8; 8] = transmute_ref!(&u);
1370        assert_eq!(*x, array);
1371
1372        // Test that a mutable reference can be turned into an immutable one.
1373        let mut x = 0u8;
1374        #[allow(clippy::useless_transmute)]
1375        let y: &u8 = transmute_ref!(&mut x);
1376        assert_eq!(*y, 0);
1377    }
1378
1379    #[test]
1380    fn test_try_transmute() {
1381        // Test that memory is transmuted with `try_transmute` as expected.
1382        let array_of_bools = [false, true, false, true, false, true, false, true];
1383        let array_of_arrays = [[0, 1], [0, 1], [0, 1], [0, 1]];
1384        let x: Result<[[u8; 2]; 4], _> = try_transmute!(array_of_bools);
1385        assert_eq!(x, Ok(array_of_arrays));
1386        let x: Result<[bool; 8], _> = try_transmute!(array_of_arrays);
1387        assert_eq!(x, Ok(array_of_bools));
1388
1389        // Test that `try_transmute!` works with `!Immutable` types.
1390        let x: Result<usize, _> = try_transmute!(UnsafeCell::new(1usize));
1391        assert_eq!(x.unwrap(), 1);
1392        let x: Result<UnsafeCell<usize>, _> = try_transmute!(1usize);
1393        assert_eq!(x.unwrap().into_inner(), 1);
1394        let x: Result<UnsafeCell<isize>, _> = try_transmute!(UnsafeCell::new(1usize));
1395        assert_eq!(x.unwrap().into_inner(), 1);
1396
1397        #[derive(FromBytes, IntoBytes, Debug, PartialEq)]
1398        #[repr(transparent)]
1399        struct PanicOnDrop<T>(T);
1400
1401        impl<T> Drop for PanicOnDrop<T> {
1402            fn drop(&mut self) {
1403                panic!("PanicOnDrop dropped");
1404            }
1405        }
1406
1407        // Since `try_transmute!` semantically moves its argument on failure,
1408        // the `PanicOnDrop` is not dropped, and thus this shouldn't panic.
1409        let x: Result<usize, _> = try_transmute!(PanicOnDrop(1usize));
1410        assert_eq!(x, Ok(1));
1411
1412        // Since `try_transmute!` semantically returns ownership of its argument
1413        // on failure, the `PanicOnDrop` is returned rather than dropped, and
1414        // thus this shouldn't panic.
1415        let y: Result<bool, _> = try_transmute!(PanicOnDrop(2u8));
1416        // We have to use `map_err` instead of comparing against
1417        // `Err(PanicOnDrop(2u8))` because the latter would create and then drop
1418        // its `PanicOnDrop` temporary, which would cause a panic.
1419        assert_eq!(y.as_ref().map_err(|p| &p.src.0), Err::<&bool, _>(&2u8));
1420        mem::forget(y);
1421    }
1422
1423    #[test]
1424    fn test_try_transmute_ref() {
1425        // Test that memory is transmuted with `try_transmute_ref` as expected.
1426        let array_of_bools = &[false, true, false, true, false, true, false, true];
1427        let array_of_arrays = &[[0, 1], [0, 1], [0, 1], [0, 1]];
1428        let x: Result<&[[u8; 2]; 4], _> = try_transmute_ref!(array_of_bools);
1429        assert_eq!(x, Ok(array_of_arrays));
1430        let x: Result<&[bool; 8], _> = try_transmute_ref!(array_of_arrays);
1431        assert_eq!(x, Ok(array_of_bools));
1432
1433        // Test that it's legal to transmute a reference while shrinking the
1434        // lifetime.
1435        {
1436            let x: Result<&[[u8; 2]; 4], _> = try_transmute_ref!(array_of_bools);
1437            assert_eq!(x, Ok(array_of_arrays));
1438        }
1439
1440        // Test that `try_transmute_ref!` supports decreasing alignment.
1441        let u = AU64(0);
1442        let array = [0u8, 0, 0, 0, 0, 0, 0, 0];
1443        let x: Result<&[u8; 8], _> = try_transmute_ref!(&u);
1444        assert_eq!(x, Ok(&array));
1445
1446        // Test that a mutable reference can be turned into an immutable one.
1447        let mut x = 0u8;
1448        #[allow(clippy::useless_transmute)]
1449        let y: Result<&u8, _> = try_transmute_ref!(&mut x);
1450        assert_eq!(y, Ok(&0));
1451
1452        // Test that sized types work which don't implement `KnownLayout`.
1453        let array_of_nkl_u8s = Nkl([0u8, 1, 2, 3, 4, 5, 6, 7]);
1454        let array_of_nkl_arrays = Nkl([[0, 1], [2, 3], [4, 5], [6, 7]]);
1455        let x: Result<&Nkl<[[u8; 2]; 4]>, _> = try_transmute_ref!(&array_of_nkl_u8s);
1456        assert_eq!(x, Ok(&array_of_nkl_arrays));
1457
1458        // Test sized -> unsized transmutation.
1459        let array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
1460        let array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
1461        let slice_of_arrays = &array_of_arrays[..];
1462        let x: Result<&[[u8; 2]], _> = try_transmute_ref!(&array_of_u8s);
1463        assert_eq!(x, Ok(slice_of_arrays));
1464
1465        // Test unsized -> unsized transmutation.
1466        let slice_dst_of_u8s =
1467            SliceDst::<U16, [u8; 2]>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
1468        let slice_dst_of_u16s =
1469            SliceDst::<U16, U16>::ref_from_bytes(&[0, 1, 2, 3, 4, 5][..]).unwrap();
1470        let x: Result<&SliceDst<U16, U16>, _> = try_transmute_ref!(slice_dst_of_u8s);
1471        assert_eq!(x, Ok(slice_dst_of_u16s));
1472    }
1473
1474    #[test]
1475    fn test_try_transmute_mut() {
1476        // Test that memory is transmuted with `try_transmute_mut` as expected.
1477        let array_of_u8s = &mut [0u8, 1, 0, 1, 0, 1, 0, 1];
1478        let array_of_arrays = &mut [[0u8, 1], [0, 1], [0, 1], [0, 1]];
1479        let x: Result<&mut [[u8; 2]; 4], _> = try_transmute_mut!(array_of_u8s);
1480        assert_eq!(x, Ok(array_of_arrays));
1481
1482        let array_of_bools = &mut [false, true, false, true, false, true, false, true];
1483        let array_of_arrays = &mut [[0u8, 1], [0, 1], [0, 1], [0, 1]];
1484        let x: Result<&mut [bool; 8], _> = try_transmute_mut!(array_of_arrays);
1485        assert_eq!(x, Ok(array_of_bools));
1486
1487        // Test that it's legal to transmute a reference while shrinking the
1488        // lifetime.
1489        let array_of_bools = &mut [false, true, false, true, false, true, false, true];
1490        let array_of_arrays = &mut [[0u8, 1], [0, 1], [0, 1], [0, 1]];
1491        {
1492            let x: Result<&mut [bool; 8], _> = try_transmute_mut!(array_of_arrays);
1493            assert_eq!(x, Ok(array_of_bools));
1494        }
1495
1496        // Test that `try_transmute_mut!` supports decreasing alignment.
1497        let u = &mut AU64(0);
1498        let array = &mut [0u8, 0, 0, 0, 0, 0, 0, 0];
1499        let x: Result<&mut [u8; 8], _> = try_transmute_mut!(u);
1500        assert_eq!(x, Ok(array));
1501
1502        // Test that a mutable reference can be turned into an immutable one.
1503        let mut x = 0u8;
1504        #[allow(clippy::useless_transmute)]
1505        let y: Result<&mut u8, _> = try_transmute_mut!(&mut x);
1506        assert_eq!(y, Ok(&mut 0));
1507
1508        // Test that sized types work which don't implement `KnownLayout`.
1509        let mut array_of_nkl_u8s = Nkl([0u8, 1, 2, 3, 4, 5, 6, 7]);
1510        let mut array_of_nkl_arrays = Nkl([[0, 1], [2, 3], [4, 5], [6, 7]]);
1511        let x: Result<&mut Nkl<[[u8; 2]; 4]>, _> = try_transmute_mut!(&mut array_of_nkl_u8s);
1512        assert_eq!(x, Ok(&mut array_of_nkl_arrays));
1513
1514        // Test sized -> unsized transmutation.
1515        let mut array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
1516        let mut array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
1517        let slice_of_arrays = &mut array_of_arrays[..];
1518        let x: Result<&mut [[u8; 2]], _> = try_transmute_mut!(&mut array_of_u8s);
1519        assert_eq!(x, Ok(slice_of_arrays));
1520
1521        // Test unsized -> unsized transmutation.
1522        let mut bytes = [0, 1, 2, 3, 4, 5, 6];
1523        let slice_dst_of_u8s = SliceDst::<u8, [u8; 2]>::mut_from_bytes(&mut bytes[..]).unwrap();
1524        let mut bytes = [0, 1, 2, 3, 4, 5, 6];
1525        let slice_dst_of_u16s = SliceDst::<u8, U16>::mut_from_bytes(&mut bytes[..]).unwrap();
1526        let x: Result<&mut SliceDst<u8, U16>, _> = try_transmute_mut!(slice_dst_of_u8s);
1527        assert_eq!(x, Ok(slice_dst_of_u16s));
1528    }
1529
1530    #[test]
1531    fn test_transmute_mut() {
1532        // Test that memory is transmuted as expected.
1533        let mut array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
1534        let mut array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
1535        let x: &mut [[u8; 2]; 4] = transmute_mut!(&mut array_of_u8s);
1536        assert_eq!(*x, array_of_arrays);
1537        let x: &mut [u8; 8] = transmute_mut!(&mut array_of_arrays);
1538        assert_eq!(*x, array_of_u8s);
1539
1540        {
1541            // Test that it's legal to transmute a reference while shrinking the
1542            // lifetime.
1543            let x: &mut [u8; 8] = transmute_mut!(&mut array_of_arrays);
1544            assert_eq!(*x, array_of_u8s);
1545        }
1546
1547        // Test that `transmute_mut!` supports non-`KnownLayout` types.
1548        let mut array_of_u8s = Nkl([0u8, 1, 2, 3, 4, 5, 6, 7]);
1549        let mut array_of_arrays = Nkl([[0, 1], [2, 3], [4, 5], [6, 7]]);
1550        let x: &mut Nkl<[[u8; 2]; 4]> = transmute_mut!(&mut array_of_u8s);
1551        assert_eq!(*x, array_of_arrays);
1552        let x: &mut Nkl<[u8; 8]> = transmute_mut!(&mut array_of_arrays);
1553        assert_eq!(*x, array_of_u8s);
1554
1555        // Test that `transmute_mut!` supports decreasing alignment.
1556        let mut u = AU64(0);
1557        let array = [0, 0, 0, 0, 0, 0, 0, 0];
1558        let x: &[u8; 8] = transmute_mut!(&mut u);
1559        assert_eq!(*x, array);
1560
1561        // Test that a mutable reference can be turned into an immutable one.
1562        let mut x = 0u8;
1563        #[allow(clippy::useless_transmute)]
1564        let y: &u8 = transmute_mut!(&mut x);
1565        assert_eq!(*y, 0);
1566
1567        // Test that `transmute_mut!` works on slice DSTs in and that memory is
1568        // transmuted as expected.
1569        let mut bytes = [0, 1, 2, 3, 4, 5, 6];
1570        let slice_dst_of_u8s = SliceDst::<u8, [u8; 2]>::mut_from_bytes(&mut bytes[..]).unwrap();
1571        let mut bytes = [0, 1, 2, 3, 4, 5, 6];
1572        let slice_dst_of_u16s = SliceDst::<u8, U16>::mut_from_bytes(&mut bytes[..]).unwrap();
1573        let x: &mut SliceDst<u8, U16> = transmute_mut!(slice_dst_of_u8s);
1574        assert_eq!(x, slice_dst_of_u16s);
1575
1576        // Test that `transmute_mut!` works on slices that memory is transmuted
1577        // as expected.
1578        let array_of_u16s: &mut [u16] = &mut [0u16, 1, 2];
1579        let array_of_i16s: &mut [i16] = &mut [0i16, 1, 2];
1580        let x: &mut [i16] = transmute_mut!(array_of_u16s);
1581        assert_eq!(x, array_of_i16s);
1582
1583        // Test that transmuting from a type with larger trailing slice offset
1584        // and larger trailing slice element works.
1585        let mut bytes = [0, 1, 2, 3, 4, 5, 6, 7];
1586        let slice_dst_big = SliceDst::<U32, U16>::mut_from_bytes(&mut bytes[..]).unwrap();
1587        let mut bytes = [0, 1, 2, 3, 4, 5, 6, 7];
1588        let slice_dst_small = SliceDst::<U16, u8>::mut_from_bytes(&mut bytes[..]).unwrap();
1589        let x: &mut SliceDst<U16, u8> = transmute_mut!(slice_dst_big);
1590        assert_eq!(x, slice_dst_small);
1591
1592        // Test sized -> unsized transmutation.
1593        let mut array_of_u8s = [0u8, 1, 2, 3, 4, 5, 6, 7];
1594        let mut array_of_arrays = [[0, 1], [2, 3], [4, 5], [6, 7]];
1595        let slice_of_arrays = &mut array_of_arrays[..];
1596        let x: &mut [[u8; 2]] = transmute_mut!(&mut array_of_u8s);
1597        assert_eq!(x, slice_of_arrays);
1598    }
1599
1600    #[test]
1601    fn test_macros_evaluate_args_once() {
1602        let mut ctr = 0;
1603        #[allow(clippy::useless_transmute)]
1604        let _: usize = transmute!({
1605            ctr += 1;
1606            0usize
1607        });
1608        assert_eq!(ctr, 1);
1609
1610        let mut ctr = 0;
1611        let _: &usize = transmute_ref!({
1612            ctr += 1;
1613            &0usize
1614        });
1615        assert_eq!(ctr, 1);
1616
1617        let mut ctr: usize = 0;
1618        let _: &mut usize = transmute_mut!({
1619            ctr += 1;
1620            &mut ctr
1621        });
1622        assert_eq!(ctr, 1);
1623
1624        let mut ctr = 0;
1625        #[allow(clippy::useless_transmute)]
1626        let _: usize = try_transmute!({
1627            ctr += 1;
1628            0usize
1629        })
1630        .unwrap();
1631        assert_eq!(ctr, 1);
1632    }
1633
1634    #[test]
1635    fn test_include_value() {
1636        const AS_U32: u32 = include_value!("../testdata/include_value/data");
1637        assert_eq!(AS_U32, u32::from_ne_bytes([b'a', b'b', b'c', b'd']));
1638        const AS_I32: i32 = include_value!("../testdata/include_value/data");
1639        assert_eq!(AS_I32, i32::from_ne_bytes([b'a', b'b', b'c', b'd']));
1640    }
1641
1642    #[test]
1643    #[allow(non_camel_case_types, unreachable_pub, dead_code)]
1644    fn test_cryptocorrosion_derive_traits() {
1645        // Test the set of invocations added in
1646        // https://github.com/cryptocorrosion/cryptocorrosion/pull/85
1647
1648        fn assert_impls<T: FromBytes + IntoBytes + Immutable>() {}
1649
1650        cryptocorrosion_derive_traits! {
1651            #[repr(C)]
1652            #[derive(Clone, Copy)]
1653            pub union vec128_storage {
1654                d: [u32; 4],
1655                q: [u64; 2],
1656            }
1657        }
1658
1659        assert_impls::<vec128_storage>();
1660
1661        cryptocorrosion_derive_traits! {
1662            #[repr(transparent)]
1663            #[derive(Copy, Clone, Debug, PartialEq)]
1664            pub struct u32x4_generic([u32; 4]);
1665        }
1666
1667        assert_impls::<u32x4_generic>();
1668
1669        cryptocorrosion_derive_traits! {
1670            #[repr(transparent)]
1671            #[derive(Copy, Clone, Debug, PartialEq)]
1672            pub struct u64x2_generic([u64; 2]);
1673        }
1674
1675        assert_impls::<u64x2_generic>();
1676
1677        cryptocorrosion_derive_traits! {
1678            #[repr(transparent)]
1679            #[derive(Copy, Clone, Debug, PartialEq)]
1680            pub struct u128x1_generic([u128; 1]);
1681        }
1682
1683        assert_impls::<u128x1_generic>();
1684
1685        cryptocorrosion_derive_traits! {
1686            #[repr(transparent)]
1687            #[derive(Copy, Clone, Default)]
1688            #[allow(non_camel_case_types)]
1689            pub struct x2<W, G>(pub [W; 2], PhantomData<G>);
1690        }
1691
1692        enum NotZerocopy {}
1693        assert_impls::<x2<(), NotZerocopy>>();
1694
1695        cryptocorrosion_derive_traits! {
1696            #[repr(transparent)]
1697            #[derive(Copy, Clone, Default)]
1698            #[allow(non_camel_case_types)]
1699            pub struct x4<W>(pub [W; 4]);
1700        }
1701
1702        assert_impls::<x4<()>>();
1703
1704        #[cfg(feature = "simd")]
1705        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1706        {
1707            #[cfg(target_arch = "x86")]
1708            use core::arch::x86::{__m128i, __m256i};
1709            #[cfg(target_arch = "x86_64")]
1710            use core::arch::x86_64::{__m128i, __m256i};
1711
1712            cryptocorrosion_derive_traits! {
1713                #[repr(C)]
1714                #[derive(Copy, Clone)]
1715                pub struct X4(__m128i, __m128i, __m128i, __m128i);
1716            }
1717
1718            assert_impls::<X4>();
1719
1720            cryptocorrosion_derive_traits! {
1721                #[repr(C)]
1722                /// Generic wrapper for unparameterized storage of any of the
1723                /// possible impls. Converting into and out of this type should
1724                /// be essentially free, although it may be more aligned than a
1725                /// particular impl requires.
1726                #[allow(non_camel_case_types)]
1727                #[derive(Copy, Clone)]
1728                pub union vec128_storage {
1729                    u32x4: [u32; 4],
1730                    u64x2: [u64; 2],
1731                    u128x1: [u128; 1],
1732                    sse2: __m128i,
1733                }
1734            }
1735
1736            assert_impls::<vec128_storage>();
1737
1738            cryptocorrosion_derive_traits! {
1739                #[repr(transparent)]
1740                #[allow(non_camel_case_types)]
1741                #[derive(Copy, Clone)]
1742                pub struct vec<S3, S4, NI> {
1743                    x: __m128i,
1744                    s3: PhantomData<S3>,
1745                    s4: PhantomData<S4>,
1746                    ni: PhantomData<NI>,
1747                }
1748            }
1749
1750            assert_impls::<vec<NotZerocopy, NotZerocopy, NotZerocopy>>();
1751
1752            cryptocorrosion_derive_traits! {
1753                #[repr(transparent)]
1754                #[derive(Copy, Clone)]
1755                pub struct u32x4x2_avx2<NI> {
1756                    x: __m256i,
1757                    ni: PhantomData<NI>,
1758                }
1759            }
1760
1761            assert_impls::<u32x4x2_avx2<NotZerocopy>>();
1762        }
1763
1764        // Make sure that our derive works for `#[repr(C)]` structs even though
1765        // cryptocorrosion doesn't currently have any.
1766        cryptocorrosion_derive_traits! {
1767            #[repr(C)]
1768            #[derive(Copy, Clone, Debug, PartialEq)]
1769            pub struct ReprC(u8, u8, u16);
1770        }
1771    }
1772}