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