mirror of
https://github.com/openharmony/third_party_rust_static-assertions-rs.git
synced 2026-07-19 16:53:36 -04:00
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:
+15
@@ -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),+);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user