pub type CheckedUnsigned<const MAX: u32, T> = Checked<BitCount<MAX>, T>;
Expand description
An unsigned type with a verified value
Aliased Type§
pub struct CheckedUnsigned<const MAX: u32, T> { /* private fields */ }
Implementations§
Source§impl<const MAX: u32, U: UnsignedInteger> CheckedUnsigned<MAX, U>
impl<const MAX: u32, U: UnsignedInteger> CheckedUnsigned<MAX, U>
Sourcepub fn new(
count: impl TryInto<BitCount<MAX>>,
value: U,
) -> Result<Self, CheckedError>
pub fn new( count: impl TryInto<BitCount<MAX>>, value: U, ) -> Result<Self, CheckedError>
Returns our value if it fits in the given number of bits
§Example
use bitstream_io::{BitCount, CheckedUnsigned, CheckedError};
// a value of 7 fits into a 3 bit count
assert!(CheckedUnsigned::<8, u8>::new(3, 0b111).is_ok());
// a value of 8 does not fit into a 3 bit count
assert!(matches!(
CheckedUnsigned::<8, u8>::new(3, 0b1000),
Err(CheckedError::ExcessiveValue),
));
// a bit count of 9 is too large for u8
assert!(matches!(
CheckedUnsigned::<9, _>::new(9, 1u8),
Err(CheckedError::ExcessiveBits),
));
Sourcepub fn new_fixed<const BITS: u32>(value: U) -> Result<Self, CheckedError>
pub fn new_fixed<const BITS: u32>(value: U) -> Result<Self, CheckedError>
Returns our value if it fits in the given number of const bits
§Examples
use bitstream_io::{CheckedUnsigned, CheckedError};
// a value of 7 fits into a 3 bit count
assert!(CheckedUnsigned::<8, u8>::new_fixed::<3>(0b111).is_ok());
// a value of 8 does not fit into a 3 bit count
assert!(matches!(
CheckedUnsigned::<8, u8>::new_fixed::<3>(0b1000),
Err(CheckedError::ExcessiveValue),
));
ⓘ
use bitstream_io::{BitCount, CheckedUnsigned};
// a bit count of 9 is too large for u8
// because this is checked at compile-time,
// it does not compile at all
let c = CheckedUnsigned::<16, u8>::new_fixed::<9>(1);