Skip to content

Commit e8fa408

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 e8fa408

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

crates/core_arch/src/macros.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
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+
#[allow(unused)]
14+
macro_rules! static_assert_imm8 {
15+
($imm:ident) => {
16+
let _ = $crate::core_arch::macros::ValidateConstImm8::<$imm>::VALID;
17+
};
18+
}
19+
320
#[allow(unused)]
421
macro_rules! static_assert {
522
($imm:ident : $ty:ty where $e:expr) => {

0 commit comments

Comments
 (0)