My code looks like:
macro_rules! mask {
($bitmap: tt, [..$count: tt], for type = $ty: ty) => {
{
let bit_count = std::mem::size_of::<$ty>() * 8;
let dec_bit_count = bit_count - 1;
$bitmap & [(1 << ($count & dec_bit_count)) - 1, <$ty>::MAX][((($count & !dec_bit_count)) != 0) as usize]
}
};
}
fn main() {
let bitmap: u8 = 0b_1111_1111;
let masked_bitmap = mask!(bitmap, [..5], for type = u8);
println!("{:#010b}", masked_bitmap);
}
The above code will mask the bitmap. In the above example, 0b_1111_1111 on being masked by [..5] will become 0b_0001_1111.
I want my macro to be like this:
macro_rules! mask {
($bitmap: tt, [..$count: tt]) => {
{
let bit_count = std::mem::size_of::<decltype($bitmap)>() * 8;
let dec_bit_count = bit_count - 1;
$bitmap & [(1 << ($count & dec_bit_count)) - 1, <decltype($bitmap)>::MAX][((($count & !dec_bit_count)) != 0) as usize]
}
};
}
But I have to pass type to the macro to get this done. Is there something like decltype() from C++ that I could use?