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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
//! 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.2" //! ``` //! //! and this to your crate root: //! //! ``` //! #[macro_use] //! extern crate static_assertions; //! # fn main() {} //! ``` //! //! # Assert Equal Size //! //! When performing operations such as pointer casts or dealing with [`usize`] //! versus [`u64`] versus [`u32`], the size of your types matter. This is where //! [`assert_eq_size`] comes into play. Types provided as arguments to //! [`assert_eq_size`] are ensured to be the same size at compile-time. If the //! types differ in size, the code will fail to compile. //! //! ``` //! # #[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() { //! assert_eq_size!([u8; 4], (u16, u16), u32); //! //! // Produces a compilation failure: //! // assert_eq_size!(u32, u8); //! } //! ``` //! //! Similar to [`assert_eq_size`], there is [`assert_eq_size_val`]. Instead of //! specifying types to compare, values' sizes can be directly compared against //! each other. //! //! ``` //! # #[macro_use] //! # extern crate static_assertions; //! # fn main() { //! let x = 42u8; //! let y = true; //! //! assert_eq_size_val!(x, y); //! # } //! ``` //! [`assert_eq_size_val`] doesn't consume its arguments and thus works for //! non-[`Clone`]able values. //! //! ``` //! # #[macro_use] //! # extern crate static_assertions; //! # fn main() { //! struct Buffer([u8; 256]); //! //! let buf = Buffer([0; 256]); //! let val = [0u64; 32]; //! //! assert_eq_size_val!(buf, val); //! //! // `buf` and `val` can be used here //! # } //! ``` //! //! Rather than dereference a pointer to achieve the same effect as //! [`assert_eq_size_val`], there is also the option of [`assert_eq_size_ptr`]. //! //! # Assert Constant Expression //! //! Constant expressions can be ensured to have certain properties via //! [`const_assert`]. 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](#limitations), a unique label is required if the macro is //! used outside of a function. //! //! ``` //! # #[macro_use] //! # extern crate static_assertions; //! # fn main() { //! const NUM: usize = 32; //! //! const_assert!(NUM * NUM == 1024); //! # } //! ``` //! //! As a shorthand for `const_assert!(a == b)`, there's [`const_assert_eq`]: //! //! ``` //! # #[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); //! } //! ``` //! //! # Assert Object Safety //! //! Sometimes changes are made to traits that prevent them from being used in //! the context of 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 end up affecting crate users and break compatibility. //! //! [`assert_obj_safe`] is here to save you from those troubles: //! //! ``` //! # #[macro_use] //! # extern crate static_assertions; //! assert_obj_safe!(basic; Send, Sync, AsRef<str>); //! //! trait MySafeTrait {} //! //! trait MyUnsafeTrait { //! fn generic<T>(); //! } //! //! fn main() { //! assert_obj_safe!(MySafeTrait); //! //! // Produces a compilation failure: //! // assert_obj_safe!(MyUnsafeTrait); //! } //! ``` //! //! # Assert Trait `impl` //! //! To ensure types implement [`Send`], [`Sync`], and other traits, there's //! [`assert_impl`]: //! //! ``` //! # #[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); //! } //! ``` //! //! # Limitations //! //! Due to implementation details, the following can only be used normally from //! within the context of a function: //! //! - [`assert_eq_size`] //! - [`assert_obj_safe`] //! - [`const_assert`] //! - [`const_assert_eq`] //! //! To use these macros in other contexts, a unique label must be provided. //! //! If you want to read up about this and provide feedback, see //! [the related issue on GitHub][issue1]. //! //! [issue1]: https://github.com/nvzqz/static-assertions-rs/issues/1 //! [crate]: https://crates.io/crates/static_assertions //! [static_assert]: http://en.cppreference.com/w/cpp/language/static_assert //! [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html //! [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html //! [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html //! [`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 //! [`assert_eq_size_val`]: macro.assert_eq_size_val.html //! [`assert_eq_size_ptr`]: macro.assert_eq_size_ptr.html //! [`assert_eq_size`]: macro.assert_eq_size.html //! [`assert_obj_safe`]: macro.assert_obj_safe.html //! [`assert_impl`]: macro.assert_impl.html //! [`const_assert`]: macro.const_assert.html //! [`const_assert_eq`]: macro.const_assert_eq.html #![no_std] #[doc(hidden)] pub extern crate core as _core; /// Asserts at compile-time that the types have equal sizes. /// /// # Example /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// struct Byte(u8); /// /// assert_eq_size!(pair; (u16, u16), [u16; 2], [u8; 4]); /// /// // Fails to compile (same label): /// // assert_eq_size!(pair; u8, u8); /// /// fn main() { /// assert_eq_size!(Byte, u8); /// /// // Supports unlimited arguments: /// assert_eq_size!([Byte; 4], [u16; 2], u32); /// /// // Produces a compilation failure: /// // assert_eq_size!(Byte, u16); /// } /// ``` #[macro_export] macro_rules! assert_eq_size { ($x:ty, $($xs:ty),+) => { #[allow(unused_unsafe)] unsafe { use $crate::_core::mem::{forget, transmute, uninitialized}; $(forget::<$xs>(transmute(uninitialized::<$x>()));)+ } }; ($label:ident; $x:ty, $($xs:ty),+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_eq_size!($x, $($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(unused_unsafe)] 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. /// /// # 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); /// # } /// ``` #[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`. /// /// # Example /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// # fn main() { /// const_assert!(2 + 2 == 4); /// /// const FIVE: usize = 5; /// const_assert!(FIVE - FIVE == 0); /// /// // Produces a compilation failure: /// // const_assert!(1 >= 2); /// # } /// ``` #[macro_export] macro_rules! const_assert { ($($xs:expr),+) => { let _ = [(); 0 - (!($($xs)&&+) as usize)]; }; ($label:ident; $($xs:expr),+) => { #[allow(dead_code, non_snake_case)] fn $label() { const_assert!($($xs),+); } }; } /// Asserts at compile-time that the constants are equal in value. #[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. #[macro_export] macro_rules! assert_obj_safe { ($($xs:ty),+) => { $(let _: Option<&$xs> = None;)+ }; ($label:ident; $($xs:ty),+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_obj_safe!($($xs),+); } }; } /// Asserts at compile-time that the type implements the given traits. #[macro_export] macro_rules! assert_impl { ( $x:ty, $( $y:ident $(< $($args:ty),+ $(,)* >)* ),+ ) => { $({ fn assert_impl<T: ?Sized + $y $(< $($args),+ >)* >() {} assert_impl::<$x>(); })+ }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_impl!($($xs)+); } }; }