[−][src]Struct bitter::BitGet
Reads little-endian bits platform agonistically from a slice of byte data.
Methods
impl<'a> BitGet<'a>
[src]
pub fn new(data: &'a [u8]) -> BitGet<'a>
[src]
pub fn read_u8(&mut self) -> Option<u8>
[src]
pub fn read_i8(&mut self) -> Option<i8>
[src]
pub fn read_u16(&mut self) -> Option<u16>
[src]
pub fn read_i16(&mut self) -> Option<i16>
[src]
pub fn read_u32(&mut self) -> Option<u32>
[src]
pub fn read_i32(&mut self) -> Option<i32>
[src]
pub fn read_u64(&mut self) -> Option<u64>
[src]
pub fn read_i64(&mut self) -> Option<i64>
[src]
pub fn read_u8_unchecked(&mut self) -> u8
[src]
pub fn read_i8_unchecked(&mut self) -> i8
[src]
pub fn read_u16_unchecked(&mut self) -> u16
[src]
pub fn read_i16_unchecked(&mut self) -> i16
[src]
pub fn read_u32_unchecked(&mut self) -> u32
[src]
pub fn read_i32_unchecked(&mut self) -> i32
[src]
pub fn read_u64_unchecked(&mut self) -> u64
[src]
pub fn read_i64_unchecked(&mut self) -> i64
[src]
pub fn read_u32_bits_unchecked(&mut self, bits: i32) -> u32
[src]
pub fn read_u32_bits(&mut self, bits: i32) -> Option<u32>
[src]
pub fn read_i32_bits_unchecked(&mut self, bits: i32) -> i32
[src]
pub fn read_i32_bits(&mut self, bits: i32) -> Option<i32>
[src]
pub fn approx_bytes_remaining(&self) -> usize
[src]
Return approximately how many bytes are left in the bitstream. This can overestimate by one when the bit position is non-zero. Thus it is recommended to always compare with a greater than sign to avoid undefined behavior with unchecked reads
let mut bitter = BitGet::new(&[0xff]); assert_eq!(bitter.approx_bytes_remaining(), 1); assert!(bitter.read_bit().is_some()); assert_eq!(bitter.approx_bytes_remaining(), 1); assert!(bitter.read_u32_bits(7).is_some()); assert_eq!(bitter.approx_bytes_remaining(), 0);
pub fn bits_remaining(&self) -> Option<usize>
[src]
Returns the exact number of bits remaining in the bitstream if the number of bits can fit
within a usize
. For large byte slices, the calculating the number of bits can cause an
overflow, hence an Option
is returned. See has_bits_remaining
for a more performant and
ergonomic alternative.
let mut bitter = BitGet::new(&[0xff]); assert_eq!(bitter.bits_remaining(), Some(8)); assert!(bitter.read_bit().is_some()); assert_eq!(bitter.bits_remaining(), Some(7)); assert!(bitter.read_u32_bits(7).is_some()); assert_eq!(bitter.bits_remaining(), Some(0));
pub fn has_bits_remaining(&self, bits: usize) -> bool
[src]
Returns true if at least bits
number of bits are left in the stream. A more performant
and ergonomic way than bits_remaining
.
let mut bitter = BitGet::new(&[0xff]); assert!(bitter.has_bits_remaining(7)); assert!(bitter.has_bits_remaining(8)); assert!(!bitter.has_bits_remaining(9)); assert!(bitter.read_bit().is_some()); assert!(bitter.has_bits_remaining(7)); assert!(!bitter.has_bits_remaining(8)); assert!(bitter.read_u32_bits(7).is_some()); assert!(!bitter.has_bits_remaining(7)); assert!(bitter.has_bits_remaining(0));
pub fn is_empty(&self) -> bool
[src]
Returns if the bitstream has no bits left
let mut bitter = BitGet::new(&[0b1010_1010, 0b0101_0101]); assert_eq!(bitter.is_empty(), false); assert_eq!(bitter.read_u16_unchecked(), 0b0101_0101_1010_1010); assert_eq!(bitter.is_empty(), true);
pub fn read_bit(&mut self) -> Option<bool>
[src]
Reads a bit from the bitstream if available
let mut bitter = BitGet::new(&[0b1010_1010, 0b0101_0101]); assert_eq!(bitter.read_bit(), Some(false));
pub fn read_bit_unchecked(&mut self) -> bool
[src]
Assumes there is at least one bit left in the stream.
let mut bitter = BitGet::new(&[0b1010_1010, 0b0101_0101]); assert_eq!(bitter.read_bit_unchecked(), false);
pub fn read_f32(&mut self) -> Option<f32>
[src]
Reads a f32
from the bitstream if available. The standard IEEE-754 binary layout float is
used.
pub fn read_f32_unchecked(&mut self) -> f32
[src]
Reads a f32
from the bitstream. The standard IEEE-754 binary layout float is used.
pub fn if_get<T, F>(&mut self, f: F) -> Option<Option<T>> where
F: FnMut(&mut Self) -> Option<T>,
[src]
F: FnMut(&mut Self) -> Option<T>,
If the next bit is available and on, decode the next chunk of data (which can return None). The return value can be one of the following:
- None: Not enough data was available
- Some(None): Bit was off so data not decoded
- Some(x): Bit was on and data was decoded
let mut bitter = BitGet::new(&[0xff, 0x04]); assert_eq!(bitter.if_get(BitGet::read_u8), Some(Some(0x7f))); assert_eq!(bitter.if_get(BitGet::read_u8), Some(None)); assert_eq!(bitter.if_get(BitGet::read_u8), None);
pub fn if_get_unchecked<T, F>(&mut self, f: F) -> Option<T> where
F: FnMut(&mut Self) -> T,
[src]
F: FnMut(&mut Self) -> T,
If the next bit is available and on, decode the next chunk of data. The return value can be one of the following:
- Some(None): Bit was off so data not decoded
- Some(x): Bit was on and data was decoded
let mut bitter = BitGet::new(&[0xff, 0x04]); assert_eq!(bitter.if_get_unchecked(BitGet::read_u8_unchecked), Some(0x7f)); assert_eq!(bitter.if_get_unchecked(BitGet::read_u8_unchecked), None);
Panics
Will panic if no data is left for the bit or for the data to be decoded.
pub fn read_bytes(&mut self, bytes: i32) -> Option<Cow<[u8]>>
[src]
If the number of requested bytes are available return them to the client. Since the current bit position may not be byte aligned, all bytes requested may need to be shifted appropriately.
let mut bitter = BitGet::new(&[0b1010_1010, 0b0101_0101]); assert_eq!(bitter.read_bit_unchecked(), false); assert_eq!(bitter.read_bytes(1).map(|x| x[0]), Some(0b1101_0101));
pub fn read_bits_max(&mut self, bits: i32, max: i32) -> Option<u32>
[src]
Reads a value that takes up at most bits
bits and doesn't exceed max
. This function
assumes that max
has the same bitwidth as bits
. It doesn't make sense to call this
function bits = 8
and max = 30
, you'd change your argument to bits = 5
. If bits
are
not available return None
// Reads 5 bits or stops if the 5th bit would send the accumulator over 20 let mut bitter = BitGet::new(&[0b1111_1000]); assert_eq!(bitter.read_bits_max(5, 20), Some(8));
pub fn read_bits_max_unchecked(&mut self, bits: i32, max: i32) -> u32
[src]
Reads a value that takes up at most bits
bits and doesn't exceed max
. This function
assumes that max
has the same bitwidth as bits
. It doesn't make sense to call this
function bits = 8
and max = 30
, you'd change your argument to bits = 5
// Reads 5 bits or stops if the 5th bit would send the accumulator over 20 let mut bitter = BitGet::new(&[0b1111_1000]); assert_eq!(bitter.read_bits_max(5, 20), Some(8));
Auto Trait Implementations
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,