Skip to content

Commit 66463d5

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 66463d5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

crates/core_arch/src/macros.rs

Lines changed: 16 additions & 0 deletions
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+
pub(crate) struct ValidateConstImm8<const imm8: i32>();
6+
impl<const imm8: i32> ValidateConstImm8<imm8> {
7+
pub(crate) const VALID: () = {
8+
let _ = 1 / ((imm8 >= 0 && imm8 <= 255) as usize);
9+
};
10+
}
11+
12+
#[allow(unused)]
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)