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
//! 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.0" //! ``` //! //! 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); //! //! // Produces a compilation failure: //! // const_assert_eq!(two; TWO, TWO); //! //! fn main() { //! const NUM: usize = 32; //! const_assert_eq!(NUM + NUM, 64); //! } //! ``` //! //! # Limitations //! //! Due to implementation details, [`assert_eq_size`], [`const_assert`], and //! [`const_assert_eq`] 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. //! //! 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 //! [`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 //! [`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),+); }; }