| 1 |
// Copyright 2026 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 |
extern crate zerocopy; |
| 10 |
|
| 11 |
#[allow(unused)] |
| 12 |
#[cfg(feature = "derive")] |
| 13 |
mod util { |
| 14 |
/// A type that doesn't implement any zerocopy traits. |
| 15 |
pub struct NotZerocopy<T = ()>(pub T); |
| 16 |
|
| 17 |
/// A `u16` with alignment 2. |
| 18 |
/// |
| 19 |
/// Though `u16` has alignment 2 on some platforms, it's not guaranteed. By |
| 20 |
/// contrast, `util::AU16` is guaranteed to have alignment 2. |
| 21 |
#[derive( |
| 22 |
zerocopy::KnownLayout, |
| 23 |
zerocopy::Immutable, |
| 24 |
zerocopy::FromBytes, |
| 25 |
zerocopy::IntoBytes, |
| 26 |
Copy, |
| 27 |
Clone, |
| 28 |
)] |
| 29 |
#[repr(C, align(2))] |
| 30 |
pub struct AU16(pub u16); |
| 31 |
|
| 32 |
// Since we can't import these by path (ie, `util::assert_impl_all!`), use a |
| 33 |
// name prefix to ensure our derive-emitted code isn't accidentally relying |
| 34 |
// on `assert_impl_all!` being in scope. |
| 35 |
#[macro_export] |
| 36 |
macro_rules! util_assert_impl_all { |
| 37 |
($type:ty: $($trait:path),+ $(,)?) => { |
| 38 |
const _: fn() = || { |
| 39 |
use ::core::prelude::v1::*; |
| 40 |
::static_assertions::assert_impl_all!($type: $($trait),+); |
| 41 |
}; |
| 42 |
}; |
| 43 |
} |
| 44 |
|
| 45 |
// Since we can't import these by path (ie, `util::assert_not_impl_any!`), |
| 46 |
// use a name prefix to ensure our derive-emitted code isn't accidentally |
| 47 |
// relying on `assert_not_impl_any!` being in scope. |
| 48 |
#[macro_export] |
| 49 |
macro_rules! util_assert_not_impl_any { |
| 50 |
($x:ty: $($t:path),+ $(,)?) => { |
| 51 |
const _: fn() = || { |
| 52 |
use ::core::prelude::v1::*; |
| 53 |
::static_assertions::assert_not_impl_any!($x: $($t),+); |
| 54 |
}; |
| 55 |
}; |
| 56 |
} |
| 57 |
|
| 58 |
#[macro_export] |
| 59 |
macro_rules! test_trivial_is_bit_valid { |
| 60 |
($x:ty => $name:ident) => { |
| 61 |
#[test] |
| 62 |
fn $name() { |
| 63 |
util::test_trivial_is_bit_valid::<$x>(); |
| 64 |
} |
| 65 |
}; |
| 66 |
} |
| 67 |
} |