Skip to content

Commit e501dea

Browse files
committed
const_fn: Add const_assert helper macro
Signed-off-by: Joe Richey <[email protected]>
1 parent 67be0e6 commit e501dea

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ macro_rules! const_fn {
4545
};
4646
}
4747

48+
// Helper method for assert! in const fn. Uses out of bounds indexing if an
49+
// assertion fails and the "const_fn" feature is not enabled.
50+
#[cfg(feature = "const_fn")]
51+
macro_rules! const_assert {
52+
($cond:expr, $($arg:tt)+) => { assert!($cond, $($arg)*) };
53+
}
54+
#[cfg(not(feature = "const_fn"))]
55+
macro_rules! const_assert {
56+
($cond:expr, $($arg:tt)+) => {
57+
[(); 1][!($cond as bool) as usize]
58+
};
59+
}
60+
4861
#[cfg(all(feature = "instructions", feature = "external_asm"))]
4962
pub(crate) mod asm;
5063

0 commit comments

Comments
 (0)