macro_rules! bitpattern {
($pattern:literal $(, $type_name:ident)?) => { ... };
}
Expand description
Create a const
BitPattern
from a “literal” with wildcards.
See the documentation for that type for more details.
Specify the pattern as a binary number inside of a string, with optional
0b
prefix and underscore separators. Dots (.
) will be treated as ignored/
wildcard bits which can take any value. Unspecified bits (those in places
beyond those in the string pattern) are always ignored.
The pattern will be given the smallest unsigned integer type capable of storing it. To override this behavior, specify a type manually as a second parameter. Explicitly specifying a type which is too small will cause a compilation failure.
If not storing to a variable, prefer the [is_bit_match!
] macro over
bitpattern!(...).is_match(...)
.
§Examples
use bitpatterns::*;
// Use the macro to define a BitPattern clearly
let pattern_1 = bitpattern!("10..01");
// The pattern can be stored as a constant too
// Note the supported 0b prefix and _ separator
let PATTERN_2: BitPattern<u8> = bitpattern!("0b00_01..");
// We can also store the pattern as another type with explicit specification
// This will have type BitPattern<i32>
let pattern_3 = bitpattern!("0b00_01..", i32);
// But specifying a type that is too small will fail
// let pattern_4 = bitpattern!("0b000010001000", u8); // Doesn't compile
// Now we can use the patterns to match numbers
assert!(pattern_1.is_match(45)); // 45 == 0b101101
assert!(!PATTERN_2.is_match(45));
assert!(!pattern_1.is_match(6)); // 6 = 0b000110
assert!(PATTERN_2.is_match(6));