bitmask_switch!() { /* proc-macro */ }Expand description
Switch using a bitmask as scrutinee, useful for switching on processor instruction opcodes.
The switch is similar to a normal Rust match expression:
use machine_check::{Bitvector, bitmask_switch};
let opcode = Bitvector::<6>::new(0b10_1101);
let mut foo = Bitvector::<2>::new(0);
let mut bar = Bitvector::<2>::new(0);
bitmask_switch!(opcode {
"00_----" => {}
"10_aabb" => {
foo = a;
bar = b;
}
"11_--cc" => {
foo = c;
}
_ => {}
});
assert_eq!(foo, Bitvector::new(3));
assert_eq!(bar, Bitvector::new(1));Unlike Rust match, the scrutinee must be Bitvector, and the non-default choices are string literals
containing, for each bit of the bitvector, one of these:
- ‘0’ or ‘1’: the bit must match,
- dash (‘-’): the bit can have any value (don’t care),
- ASCII letter: same as don’t care, but a new variable is created containing the bits with given letter.
- Underscore (‘_’) used to visually separate the bits.
Unlike Rust match, overlapping choices are not permitted, so that it is certain which arm is taken. In case there is no default arm, there must be full coverage.
Currently, the macro cannot return values and there is no syntactic disjunction guarantee, i.e. that exactly one of the arms is taken. This may change in the future.