Create const_assert macro

Could also be named 'static_assert' but that would conflict with the
macro in Rust nightly.
This commit is contained in:
Nikolai Vazquez
2017-08-12 17:09:59 -04:00
parent 2d623c0248
commit d44a78e4eb
2 changed files with 27 additions and 0 deletions
+15
View File
@@ -48,3 +48,18 @@ macro_rules! assert_eq_size_val {
}
}
}
/// Asserts at compile-time that the constant expression evaluates to `true`.
#[macro_export]
macro_rules! const_assert {
($cond:expr) => {
// Causes overflow if condition is false
let _ = [(); 0 - (!($cond) as usize)];
};
($($xs:expr),+) => {
const_assert!($($xs)&&+);
};
($($xs:expr);+ $(;)*) => {
const_assert!($($xs),+);
};
}
+12
View File
@@ -0,0 +1,12 @@
#![no_std]
#[macro_use]
extern crate static_assertions;
#[test]
fn const_assert() {
const FIVE: usize = 5;
const_assert!(FIVE * 2 == 10);
const_assert!(FIVE > 2);
}