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