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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
//! Compile-time assertions to ensure that invariants are met. //! //! # Usage //! //! This crate is available [on crates.io][crate] and can be used by adding the //! following to your project's `Cargo.toml`: //! //! ```toml //! [dependencies] //! static_assertions = "0.2.4" //! ``` //! //! and this to your crate root: //! //! ``` //! #[macro_use] //! extern crate static_assertions; //! # fn main() {} //! ``` //! //! # Limitations //! //! Due to implementation details, some macros can only be used normally from //! within the context of a function. To use these macros in other contexts, a //! unique label must be provided. //! //! This issue can be followed [here][issue1]. Feedback and potential solutions //! are welcome! //! //! [issue1]: https://github.com/nvzqz/static-assertions-rs/issues/1 //! [crate]: https://crates.io/crates/static_assertions #![no_std] #![deny(unused_macros)] #[doc(hidden)] pub extern crate core as _core; /// Asserts that the configuration is set. /// /// # Examples /// /// A project may not support a set of configurations and thus you may want to /// report why: /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// // We should only be compiling for Unix or Linux /// # #[cfg(any(unix, linux))] /// assert_cfg!(any(unix, linux)); /// # fn main() {} /// ``` /// /// If users need to specify a database back-end: /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// # #[cfg(target_pointer_width = "0")] // Impossible /// assert_cfg!("Must exclusively use MySQL or MongoDB as database back-end", /// all(not(all(feature = "mysql", feature = "mongodb")), /// any( feature = "mysql", feature = "mongodb"))); /// # fn main() {} /// ``` #[macro_export] macro_rules! assert_cfg { () => {}; ($msg:expr, $($cfg:tt)*) => { #[cfg(not($($cfg)*))] compile_error!($msg); }; ($($cfg:tt)*) => { #[cfg(not($($cfg)*))] compile_error!(concat!("Cfg does not pass: ", stringify!($($cfg)*))); }; } /// Asserts at compile-time that the types have equal sizes. /// /// When performing operations such as pointer casts or dealing with [`usize`] /// versus [`u64`] versus [`u32`], the size of your types matter. This is where /// this macro comes into play. /// /// # Alternatives /// /// There are also [`assert_eq_size_val`](macro.assert_eq_size_val.html) and /// [`assert_eq_size_ptr`](macro.assert_eq_size_ptr.html). Instead of specifying /// types to compare, values' sizes can be directly compared against each other. /// /// # Example /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// // Can be declared outside of a function if labeled /// assert_eq_size!(bytes; (u8, u8), u16); /// /// // Fails to compile (same label): /// // assert_eq_size!(bytes; u8, u8); /// /// fn main() { /// // Supports unlimited arguments: /// assert_eq_size!([u8; 4], (u16, u16), u32); /// /// // Produces a compilation failure: /// // assert_eq_size!(u32, u8); /// } /// ``` /// /// [`usize`]: https://doc.rust-lang.org/std/primitive.usize.html /// [`u64`]: https://doc.rust-lang.org/std/primitive.u64.html /// [`u32`]: https://doc.rust-lang.org/std/primitive.u32.html #[macro_export] macro_rules! assert_eq_size { ($x:ty, $($xs:ty),+ $(,)*) => { $(let _ = $crate::_core::mem::transmute::<$x, $xs>;)+ }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_eq_size!($($xs)+); } }; } /// Asserts at compile-time that the values pointed to have equal sizes. /// /// This especially is useful for when coercing pointers between different types /// and ensuring the underlying values are the same size. /// /// # Example /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// fn operation(x: &(u32, u32), y: &[u16; 4]) { /// assert_eq_size_ptr!(x, y); /// } /// # fn main() {} /// ``` #[macro_export] macro_rules! assert_eq_size_ptr { ($x:expr, $($xs:expr),+ $(,)*) => { #[allow(unknown_lints, forget_copy, unused_unsafe, useless_transmute)] unsafe { use $crate::_core::{mem, ptr}; let mut copy = ptr::read($x); $(ptr::write(&mut copy, mem::transmute(ptr::read($xs)));)+ mem::forget(copy); } } } /// Asserts at compile-time that the values have equal sizes. /// /// This macro doesn't consume its arguments and thus works for /// non-[`Clone`]able values. /// /// # Example /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// # fn main() { /// struct Byte(u8); /// /// let x = 10u8; /// let y = Byte(42); // Works for non-cloneable types /// /// assert_eq_size_val!(x, y); /// assert_eq_size_val!(x, y, 0u8); /// /// // Fails to compile: /// // assert_eq_size_val!(x, 0u32); /// # } /// ``` /// /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[macro_export] macro_rules! assert_eq_size_val { ($x:expr, $($xs:expr),+ $(,)*) => { assert_eq_size_ptr!(&$x, $(&$xs),+); } } /// Asserts at compile-time that the constant expression evaluates to `true`. /// /// There also exists [`const_assert_eq`](macro.const_assert_eq.html) for /// validating whether a sequence of expressions are equal to one another. /// /// # Example /// /// Constant expressions can be ensured to have certain properties via this /// macro If the expression evaluates to `false`, the file will fail to compile. /// This is synonymous to [`static_assert` in C++][static_assert]. /// /// As a [limitation](index.html#limitations), a unique label is required if /// the macro is used outside of a function. /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// const FIVE: usize = 5; /// /// const_assert!(twenty_five; FIVE * FIVE == 25); /// /// fn main() { /// const_assert!(2 + 2 == 4); /// const_assert!(FIVE - FIVE == 0); /// /// // Produces a compilation failure: /// // const_assert!(1 >= 2); /// } /// ``` /// /// [static_assert]: http://en.cppreference.com/w/cpp/language/static_assert #[macro_export] macro_rules! const_assert { ($($xs:expr),+ $(,)*) => { #[allow(unknown_lints, eq_op)] let _ = [(); 0 - !($($xs)&&+) as usize]; }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { const_assert!($($xs)+); } }; } /// Asserts at compile-time that the constants are equal in value. /// /// # Example /// /// Works as a shorthand for `const_assert!(a == b)`: /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// const TWO: usize = 2; /// const_assert_eq!(two; TWO * TWO, TWO + TWO, 4); /// /// // Fails to compile (same label): /// // const_assert_eq!(two; TWO, TWO); /// /// fn main() { /// const NUM: usize = 32; /// const_assert_eq!(NUM + NUM, 64); /// } /// ``` #[macro_export] macro_rules! const_assert_eq { ($x:expr, $($xs:expr),+ $(,)*) => { const_assert!($($x == $xs),+); }; ($label:ident; $x:expr, $($xs:expr),+ $(,)*) => { const_assert!($label; $($x == $xs),+); }; } /// Asserts at compile-time that the traits are object-safe. /// /// This is useful for when changes are made to a trait that accidentally /// prevent it from being used as an object. Such a case would be adding a /// generic method and forgetting to add `where Self: Sized` after it. If left /// unnoticed, that mistake will affect crate users and break both forward and /// backward compatibility. /// /// # Example /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// assert_obj_safe!(basic; Send, Sync, AsRef<str>); /// /// trait MySafeTrait { /// fn foo(&self) -> u32; /// } /// /// trait MyUnsafeTrait { /// fn bar<T>(&self) -> T; /// } /// /// fn main() { /// assert_obj_safe!(MySafeTrait); /// /// // Produces a compilation failure: /// // assert_obj_safe!(MyUnsafeTrait); /// } /// ``` #[macro_export] macro_rules! assert_obj_safe { ($($xs:ty),+ $(,)*) => { $(let _: Option<&$xs> = None;)+ }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_obj_safe!($($xs)+); } }; } /// Asserts at compile-time that the type has the given fields. /// /// This is useful for when types have odd fields as a result of `#[cfg]`. #[macro_export] macro_rules! assert_fields { ($t:path, $($f:ident),+) => { $(let $t { $f: _, .. };)+ }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_fields!($($xs)+); } }; } /// Asserts at compile-time that the type implements the given traits. /// /// # Examples /// /// Can be used to ensure types implement [`Send`], [`Sync`], and other traits: /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// assert_impl!(str; String, Send, Sync, From<&'static str>); /// assert_impl!(vec; &'static [u8], Into<Vec<u8>>); /// /// fn main() { /// // Produces a compilation failure: /// // `*const u8` cannot be sent between threads safely /// // assert_impl!(*const u8, Send); /// } /// ``` /// /// [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html /// [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html #[macro_export] macro_rules! assert_impl { ($x:ty, $($t:path),+ $(,)*) => { $({ fn assert_impl<T: ?Sized + $t>() {} assert_impl::<$x>(); })+ }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_impl!($($xs)+); } }; }