zerocopy/util/
macros.rs

1// Copyright 2023 The Fuchsia Authors
2//
3// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except according to
7// those terms.
8
9/// Documents multiple unsafe blocks with a single safety comment.
10///
11/// Invoked as:
12///
13/// ```rust,ignore
14/// safety_comment! {
15///     // Non-doc comments come first.
16///     /// SAFETY:
17///     /// Safety comment starts on its own line.
18///     macro_1!(args);
19///     macro_2! { args };
20///     /// SAFETY:
21///     /// Subsequent safety comments are allowed but not required.
22///     macro_3! { args };
23/// }
24/// ```
25///
26/// The macro invocations are emitted, each decorated with the following
27/// attribute: `#[allow(clippy::undocumented_unsafe_blocks)]`.
28macro_rules! safety_comment {
29    (#[doc = r" SAFETY:"] $($(#[$attr:meta])* $macro:ident!$args:tt;)*) => {
30        #[allow(clippy::undocumented_unsafe_blocks, unused_attributes)]
31        const _: () = { $($(#[$attr])* $macro!$args;)* };
32    }
33}
34
35/// Unsafely implements trait(s) for a type.
36///
37/// # Safety
38///
39/// The trait impl must be sound.
40///
41/// When implementing `TryFromBytes`:
42/// - If no `is_bit_valid` impl is provided, then it must be valid for
43///   `is_bit_valid` to unconditionally return `true`. In other words, it must
44///   be the case that any initialized sequence of bytes constitutes a valid
45///   instance of `$ty`.
46/// - If an `is_bit_valid` impl is provided, then:
47///   - Regardless of whether the provided closure takes a `Ptr<$repr>` or
48///     `&$repr` argument, if `$ty` and `$repr` are different types, then it
49///     must be the case that, given `t: *mut $ty` and `let r = t as *mut
50///     $repr`:
51///     - `r` refers to an object of equal or lesser size than the object
52///       referred to by `t`.
53///     - `r` refers to an object with `UnsafeCell`s at the same byte ranges as
54///       the object referred to by `t`.
55///   - If the provided closure takes a `&$repr` argument, then given a `Ptr<'a,
56///     $ty>` which satisfies the preconditions of
57///     `TryFromBytes::<$ty>::is_bit_valid`, it must be guaranteed that the
58///     memory referenced by that `Ptr` always contains a valid `$repr`.
59///   - The impl of `is_bit_valid` must only return `true` for its argument
60///     `Ptr<$repr>` if the original `Ptr<$ty>` refers to a valid `$ty`.
61macro_rules! unsafe_impl {
62    // Implement `$trait` for `$ty` with no bounds.
63    ($(#[$attr:meta])* $ty:ty: $trait:ident $(; |$candidate:ident: MaybeAligned<$repr:ty>| $is_bit_valid:expr)?) => {
64        $(#[$attr])*
65        unsafe impl $trait for $ty {
66            unsafe_impl!(@method $trait $(; |$candidate: MaybeAligned<$repr>| $is_bit_valid)?);
67        }
68    };
69    // Implement all `$traits` for `$ty` with no bounds.
70    ($ty:ty: $($traits:ident),*) => {
71        $( unsafe_impl!($ty: $traits); )*
72    };
73    // This arm is identical to the following one, except it contains a
74    // preceding `const`. If we attempt to handle these with a single arm, there
75    // is an inherent ambiguity between `const` (the keyword) and `const` (the
76    // ident match for `$tyvar:ident`).
77    //
78    // To explain how this works, consider the following invocation:
79    //
80    //   unsafe_impl!(const N: usize, T: ?Sized + Copy => Clone for Foo<T>);
81    //
82    // In this invocation, here are the assignments to meta-variables:
83    //
84    //   |---------------|------------|
85    //   | Meta-variable | Assignment |
86    //   |---------------|------------|
87    //   | $constname    |  N         |
88    //   | $constty      |  usize     |
89    //   | $tyvar        |  T         |
90    //   | $optbound     |  Sized     |
91    //   | $bound        |  Copy      |
92    //   | $trait        |  Clone     |
93    //   | $ty           |  Foo<T>    |
94    //   |---------------|------------|
95    //
96    // The following arm has the same behavior with the exception of the lack of
97    // support for a leading `const` parameter.
98    (
99        $(#[$attr:meta])*
100        const $constname:ident : $constty:ident $(,)?
101        $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),*
102        => $trait:ident for $ty:ty $(; |$candidate:ident $(: MaybeAligned<$ref_repr:ty>)? $(: Maybe<$ptr_repr:ty>)?| $is_bit_valid:expr)?
103    ) => {
104        unsafe_impl!(
105            @inner
106            $(#[$attr])*
107            @const $constname: $constty,
108            $($tyvar $(: $(? $optbound +)* + $($bound +)*)?,)*
109            => $trait for $ty $(; |$candidate $(: MaybeAligned<$ref_repr>)? $(: Maybe<$ptr_repr>)?| $is_bit_valid)?
110        );
111    };
112    (
113        $(#[$attr:meta])*
114        $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),*
115        => $trait:ident for $ty:ty $(; |$candidate:ident $(: MaybeAligned<$ref_repr:ty>)? $(: Maybe<$ptr_repr:ty>)?| $is_bit_valid:expr)?
116    ) => {
117        unsafe_impl!(
118            @inner
119            $(#[$attr])*
120            $($tyvar $(: $(? $optbound +)* + $($bound +)*)?,)*
121            => $trait for $ty $(; |$candidate $(: MaybeAligned<$ref_repr>)? $(: Maybe<$ptr_repr>)?| $is_bit_valid)?
122        );
123    };
124    (
125        @inner
126        $(#[$attr:meta])*
127        $(@const $constname:ident : $constty:ident,)*
128        $($tyvar:ident $(: $(? $optbound:ident +)* + $($bound:ident +)* )?,)*
129        => $trait:ident for $ty:ty $(; |$candidate:ident $(: MaybeAligned<$ref_repr:ty>)? $(: Maybe<$ptr_repr:ty>)?| $is_bit_valid:expr)?
130    ) => {
131        $(#[$attr])*
132        #[allow(non_local_definitions)]
133        unsafe impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?),* $(, const $constname: $constty,)*> $trait for $ty {
134            unsafe_impl!(@method $trait $(; |$candidate: $(MaybeAligned<$ref_repr>)? $(Maybe<$ptr_repr>)?| $is_bit_valid)?);
135        }
136    };
137
138    (@method TryFromBytes ; |$candidate:ident: MaybeAligned<$repr:ty>| $is_bit_valid:expr) => {
139        #[allow(clippy::missing_inline_in_public_items)]
140        #[cfg_attr(coverage_nightly, coverage(off))]
141        fn only_derive_is_allowed_to_implement_this_trait() {}
142
143        #[inline]
144        fn is_bit_valid<AA: invariant::Aliasing + invariant::AtLeast<invariant::Shared>>(candidate: Maybe<'_, Self, AA>) -> bool {
145            // SAFETY:
146            // - The cast preserves address. The caller has promised that the
147            //   cast results in an object of equal or lesser size, and so the
148            //   cast returns a pointer which references a subset of the bytes
149            //   of `p`.
150            // - The cast preserves provenance.
151            // - The caller has promised that the destination type has
152            //   `UnsafeCell`s at the same byte ranges as the source type.
153            #[allow(clippy::as_conversions)]
154            let candidate = unsafe { candidate.cast_unsized::<$repr, _>(|p| p as *mut _) };
155
156            // SAFETY: The caller has promised that the referenced memory region
157            // will contain a valid `$repr`.
158            let $candidate = unsafe { candidate.assume_validity::<crate::pointer::invariant::Valid>() };
159            $is_bit_valid
160        }
161    };
162    (@method TryFromBytes ; |$candidate:ident: Maybe<$repr:ty>| $is_bit_valid:expr) => {
163        #[allow(clippy::missing_inline_in_public_items)]
164        #[cfg_attr(coverage_nightly, coverage(off))]
165        fn only_derive_is_allowed_to_implement_this_trait() {}
166
167        #[inline]
168        fn is_bit_valid<AA: invariant::Aliasing + invariant::AtLeast<invariant::Shared>>(candidate: Maybe<'_, Self, AA>) -> bool {
169            // SAFETY:
170            // - The cast preserves address. The caller has promised that the
171            //   cast results in an object of equal or lesser size, and so the
172            //   cast returns a pointer which references a subset of the bytes
173            //   of `p`.
174            // - The cast preserves provenance.
175            // - The caller has promised that the destination type has
176            //   `UnsafeCell`s at the same byte ranges as the source type.
177            #[allow(clippy::as_conversions)]
178            let $candidate = unsafe { candidate.cast_unsized::<$repr, _>(|p| p as *mut _) };
179
180            // Restore the invariant that the referent bytes are initialized.
181            // SAFETY: The above cast does not uninitialize any referent bytes;
182            // they remain initialized.
183            let $candidate = unsafe { $candidate.assume_validity::<crate::pointer::invariant::Initialized>() };
184
185            $is_bit_valid
186        }
187    };
188    (@method TryFromBytes) => {
189        #[allow(clippy::missing_inline_in_public_items)]
190        #[cfg_attr(coverage_nightly, coverage(off))]
191        fn only_derive_is_allowed_to_implement_this_trait() {}
192        #[inline(always)] fn is_bit_valid<A: invariant::Aliasing + invariant::AtLeast<invariant::Shared>>(_: Maybe<'_, Self, A>) -> bool { true }
193    };
194    (@method $trait:ident) => {
195        #[allow(clippy::missing_inline_in_public_items)]
196        #[cfg_attr(coverage_nightly, coverage(off))]
197        fn only_derive_is_allowed_to_implement_this_trait() {}
198    };
199    (@method $trait:ident; |$_candidate:ident $(: &$_ref_repr:ty)? $(: NonNull<$_ptr_repr:ty>)?| $_is_bit_valid:expr) => {
200        compile_error!("Can't provide `is_bit_valid` impl for trait other than `TryFromBytes`");
201    };
202}
203
204/// Implements `$trait` for a type which implements `TransparentWrapper`.
205///
206/// Calling this macro is safe; the internals of the macro emit appropriate
207/// trait bounds which ensure that the given impl is sound.
208macro_rules! impl_for_transparent_wrapper {
209    (
210        $(#[$attr:meta])*
211        $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?)?
212        => $trait:ident for $ty:ty $(; |$candidate:ident $(: MaybeAligned<$ref_repr:ty>)? $(: Maybe<$ptr_repr:ty>)?| $is_bit_valid:expr)?
213    ) => {
214        $(#[$attr])*
215        #[allow(non_local_definitions)]
216
217        // This block implements `$trait` for `$ty` under the following
218        // conditions:
219        // - `$ty: TransparentWrapper`
220        // - `$ty::Inner: $trait`
221        // - For some `Xxx`, `$ty::XxxVariance = Covariant` (`Xxx` is determined
222        //   by the `@define_is_transparent_wrapper` macro arms). This bound
223        //   ensures that some layout property is the same between `$ty` and
224        //   `$ty::Inner`. Which layout property this is depends on the trait
225        //   being implemented (for example, `FromBytes` is not concerned with
226        //   alignment, but is concerned with bit validity).
227        //
228        // In other words, `$ty` is guaranteed to soundly implement `$trait`
229        // because some property of its layout is the same as `$ty::Inner`,
230        // which implements `$trait`. Most of the complexity in this macro is to
231        // ensure that the above-mentioned conditions are actually met, and that
232        // the proper variance (ie, the proper layout property) is chosen.
233
234        // SAFETY:
235        // - `is_transparent_wrapper<I, W>` requires:
236        //   - `W: TransparentWrapper<I>`
237        //   - `W::Inner: $trait`
238        // - `f` is generic over `I: Invariants`, and in its body, calls
239        //   `is_transparent_wrapper::<I, $ty>()`. Thus, this code will only
240        //   compile if, for all `I: Invariants`:
241        //   - `$ty: TransparentWrapper<I>`
242        //   - `$ty::Inner: $trait`
243        //
244        // These two facts - that `$ty: TransparentWrapper<I>` and that
245        // `$ty::Inner: $trait` - are the preconditions to the full safety
246        // proofs, which are completed below in the
247        // `@define_is_transparent_wrapper` macro arms. The safety proof is
248        // slightly different for each trait.
249        unsafe impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?)?> $trait for $ty {
250            #[allow(dead_code, clippy::missing_inline_in_public_items)]
251            #[cfg_attr(coverage_nightly, coverage(off))]
252            fn only_derive_is_allowed_to_implement_this_trait() {
253                use crate::{pointer::invariant::Invariants, util::*};
254
255                impl_for_transparent_wrapper!(@define_is_transparent_wrapper $trait);
256
257                #[cfg_attr(coverage_nightly, coverage(off))]
258                fn f<I: Invariants, $($tyvar $(: $(? $optbound +)* $($bound +)*)?)?>() {
259                    is_transparent_wrapper::<I, $ty>();
260                }
261            }
262
263            impl_for_transparent_wrapper!(
264                @is_bit_valid
265                $(<$tyvar $(: $(? $optbound +)* $($bound +)*)?>)?
266                $trait for $ty
267            );
268        }
269    };
270    (@define_is_transparent_wrapper Immutable) => {
271        // SAFETY: `W: TransparentWrapper<UnsafeCellVariance = Covariant>`
272        // requires that `W` has `UnsafeCell`s at the same byte offsets as
273        // `W::Inner`. `W::Inner: Immutable` implies that `W::Inner` does not
274        // contain any `UnsafeCell`s, and so `W` does not contain any
275        // `UnsafeCell`s. Since `W = $ty`, `$ty` can soundly implement
276        // `Immutable`.
277        impl_for_transparent_wrapper!(@define_is_transparent_wrapper Immutable, UnsafeCellVariance)
278    };
279    (@define_is_transparent_wrapper FromZeros) => {
280        // SAFETY: `W: TransparentWrapper<ValidityVariance = Covariant>`
281        // requires that `W` has the same bit validity as `W::Inner`. `W::Inner:
282        // FromZeros` implies that the all-zeros bit pattern is a bit-valid
283        // instance of `W::Inner`, and so the all-zeros bit pattern is a
284        // bit-valid instance of `W`. Since `W = $ty`, `$ty` can soundly
285        // implement `FromZeros`.
286        impl_for_transparent_wrapper!(@define_is_transparent_wrapper FromZeros, ValidityVariance)
287    };
288    (@define_is_transparent_wrapper FromBytes) => {
289        // SAFETY: `W: TransparentWrapper<ValidityVariance = Covariant>`
290        // requires that `W` has the same bit validity as `W::Inner`. `W::Inner:
291        // FromBytes` implies that any initialized bit pattern is a bit-valid
292        // instance of `W::Inner`, and so any initialized bit pattern is a
293        // bit-valid instance of `W`. Since `W = $ty`, `$ty` can soundly
294        // implement `FromBytes`.
295        impl_for_transparent_wrapper!(@define_is_transparent_wrapper FromBytes, ValidityVariance)
296    };
297    (@define_is_transparent_wrapper IntoBytes) => {
298        // SAFETY: `W: TransparentWrapper<ValidityVariance = Covariant>`
299        // requires that `W` has the same bit validity as `W::Inner`. `W::Inner:
300        // IntoBytes` implies that no bit-valid instance of `W::Inner` contains
301        // uninitialized bytes, and so no bit-valid instance of `W` contains
302        // uninitialized bytes. Since `W = $ty`, `$ty` can soundly implement
303        // `IntoBytes`.
304        impl_for_transparent_wrapper!(@define_is_transparent_wrapper IntoBytes, ValidityVariance)
305    };
306    (@define_is_transparent_wrapper Unaligned) => {
307        // SAFETY: `W: TransparentWrapper<AlignmentVariance = Covariant>`
308        // requires that `W` has the same alignment as `W::Inner`. `W::Inner:
309        // Unaligned` implies `W::Inner`'s alignment is 1, and so `W`'s
310        // alignment is 1. Since `W = $ty`, `W` can soundly implement
311        // `Unaligned`.
312        impl_for_transparent_wrapper!(@define_is_transparent_wrapper Unaligned, AlignmentVariance)
313    };
314    (@define_is_transparent_wrapper TryFromBytes) => {
315        // SAFETY: `W: TransparentWrapper<ValidityVariance = Covariant>`
316        // requires that `W` has the same bit validity as `W::Inner`. `W::Inner:
317        // TryFromBytes` implies that `<W::Inner as
318        // TryFromBytes>::is_bit_valid(c)` only returns `true` if `c` references
319        // a bit-valid instance of `W::Inner`. Thus, `<W::Inner as
320        // TryFromBytes>::is_bit_valid(c)` only returns `true` if `c` references
321        // a bit-valid instance of `W`. Below, we implement `<W as
322        // TryFromBytes>::is_bit_valid` by deferring to `<W::Inner as
323        // TryFromBytes>::is_bit_valid`. Since `W = $ty`, it is sound for `$ty`
324        // to implement `TryFromBytes` with this implementation of
325        // `is_bit_valid`.
326        impl_for_transparent_wrapper!(@define_is_transparent_wrapper TryFromBytes, ValidityVariance)
327    };
328    (@define_is_transparent_wrapper $trait:ident, $variance:ident) => {
329        #[cfg_attr(coverage_nightly, coverage(off))]
330        fn is_transparent_wrapper<I: Invariants, W: TransparentWrapper<I, $variance = Covariant> + ?Sized>()
331        where
332            W::Inner: $trait,
333        {}
334    };
335    (
336        @is_bit_valid
337        $(<$tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?>)?
338        TryFromBytes for $ty:ty
339    ) => {
340        // SAFETY: See safety comment in `(@define_is_transparent_wrapper
341        // TryFromBytes)` macro arm for an explanation of why this is a sound
342        // implementation of `is_bit_valid`.
343        #[inline]
344        fn is_bit_valid<A: crate::pointer::invariant::Aliasing + crate::pointer::invariant::AtLeast<invariant::Shared>>(candidate: Maybe<'_, Self, A>) -> bool {
345            TryFromBytes::is_bit_valid(candidate.transparent_wrapper_into_inner())
346        }
347    };
348    (
349        @is_bit_valid
350        $(<$tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?>)?
351        $trait:ident for $ty:ty
352    ) => {
353        // Trait other than `TryFromBytes`; no `is_bit_valid` impl.
354    };
355}
356
357/// Implements a trait for a type, bounding on each memeber of the power set of
358/// a set of type variables. This is useful for implementing traits for tuples
359/// or `fn` types.
360///
361/// The last argument is the name of a macro which will be called in every
362/// `impl` block, and is expected to expand to the name of the type for which to
363/// implement the trait.
364///
365/// For example, the invocation:
366/// ```ignore
367/// unsafe_impl_for_power_set!(A, B => Foo for type!(...))
368/// ```
369/// ...expands to:
370/// ```ignore
371/// unsafe impl       Foo for type!()     { ... }
372/// unsafe impl<B>    Foo for type!(B)    { ... }
373/// unsafe impl<A, B> Foo for type!(A, B) { ... }
374/// ```
375macro_rules! unsafe_impl_for_power_set {
376    (
377        $first:ident $(, $rest:ident)* $(-> $ret:ident)? => $trait:ident for $macro:ident!(...)
378        $(; |$candidate:ident $(: MaybeAligned<$ref_repr:ty>)? $(: Maybe<$ptr_repr:ty>)?| $is_bit_valid:expr)?
379    ) => {
380        unsafe_impl_for_power_set!(
381            $($rest),* $(-> $ret)? => $trait for $macro!(...)
382            $(; |$candidate $(: MaybeAligned<$ref_repr>)? $(: Maybe<$ptr_repr>)?| $is_bit_valid)?
383        );
384        unsafe_impl_for_power_set!(
385            @impl $first $(, $rest)* $(-> $ret)? => $trait for $macro!(...)
386            $(; |$candidate $(: MaybeAligned<$ref_repr>)? $(: Maybe<$ptr_repr>)?| $is_bit_valid)?
387        );
388    };
389    (
390        $(-> $ret:ident)? => $trait:ident for $macro:ident!(...)
391        $(; |$candidate:ident $(: MaybeAligned<$ref_repr:ty>)? $(: Maybe<$ptr_repr:ty>)?| $is_bit_valid:expr)?
392    ) => {
393        unsafe_impl_for_power_set!(
394            @impl $(-> $ret)? => $trait for $macro!(...)
395            $(; |$candidate $(: MaybeAligned<$ref_repr>)? $(: Maybe<$ptr_repr>)?| $is_bit_valid)?
396        );
397    };
398    (
399        @impl $($vars:ident),* $(-> $ret:ident)? => $trait:ident for $macro:ident!(...)
400        $(; |$candidate:ident $(: MaybeAligned<$ref_repr:ty>)? $(: Maybe<$ptr_repr:ty>)?| $is_bit_valid:expr)?
401    ) => {
402        unsafe_impl!(
403            $($vars,)* $($ret)? => $trait for $macro!($($vars),* $(-> $ret)?)
404            $(; |$candidate $(: MaybeAligned<$ref_repr>)? $(: Maybe<$ptr_repr>)?| $is_bit_valid)?
405        );
406    };
407}
408
409/// Expands to an `Option<extern "C" fn>` type with the given argument types and
410/// return type. Designed for use with `unsafe_impl_for_power_set`.
411macro_rules! opt_extern_c_fn {
412    ($($args:ident),* -> $ret:ident) => { Option<extern "C" fn($($args),*) -> $ret> };
413}
414
415/// Expands to a `Option<fn>` type with the given argument types and return
416/// type. Designed for use with `unsafe_impl_for_power_set`.
417macro_rules! opt_fn {
418    ($($args:ident),* -> $ret:ident) => { Option<fn($($args),*) -> $ret> };
419}
420
421/// Implements trait(s) for a type or verifies the given implementation by
422/// referencing an existing (derived) implementation.
423///
424/// This macro exists so that we can provide zerocopy-derive as an optional
425/// dependency and still get the benefit of using its derives to validate that
426/// our trait impls are sound.
427///
428/// When compiling without `--cfg 'feature = "derive"` and without `--cfg test`,
429/// `impl_or_verify!` emits the provided trait impl. When compiling with either
430/// of those cfgs, it is expected that the type in question is deriving the
431/// traits instead. In this case, `impl_or_verify!` emits code which validates
432/// that the given trait impl is at least as restrictive as the the impl emitted
433/// by the custom derive. This has the effect of confirming that the impl which
434/// is emitted when the `derive` feature is disabled is actually sound (on the
435/// assumption that the impl emitted by the custom derive is sound).
436///
437/// The caller is still required to provide a safety comment (e.g. using the
438/// `safety_comment!` macro) . The reason for this restriction is that, while
439/// `impl_or_verify!` can guarantee that the provided impl is sound when it is
440/// compiled with the appropriate cfgs, there is no way to guarantee that it is
441/// ever compiled with those cfgs. In particular, it would be possible to
442/// accidentally place an `impl_or_verify!` call in a context that is only ever
443/// compiled when the `derive` feature is disabled. If that were to happen,
444/// there would be nothing to prevent an unsound trait impl from being emitted.
445/// Requiring a safety comment reduces the likelihood of emitting an unsound
446/// impl in this case, and also provides useful documentation for readers of the
447/// code.
448///
449/// Finally, if a `TryFromBytes::is_bit_valid` impl is provided, it must adhere
450/// to the safety preconditions of [`unsafe_impl!`].
451///
452/// ## Example
453///
454/// ```rust,ignore
455/// // Note that these derives are gated by `feature = "derive"`
456/// #[cfg_attr(any(feature = "derive", test), derive(FromZeros, FromBytes, IntoBytes, Unaligned))]
457/// #[repr(transparent)]
458/// struct Wrapper<T>(T);
459///
460/// safety_comment! {
461///     /// SAFETY:
462///     /// `Wrapper<T>` is `repr(transparent)`, so it is sound to implement any
463///     /// zerocopy trait if `T` implements that trait.
464///     impl_or_verify!(T: FromZeros => FromZeros for Wrapper<T>);
465///     impl_or_verify!(T: FromBytes => FromBytes for Wrapper<T>);
466///     impl_or_verify!(T: IntoBytes => IntoBytes for Wrapper<T>);
467///     impl_or_verify!(T: Unaligned => Unaligned for Wrapper<T>);
468/// }
469/// ```
470macro_rules! impl_or_verify {
471    // The following two match arms follow the same pattern as their
472    // counterparts in `unsafe_impl!`; see the documentation on those arms for
473    // more details.
474    (
475        const $constname:ident : $constty:ident $(,)?
476        $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),*
477        => $trait:ident for $ty:ty
478    ) => {
479        impl_or_verify!(@impl { unsafe_impl!(
480            const $constname: $constty, $($tyvar $(: $(? $optbound +)* $($bound +)*)?),* => $trait for $ty
481        ); });
482        impl_or_verify!(@verify $trait, {
483            impl<const $constname: $constty, $($tyvar $(: $(? $optbound +)* $($bound +)*)?),*> Subtrait for $ty {}
484        });
485    };
486    (
487        $($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),*
488        => $trait:ident for $ty:ty $(; |$candidate:ident $(: MaybeAligned<$ref_repr:ty>)? $(: Maybe<$ptr_repr:ty>)?| $is_bit_valid:expr)?
489    ) => {
490        impl_or_verify!(@impl { unsafe_impl!(
491            $($tyvar $(: $(? $optbound +)* $($bound +)*)?),* => $trait for $ty
492            $(; |$candidate $(: MaybeAligned<$ref_repr>)? $(: Maybe<$ptr_repr>)?| $is_bit_valid)?
493        ); });
494        impl_or_verify!(@verify $trait, {
495            impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?),*> Subtrait for $ty {}
496        });
497    };
498    (@impl $impl_block:tt) => {
499        #[cfg(not(any(feature = "derive", test)))]
500        const _: () = { $impl_block };
501    };
502    (@verify $trait:ident, $impl_block:tt) => {
503        #[cfg(any(feature = "derive", test))]
504        const _: () = {
505            trait Subtrait: $trait {}
506            $impl_block
507        };
508    };
509}
510
511/// Implements `KnownLayout` for a sized type.
512macro_rules! impl_known_layout {
513    ($(const $constvar:ident : $constty:ty, $tyvar:ident $(: ?$optbound:ident)? => $ty:ty),* $(,)?) => {
514        $(impl_known_layout!(@inner const $constvar: $constty, $tyvar $(: ?$optbound)? => $ty);)*
515    };
516    ($($tyvar:ident $(: ?$optbound:ident)? => $ty:ty),* $(,)?) => {
517        $(impl_known_layout!(@inner , $tyvar $(: ?$optbound)? => $ty);)*
518    };
519    ($($ty:ty),*) => { $(impl_known_layout!(@inner , => $ty);)* };
520    (@inner $(const $constvar:ident : $constty:ty)? , $($tyvar:ident $(: ?$optbound:ident)?)? => $ty:ty) => {
521        const _: () = {
522            use core::ptr::NonNull;
523
524            #[allow(non_local_definitions)]
525            // SAFETY: Delegates safety to `DstLayout::for_type`.
526            unsafe impl<$($tyvar $(: ?$optbound)?)? $(, const $constvar : $constty)?> KnownLayout for $ty {
527                #[allow(clippy::missing_inline_in_public_items)]
528                #[cfg_attr(coverage_nightly, coverage(off))]
529                fn only_derive_is_allowed_to_implement_this_trait() where Self: Sized {}
530
531                type PointerMetadata = ();
532
533                const LAYOUT: crate::DstLayout = crate::DstLayout::for_type::<$ty>();
534
535                // SAFETY: `.cast` preserves address and provenance.
536                //
537                // TODO(#429): Add documentation to `.cast` that promises that
538                // it preserves provenance.
539                #[inline(always)]
540                fn raw_from_ptr_len(bytes: NonNull<u8>, _meta: ()) -> NonNull<Self> {
541                    bytes.cast::<Self>()
542                }
543
544                #[inline(always)]
545                fn pointer_to_metadata(_ptr: NonNull<Self>) -> () {
546                }
547            }
548        };
549    };
550}
551
552/// Implements `KnownLayout` for a type in terms of the implementation of
553/// another type with the same representation.
554///
555/// # Safety
556///
557/// - `$ty` and `$repr` must have the same:
558///   - Fixed prefix size
559///   - Alignment
560///   - (For DSTs) trailing slice element size
561/// - It must be valid to perform an `as` cast from `*mut $repr` to `*mut $ty`,
562///   and this operation must preserve referent size (ie, `size_of_val_raw`).
563macro_rules! unsafe_impl_known_layout {
564    ($($tyvar:ident: ?Sized + KnownLayout =>)? #[repr($repr:ty)] $ty:ty) => {
565        const _: () = {
566            use core::ptr::NonNull;
567
568            #[allow(non_local_definitions)]
569            unsafe impl<$($tyvar: ?Sized + KnownLayout)?> KnownLayout for $ty {
570                #[allow(clippy::missing_inline_in_public_items)]
571                #[cfg_attr(coverage_nightly, coverage(off))]
572                fn only_derive_is_allowed_to_implement_this_trait() {}
573
574                type PointerMetadata = <$repr as KnownLayout>::PointerMetadata;
575
576                const LAYOUT: DstLayout = <$repr as KnownLayout>::LAYOUT;
577
578                // SAFETY: All operations preserve address and provenance.
579                // Caller has promised that the `as` cast preserves size.
580                //
581                // TODO(#429): Add documentation to `NonNull::new_unchecked`
582                // that it preserves provenance.
583                #[inline(always)]
584                fn raw_from_ptr_len(bytes: NonNull<u8>, meta: <$repr as KnownLayout>::PointerMetadata) -> NonNull<Self> {
585                    #[allow(clippy::as_conversions)]
586                    let ptr = <$repr>::raw_from_ptr_len(bytes, meta).as_ptr() as *mut Self;
587                    // SAFETY: `ptr` was converted from `bytes`, which is non-null.
588                    unsafe { NonNull::new_unchecked(ptr) }
589                }
590
591                #[inline(always)]
592                fn pointer_to_metadata(ptr: NonNull<Self>) -> Self::PointerMetadata {
593                    // SAFETY: `ptr` is non-null.
594                    #[allow(clippy::as_conversions)]
595                    let ptr = unsafe { NonNull::new_unchecked(ptr.as_ptr() as *mut $repr) };
596                    <$repr>::pointer_to_metadata(ptr)
597                }
598            }
599        };
600    };
601}
602
603/// Uses `align_of` to confirm that a type or set of types have alignment 1.
604///
605/// Note that `align_of<T>` requires `T: Sized`, so this macro doesn't work for
606/// unsized types.
607macro_rules! assert_unaligned {
608    ($($tys:ty),*) => {
609        $(
610            // We only compile this assertion under `cfg(test)` to avoid taking
611            // an extra non-dev dependency (and making this crate more expensive
612            // to compile for our dependents).
613            #[cfg(test)]
614            static_assertions::const_assert_eq!(core::mem::align_of::<$tys>(), 1);
615        )*
616    };
617}
618
619/// Emits a function definition as either `const fn` or `fn` depending on
620/// whether the current toolchain version supports `const fn` with generic trait
621/// bounds.
622macro_rules! maybe_const_trait_bounded_fn {
623    // This case handles both `self` methods (where `self` is by value) and
624    // non-method functions. Each `$args` may optionally be followed by `:
625    // $arg_tys:ty`, which can be omitted for `self`.
626    ($(#[$attr:meta])* $vis:vis const fn $name:ident($($args:ident $(: $arg_tys:ty)?),* $(,)?) $(-> $ret_ty:ty)? $body:block) => {
627        #[cfg(zerocopy_generic_bounds_in_const_fn)]
628        $(#[$attr])* $vis const fn $name($($args $(: $arg_tys)?),*) $(-> $ret_ty)? $body
629
630        #[cfg(not(zerocopy_generic_bounds_in_const_fn))]
631        $(#[$attr])* $vis fn $name($($args $(: $arg_tys)?),*) $(-> $ret_ty)? $body
632    };
633}
634
635/// Either panic (if the current Rust toolchain supports panicking in `const
636/// fn`) or evaluate a constant that will cause an array indexing error whose
637/// error message will include the format string.
638///
639/// The type that this expression evaluates to must be `Copy`, or else the
640/// non-panicking desugaring will fail to compile.
641macro_rules! const_panic {
642    ($fmt:literal) => {{
643        #[cfg(zerocopy_panic_in_const_and_vec_try_reserve)]
644        panic!($fmt);
645        #[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve))]
646        const_panic!(@non_panic $fmt)
647    }};
648    (@non_panic $fmt:expr) => {{
649        // This will type check to whatever type is expected based on the call
650        // site.
651        let panic: [_; 0] = [];
652        // This will always fail (since we're indexing into an array of size 0.
653        #[allow(unconditional_panic)]
654        panic[0]
655    }}
656}
657
658/// Either assert (if the current Rust toolchain supports panicking in `const
659/// fn`) or evaluate the expression and, if it evaluates to `false`, call
660/// `const_panic!`. This is used in place of `assert!` in const contexts to
661/// accommodate old toolchains.
662macro_rules! const_assert {
663    ($e:expr) => {{
664        #[cfg(zerocopy_panic_in_const_and_vec_try_reserve)]
665        assert!($e);
666        #[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve))]
667        {
668            let e = $e;
669            if !e {
670                let _: () = const_panic!(@non_panic concat!("assertion failed: ", stringify!($e)));
671            }
672        }
673    }}
674}
675
676/// Like `const_assert!`, but relative to `debug_assert!`.
677macro_rules! const_debug_assert {
678    ($e:expr $(, $msg:expr)?) => {{
679        #[cfg(zerocopy_panic_in_const_and_vec_try_reserve)]
680        debug_assert!($e $(, $msg)?);
681        #[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve))]
682        {
683            // Use this (rather than `#[cfg(debug_assertions)]`) to ensure that
684            // `$e` is always compiled even if it will never be evaluated at
685            // runtime.
686            if cfg!(debug_assertions) {
687                let e = $e;
688                if !e {
689                    let _: () = const_panic!(@non_panic concat!("assertion failed: ", stringify!($e) $(, ": ", $msg)?));
690                }
691            }
692        }
693    }}
694}
695
696/// Either invoke `unreachable!()` or `loop {}` depending on whether the Rust
697/// toolchain supports panicking in `const fn`.
698macro_rules! const_unreachable {
699    () => {{
700        #[cfg(zerocopy_panic_in_const_and_vec_try_reserve)]
701        unreachable!();
702
703        #[cfg(not(zerocopy_panic_in_const_and_vec_try_reserve))]
704        loop {}
705    }};
706}
707
708/// Asserts at compile time that `$condition` is true for `Self` or the given
709/// `$tyvar`s. Unlike `const_assert`, this is *strictly* a compile-time check;
710/// it cannot be evaluated in a runtime context. The condition is checked after
711/// monomorphization and, upon failure, emits a compile error.
712macro_rules! static_assert {
713    (Self $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )? => $condition:expr) => {{
714        trait StaticAssert {
715            const ASSERT: bool;
716        }
717
718        impl<T $(: $(? $optbound +)* $($bound +)*)?> StaticAssert for T {
719            const ASSERT: bool = {
720                const_assert!($condition);
721                $condition
722            };
723        }
724
725        const_assert!(<Self as StaticAssert>::ASSERT);
726    }};
727    ($($tyvar:ident $(: $(? $optbound:ident $(+)?)* $($bound:ident $(+)?)* )?),* => $condition:expr) => {{
728        trait StaticAssert {
729            const ASSERT: bool;
730        }
731
732        impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?,)*> StaticAssert for ($($tyvar,)*) {
733            const ASSERT: bool = {
734                const_assert!($condition);
735                $condition
736            };
737        }
738
739        const_assert!(<($($tyvar,)*) as StaticAssert>::ASSERT);
740    }};
741}
742
743/// Assert at compile time that `tyvar` does not have a zero-sized DST
744/// component.
745macro_rules! static_assert_dst_is_not_zst {
746    ($tyvar:ident) => {{
747        use crate::KnownLayout;
748        static_assert!($tyvar: ?Sized + KnownLayout => {
749            let dst_is_zst = match $tyvar::LAYOUT.size_info {
750                crate::SizeInfo::Sized { .. } => false,
751                crate::SizeInfo::SliceDst(TrailingSliceLayout { elem_size, .. }) => {
752                    elem_size == 0
753                }
754            };
755            !dst_is_zst
756        });
757    }}
758}