Type Alias CheckedUnsigned

Source
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>

Source

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),
));
Source

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);

Trait Implementations§

Source§

impl<const MAX: u32, U: UnsignedInteger> Checkable for CheckedUnsigned<MAX, U>

Source§

fn write<W: BitWrite + ?Sized>(&self, writer: &mut W) -> Result<()>

Write our value to the given stream
Source§

fn written_bits(&self) -> u32

The number of written bits
Source§

impl<const MAX: u32, U: UnsignedInteger> CheckablePrimitive for CheckedUnsigned<MAX, U>

Source§

type CountType = BitCount<MAX>

Our bit count type for reading
Source§

fn read<R: BitRead + ?Sized>( reader: &mut R, count: Self::CountType, ) -> Result<Self>

Reads our value from the given stream