1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
/// Asserts that the types' alignments are equal.
///
/// 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_align_eq!(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_align_eq!([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_align_eq!(i32x4, [i32; 4]);
/// ```
///
/// [FFI]: https://en.wikipedia.org/wiki/Foreign_function_interface
#[macro_export(local_inner_macros)]
macro_rules! assert_align_eq {
($x:ty, $($y:ty),+ $(,)?) => {
const _: fn() = || {
use $crate::_core::mem::align_of;
const_assert_eq_usize!(align_of::<$x>() $(, align_of::<$y>())+);
};
};
}
/// Asserts that the types' alignments are equal.
///
/// This macro has been deprecated in favor of
/// [`assert_align_eq!`](macro.assert_align_eq.html).
#[deprecated(
since = "1.2.0",
note = "Please use the 'assert_align_eq' macro instead"
)]
#[macro_export(local_inner_macros)]
macro_rules! assert_eq_align {
($($t:tt)*) => {
assert_align_eq!($($t)*);
};
}
/// Asserts that the types' alignments are **not** equal.
///
/// # Examples
///
/// A `u8` does not have the same alignment as a pointer:
///
/// ```
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// assert_align_ne!(u8, *const u8);
/// ```
///
/// The following example fails to compile because a `usize` has the same
/// alignment as a pointer:
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// assert_align_ne!(*const u8, usize);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! assert_align_ne {
($x:ty, $($y:ty),+ $(,)?) => {
const _: fn() = || {
use $crate::_core::mem::align_of;
const_assert_ne!(align_of::<$x>() $(, align_of::<$y>())+);
};
};
}
/// Asserts that the types' alignments are less than each other.
///
/// # Examples
///
/// A `u8` has smaller alignment than `u16`, which has smaller alignment than
/// a pointer:
///
/// ```
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// assert_align_lt!(u8, u16, *const u8);
/// ```
///
/// The following example fails to compile because a `usize` has the same
/// alignment as a pointer:
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// assert_align_lt!(*const u8, usize);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! assert_align_lt {
($x:ty, $($y:ty),+ $(,)?) => {
const _: fn() = || {
use $crate::_core::mem::align_of;
const_assert_lt!(align_of::<$x>() $(, align_of::<$y>())+);
};
};
}
/// Asserts that the types' alignments are less than or equal to each other.
///
/// # Examples
///
/// A `u8` and `i8` have smaller alignment than any pointer type:
///
/// ```
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// assert_align_le!(u8, i8, *const u8);
/// ```
///
/// The following example fails to compile because a `usize` has greater
/// alignment than `u8`:
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// assert_align_le!(usize, u8);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! assert_align_le {
($x:ty, $($y:ty),+ $(,)?) => {
const _: fn() = || {
use $crate::_core::mem::align_of;
const_assert_le!(align_of::<$x>() $(, align_of::<$y>())+);
};
};
}
/// Asserts that the types' alignments are greater than each other.
///
/// # Examples
///
/// A pointer has greater alignment than `u16`, which has greater alignment than
/// `u8`:
///
/// ```
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// assert_align_gt!(*const u8, u16, u8);
/// ```
///
/// The following example fails to compile because a `usize` has the same
/// alignment as a pointer:
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// assert_align_gt!(*const u8, usize);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! assert_align_gt {
($x:ty, $($y:ty),+ $(,)?) => {
const _: fn() = || {
use $crate::_core::mem::align_of;
const_assert_gt!(align_of::<$x>() $(, align_of::<$y>())+);
};
};
}
/// Asserts that the types' alignments are greater than or equal to each other.
///
/// # Examples
///
/// A pointer has greater alignment than `u8` and `i8`:
///
/// ```
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// assert_align_ge!(*const u8, u8, i8);
/// ```
///
/// The following example fails to compile because a `u8` has smaller alignment
/// than `usize`:
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// assert_align_ge!(u8, usize);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! assert_align_ge {
($x:ty, $($y:ty),+ $(,)?) => {
const _: fn() = || {
use $crate::_core::mem::align_of;
const_assert_ge!(align_of::<$x>() $(, align_of::<$y>())+);
};
};
}