Type Alias CheckedSigned

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

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<const MAX: u32, S: SignedInteger> Checkable for CheckedSigned<MAX, S>

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, S: SignedInteger> CheckablePrimitive for CheckedSigned<MAX, S>

Source§

type CountType = SignedBitCount<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