Skip to main content

Checkable

Trait Checkable 

Source
pub trait Checkable: Checkable + Sized {
    // Required methods
    fn write<W: BitWrite + ?Sized>(&self, writer: &mut W) -> Result<()>;
    fn written_bits(&self) -> u32;
}
Expand description

A trait for writable types whose values can be validated

Ordinarily, when writing a value to a stream with a given number of bits, the value must be validated to ensure it will fit within that number of bits.

§Example 1

use bitstream_io::{BitWrite, BitWriter, BigEndian};

let mut w = BitWriter::endian(vec![], BigEndian);

// writing a value of 2 in 1 bit is always an error
// which is checked here at write-time
assert!(w.write::<1, u8>(2).is_err());

But if the value can be checked beforehand, it doesn’t need to be checked at write-time.

§Example 2

use bitstream_io::{BitWrite, BitWriter, BigEndian, CheckedUnsigned};

let mut w = BitWriter::endian(vec![], BigEndian);

// writing a value of 1 in 1 bit is ok
// and we're checking that validity at this stage
let value = CheckedUnsigned::<1, u8>::new_fixed::<1>(1).unwrap();

// because we've pre-validated the value beforehand,
// it doesn't need to be checked again here
// (though the write itself may still fail)
assert!(w.write_checked(value).is_ok());

Required Methods§

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<const BITS: u32, S: SignedInteger> Checkable for CheckedSignedFixed<BITS, S>

Source§

impl<const BITS: u32, U: UnsignedInteger> Checkable for CheckedUnsignedFixed<BITS, U>

Source§

impl<const MAX: u32, S: SignedInteger> Checkable for CheckedSigned<MAX, S>

Source§

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