Struct Checked

Source
pub struct Checked<C, T> { /* private fields */ }
Expand description

A type for eliminating redundant validation when writing

Normally, when writing a value, not only must the number of bits must be checked against the type being written (e.g. writing 9 bits from a u8 is always an error), but the value must also be checked against the number of bits (e.g. writing a value of 2 in 1 bit is always an error).

But when the value’s range can be checked in advance, the write-time check can be skipped through the use of the BitWrite::write_checked method.

Implementations§

Source§

impl<C, T> Checked<C, T>

Source

pub fn into_count_value(self) -> (C, T)

Returns our bit count and value

Source

pub fn into_value(self) -> T

Returns our value

Source§

impl<const MAX: u32, U: UnsignedInteger> Checked<BitCount<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);
Source§

impl<const MAX: u32, S: SignedInteger> Checked<SignedBitCount<MAX>, S>

Source

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

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

Trait Implementations§

Source§

impl<C, T> AsRef<T> for Checked<C, T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<C: Clone, T: Clone> Clone for Checked<C, T>

Source§

fn clone(&self) -> Checked<C, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C: Debug, T: Debug> Debug for Checked<C, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C: Copy, T: Copy> Copy for Checked<C, T>

Auto Trait Implementations§

§

impl<C, T> Freeze for Checked<C, T>
where C: Freeze, T: Freeze,

§

impl<C, T> RefUnwindSafe for Checked<C, T>

§

impl<C, T> Send for Checked<C, T>
where C: Send, T: Send,

§

impl<C, T> Sync for Checked<C, T>
where C: Sync, T: Sync,

§

impl<C, T> Unpin for Checked<C, T>
where C: Unpin, T: Unpin,

§

impl<C, T> UnwindSafe for Checked<C, T>
where C: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.