Function genmask_checked_u32

Source
pub fn genmask_checked_u32(range: RangeInclusive<u32>) -> Option<u32>
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_u32(0..=0), Some(0b1));
assert_eq!(genmask_checked_u32(0..=31), Some(u32::MAX));
assert_eq!(genmask_checked_u32(21..=31), Some(0xffe0_0000));

// `40` is out of the supported bit range.
assert_eq!(genmask_checked_u32(21..=40), None);

// Invalid range where the start is bigger than the end.
assert_eq!(genmask_checked_u32(15..=8), None);