Function genmask_checked_u8

Source
pub fn genmask_checked_u8(range: RangeInclusive<u32>) -> Option<u8>
Expand description

Creates a contiguous bitmask for the given range by validating the range at runtime.

Returns None if the range is invalid, i.e.: if the start is greater than the end or if the range is outside of the representable range for the type.

ยงExamples

assert_eq!(genmask_checked_u8(0..=0), Some(0b1));
assert_eq!(genmask_checked_u8(0..=7), Some(u8::MAX));
assert_eq!(genmask_checked_u8(6..=7), Some(0xc0));

// `10` is out of the supported bit range.
assert_eq!(genmask_checked_u8(6..=10), None);

// Invalid range where the start is bigger than the end.
assert_eq!(genmask_checked_u8(5..=2), None);