Trait Integer

Source
pub trait Integer {
    // Required methods
    fn read<const BITS: u32, R: BitRead + ?Sized>(
        reader: &mut R,
    ) -> Result<Self>
       where Self: Sized;
    fn read_var<const MAX: u32, R>(
        reader: &mut R,
        bits: BitCount<MAX>,
    ) -> Result<Self>
       where R: BitRead + ?Sized,
             Self: Sized;
    fn write<const BITS: u32, W: BitWrite + ?Sized>(
        self,
        writer: &mut W,
    ) -> Result<()>;
    fn write_var<const MAX: u32, W: BitWrite + ?Sized>(
        self,
        writer: &mut W,
        bits: BitCount<MAX>,
    ) -> Result<()>;
}
Expand description

This trait is for integer types which can be read or written to a bit stream as a partial amount of bits.

It unifies signed and unsigned integer types by delegating reads and writes to the signed and unsigned reading and writing methods as appropriate.

Required Methods§

Source

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Reads a value of ourself from the stream with the given number of bits.

§Errors

Passes along any I/O error from the underlying stream. A compile-time error occurs if the given number of bits is larger than our type.

Source

fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Reads a value of ourself from the stream with the given number of bits.

§Errors

Passes along any I/O error from the underlying stream. Also returns an error if our type is too small to hold the requested number of bits.

Source

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

Writes ourself to the stream using the given const number of bits.

§Errors

Passes along any I/O error from the underlying stream. Returns an error if our value is too large to fit the given number of bits. A compile-time error occurs if the given number of bits is larger than our type.

Source

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, bits: BitCount<MAX>, ) -> Result<()>

Writes ourself to the stream using the given number of bits.

§Errors

Passes along any I/O error from the underlying stream. Returns an error if our value is too small to hold the given number of bits. Returns an error if our value is too large to fit the given number of bits.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Integer for Option<NonZero<u8>>

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, count: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, count: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for Option<NonZero<u16>>

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, count: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, count: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for Option<NonZero<u32>>

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, count: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, count: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for Option<NonZero<u64>>

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, count: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, count: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for Option<NonZero<u128>>

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, count: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, count: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for bool

Reading and writing booleans as Integer requires the number of bits to be 1.

This is more useful when combined with the fixed array target for reading blocks of bit flags.

§Example

use bitstream_io::{BitReader, BitRead, BigEndian};

#[derive(Debug, PartialEq, Eq)]
struct Flags {
    a: bool,
    b: bool,
    c: bool,
    d: bool,
}

let data: &[u8] = &[0b1011_0000];
let mut r = BitReader::endian(data, BigEndian);
// note the number of bits must be 1 per read
// while the quantity of flags is indicated by the array length
let flags = r.read::<1, [bool; 4]>().map(|[a, b, c, d]| Flags { a, b, c, d }).unwrap();
assert_eq!(flags, Flags { a: true, b: false, c: true, d: true });
Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>(reader: &mut R, _: BitCount<MAX>) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, _: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for i8

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, bits: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for i16

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, bits: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for i32

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, bits: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for i64

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, bits: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for i128

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, bits: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for u8

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, bits: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for u16

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, bits: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for u32

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, bits: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for u64

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, bits: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for u128

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, bits: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for NonZero<u8>

Unsigned NonZero types increment their value by 1 when being read and decrement it by 1 when being written.

§Examples

use bitstream_io::{BitReader, BitRead, BigEndian};
use core::num::NonZero;

let data: &[u8] = &[0b001_00000];
// reading a regular u8 in 3 bits yields 1
assert_eq!(BitReader::endian(data, BigEndian).read::<3, u8>().unwrap(), 1);
// reading a NonZero<u8> in 3 bits of the same data yields 2
assert_eq!(BitReader::endian(data, BigEndian).read::<3, NonZero<u8>>().unwrap().get(), 2);
use bitstream_io::{BitWriter, BitWrite, BigEndian};
use core::num::NonZero;

let mut w = BitWriter::endian(vec![], BigEndian);
// writing 1 as a regular u8 in 3 bits
w.write::<3, u8>(1).unwrap();
w.byte_align();
assert_eq!(w.into_writer(), &[0b001_00000]);

let mut w = BitWriter::endian(vec![], BigEndian);
// writing 1 as a NonZero<u8> in 3 bits
w.write::<3, NonZero<u8>>(NonZero::new(1).unwrap()).unwrap();
w.byte_align();
assert_eq!(w.into_writer(), &[0b000_00000]);
Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, count: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, count: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for NonZero<u16>

Unsigned NonZero types increment their value by 1 when being read and decrement it by 1 when being written.

§Examples

use bitstream_io::{BitReader, BitRead, BigEndian};
use core::num::NonZero;

let data: &[u8] = &[0b001_00000];
// reading a regular u8 in 3 bits yields 1
assert_eq!(BitReader::endian(data, BigEndian).read::<3, u8>().unwrap(), 1);
// reading a NonZero<u8> in 3 bits of the same data yields 2
assert_eq!(BitReader::endian(data, BigEndian).read::<3, NonZero<u8>>().unwrap().get(), 2);
use bitstream_io::{BitWriter, BitWrite, BigEndian};
use core::num::NonZero;

let mut w = BitWriter::endian(vec![], BigEndian);
// writing 1 as a regular u8 in 3 bits
w.write::<3, u8>(1).unwrap();
w.byte_align();
assert_eq!(w.into_writer(), &[0b001_00000]);

let mut w = BitWriter::endian(vec![], BigEndian);
// writing 1 as a NonZero<u8> in 3 bits
w.write::<3, NonZero<u8>>(NonZero::new(1).unwrap()).unwrap();
w.byte_align();
assert_eq!(w.into_writer(), &[0b000_00000]);
Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, count: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, count: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for NonZero<u32>

Unsigned NonZero types increment their value by 1 when being read and decrement it by 1 when being written.

§Examples

use bitstream_io::{BitReader, BitRead, BigEndian};
use core::num::NonZero;

let data: &[u8] = &[0b001_00000];
// reading a regular u8 in 3 bits yields 1
assert_eq!(BitReader::endian(data, BigEndian).read::<3, u8>().unwrap(), 1);
// reading a NonZero<u8> in 3 bits of the same data yields 2
assert_eq!(BitReader::endian(data, BigEndian).read::<3, NonZero<u8>>().unwrap().get(), 2);
use bitstream_io::{BitWriter, BitWrite, BigEndian};
use core::num::NonZero;

let mut w = BitWriter::endian(vec![], BigEndian);
// writing 1 as a regular u8 in 3 bits
w.write::<3, u8>(1).unwrap();
w.byte_align();
assert_eq!(w.into_writer(), &[0b001_00000]);

let mut w = BitWriter::endian(vec![], BigEndian);
// writing 1 as a NonZero<u8> in 3 bits
w.write::<3, NonZero<u8>>(NonZero::new(1).unwrap()).unwrap();
w.byte_align();
assert_eq!(w.into_writer(), &[0b000_00000]);
Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, count: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, count: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for NonZero<u64>

Unsigned NonZero types increment their value by 1 when being read and decrement it by 1 when being written.

§Examples

use bitstream_io::{BitReader, BitRead, BigEndian};
use core::num::NonZero;

let data: &[u8] = &[0b001_00000];
// reading a regular u8 in 3 bits yields 1
assert_eq!(BitReader::endian(data, BigEndian).read::<3, u8>().unwrap(), 1);
// reading a NonZero<u8> in 3 bits of the same data yields 2
assert_eq!(BitReader::endian(data, BigEndian).read::<3, NonZero<u8>>().unwrap().get(), 2);
use bitstream_io::{BitWriter, BitWrite, BigEndian};
use core::num::NonZero;

let mut w = BitWriter::endian(vec![], BigEndian);
// writing 1 as a regular u8 in 3 bits
w.write::<3, u8>(1).unwrap();
w.byte_align();
assert_eq!(w.into_writer(), &[0b001_00000]);

let mut w = BitWriter::endian(vec![], BigEndian);
// writing 1 as a NonZero<u8> in 3 bits
w.write::<3, NonZero<u8>>(NonZero::new(1).unwrap()).unwrap();
w.byte_align();
assert_eq!(w.into_writer(), &[0b000_00000]);
Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, count: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, count: BitCount<MAX>, ) -> Result<()>

Source§

impl Integer for NonZero<u128>

Unsigned NonZero types increment their value by 1 when being read and decrement it by 1 when being written.

§Examples

use bitstream_io::{BitReader, BitRead, BigEndian};
use core::num::NonZero;

let data: &[u8] = &[0b001_00000];
// reading a regular u8 in 3 bits yields 1
assert_eq!(BitReader::endian(data, BigEndian).read::<3, u8>().unwrap(), 1);
// reading a NonZero<u8> in 3 bits of the same data yields 2
assert_eq!(BitReader::endian(data, BigEndian).read::<3, NonZero<u8>>().unwrap().get(), 2);
use bitstream_io::{BitWriter, BitWrite, BigEndian};
use core::num::NonZero;

let mut w = BitWriter::endian(vec![], BigEndian);
// writing 1 as a regular u8 in 3 bits
w.write::<3, u8>(1).unwrap();
w.byte_align();
assert_eq!(w.into_writer(), &[0b001_00000]);

let mut w = BitWriter::endian(vec![], BigEndian);
// writing 1 as a NonZero<u8> in 3 bits
w.write::<3, NonZero<u8>>(NonZero::new(1).unwrap()).unwrap();
w.byte_align();
assert_eq!(w.into_writer(), &[0b000_00000]);
Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, count: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, count: BitCount<MAX>, ) -> Result<()>

Source§

impl<const SIZE: usize, I: Integer + Copy + Default> Integer for [I; SIZE]

Source§

fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>
where Self: Sized,

Source§

fn read_var<const MAX: u32, R>( reader: &mut R, count: BitCount<MAX>, ) -> Result<Self>
where R: BitRead + ?Sized, Self: Sized,

Source§

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

Source§

fn write_var<const MAX: u32, W: BitWrite + ?Sized>( self, writer: &mut W, count: BitCount<MAX>, ) -> Result<()>

Implementors§