Skip to content

Commit 6294f15

Browse files
committed
add static_assert_imm8 macro
Since this one will be used a lot, a single macro and struct can be used to avoid duplicating the imm8 check in every function, and instantiating the same MIR struct multiple times.
1 parent f979a5a commit 6294f15

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

crates/core_arch/src/macros.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
//! Utility macros.
22
3+
// Helper struct used to trigger const eval errors when a const generic immediate value is
4+
// out of range.
5+
#[doc(hidden)]
6+
pub(crate) struct ValidateConstImm8<const imm8: i32>();
7+
impl<const imm8: i32> ValidateConstImm8<imm8> {
8+
pub(crate) const VALID: () = {
9+
let _ = 1 / ((imm8 >= 0 && imm8 <= 255) as usize);
10+
};
11+
}
12+
13+
macro_rules! static_assert_imm8 {
14+
($imm:ident) => {
15+
let _ = $crate::core_arch::macros::ValidateConstImm8::<$imm>::VALID;
16+
};
17+
}
18+
319
#[allow(unused)]
420
macro_rules! static_assert {
521
($imm:ident : $ty:ty where $e:expr) => {

0 commit comments

Comments
 (0)