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§
Sourcefn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> Result<Self>where
Self: Sized,
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.
Sourcefn read_var<const MAX: u32, R>(
reader: &mut R,
bits: BitCount<MAX>,
) -> Result<Self>
fn read_var<const MAX: u32, R>( reader: &mut R, bits: BitCount<MAX>, ) -> Result<Self>
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.
Sourcefn write<const BITS: u32, W: BitWrite + ?Sized>(
self,
writer: &mut W,
) -> Result<()>
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.
Sourcefn write_var<const MAX: u32, W: BitWrite + ?Sized>(
self,
writer: &mut W,
bits: BitCount<MAX>,
) -> Result<()>
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>>
impl Integer for Option<NonZero<u8>>
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, count: BitCount<MAX>, ) -> Result<Self>
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, count: BitCount<MAX>, ) -> Result<()>
Source§impl Integer for Option<NonZero<u16>>
impl Integer for Option<NonZero<u16>>
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, count: BitCount<MAX>, ) -> Result<Self>
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, count: BitCount<MAX>, ) -> Result<()>
Source§impl Integer for Option<NonZero<u32>>
impl Integer for Option<NonZero<u32>>
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, count: BitCount<MAX>, ) -> Result<Self>
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, count: BitCount<MAX>, ) -> Result<()>
Source§impl Integer for Option<NonZero<u64>>
impl Integer for Option<NonZero<u64>>
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, count: BitCount<MAX>, ) -> Result<Self>
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, count: BitCount<MAX>, ) -> Result<()>
Source§impl Integer for Option<NonZero<u128>>
impl Integer for Option<NonZero<u128>>
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, count: BitCount<MAX>, ) -> Result<Self>
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, count: BitCount<MAX>, ) -> Result<()>
Source§impl Integer for bool
Reading and writing booleans as Integer
requires the number of bits to be 1.
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 });
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, _: BitCount<MAX>) -> Result<Self>
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, _: BitCount<MAX>, ) -> Result<()>
Source§impl Integer for i8
impl Integer for i8
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>
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<()>
Source§impl Integer for i16
impl Integer for i16
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>
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<()>
Source§impl Integer for i32
impl Integer for i32
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>
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<()>
Source§impl Integer for i64
impl Integer for i64
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>
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<()>
Source§impl Integer for i128
impl Integer for i128
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>
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<()>
Source§impl Integer for u8
impl Integer for u8
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>
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<()>
Source§impl Integer for u16
impl Integer for u16
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>
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<()>
Source§impl Integer for u32
impl Integer for u32
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>
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<()>
Source§impl Integer for u64
impl Integer for u64
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>
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<()>
Source§impl Integer for u128
impl Integer for u128
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>
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<()>
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.
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]);
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, count: BitCount<MAX>, ) -> Result<Self>
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, 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.
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]);
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, count: BitCount<MAX>, ) -> Result<Self>
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, 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.
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]);
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, count: BitCount<MAX>, ) -> Result<Self>
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, 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.
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]);
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, count: BitCount<MAX>, ) -> Result<Self>
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, 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.
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]);