diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
commit | 64d98f8ee037282c35007b64c2649055c56af1db (patch) | |
tree | 5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /vendor/static_assertions/src/assert_eq_align.rs | |
parent | Adding debian version 1.67.1+dfsg1-1. (diff) | |
download | rustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/static_assertions/src/assert_eq_align.rs')
-rw-r--r-- | vendor/static_assertions/src/assert_eq_align.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/vendor/static_assertions/src/assert_eq_align.rs b/vendor/static_assertions/src/assert_eq_align.rs new file mode 100644 index 000000000..69412daef --- /dev/null +++ b/vendor/static_assertions/src/assert_eq_align.rs @@ -0,0 +1,45 @@ +/// Asserts that types are equal in alignment. +/// +/// This is useful when ensuring that pointer arithmetic is done correctly, or +/// when [FFI] requires a type to have the same alignment as some foreign type. +/// +/// # Examples +/// +/// A `usize` has the same alignment as any pointer type: +/// +/// ``` +/// # #[macro_use] extern crate static_assertions; fn main() {} +/// assert_eq_align!(usize, *const u8, *mut u8); +/// ``` +/// +/// The following passes because `[i32; 4]` has the same alignment as `i32`: +/// +/// ``` +/// # #[macro_use] extern crate static_assertions; fn main() {} +/// assert_eq_align!([i32; 4], i32); +/// ``` +/// +/// The following example fails to compile because `i32x4` explicitly has 4 +/// times the alignment as `[i32; 4]`: +/// +/// ```compile_fail +/// # #[macro_use] extern crate static_assertions; fn main() {} +/// # #[allow(non_camel_case_types)] +/// #[repr(align(16))] +/// struct i32x4([i32; 4]); +/// +/// assert_eq_align!(i32x4, [i32; 4]); +/// ``` +/// +/// [FFI]: https://en.wikipedia.org/wiki/Foreign_function_interface +#[macro_export] +macro_rules! assert_eq_align { + ($x:ty, $($xs:ty),+ $(,)?) => { + const _: fn() = || { + // Assigned instance must match the annotated type or else it will + // fail to compile + use $crate::_core::mem::align_of; + $(let _: [(); align_of::<$x>()] = [(); align_of::<$xs>()];)+ + }; + }; +} |