/// # Safety /// /// ptr and len must satisfy the requirements of [`core::slice::from_raw_parts`]. #[inline(always)] pubunsafefn update_slice(&mutself, ptr: *const u8, len: usize) { let end = ptr.wrapping_add(len);
#[inline(always)] pubfn bytes_remaining(&self) -> usize { self.end as usize - self.ptr as usize
}
#[inline(always)] pubfn bytes_remaining_including_buffer(&self) -> usize {
(self.end as usize - self.ptr as usize) + (self.bits_used as usize >> 3)
}
#[inline(always)] pubfn need_bits(&mutself, n: usize) -> Result<(), ReturnCode> { while (self.bits_used as usize) < n { self.pull_byte()?;
}
Ok(())
}
/// Remove zero to seven bits as needed to go to a byte boundary #[inline(always)] pubfn next_byte_boundary(&mutself) { self.bit_buffer >>= self.bits_used & 0b0111; self.bits_used -= self.bits_used & 0b0111;
}
self.bit_buffer |= (byte as u64) << self.bits_used; self.bits_used += 8;
Ok(byte)
}
#[inline(always)] /// Copy enough bytes from the BitReader's underlying slice to fill the internal /// bit buffer with 7 bytes of data. /// /// Safety: /// /// `self.ptr` must point to at least 8 readable bytes, as indicated by `bytes_remaining()` pubunsafefn refill(&mutself) {
debug_assert!(self.bytes_remaining() >= 8);
// SAFETY: caller ensures we have 8 bytes to read for a u64. let read = unsafe { core::ptr::read_unaligned(self.ptr.cast::<u64>()) }.to_le(); self.bit_buffer |= read << self.bits_used; // this xor was previously a subtraction but was changed for performance reasons. // for bits_used between 0 and 63 (inclusive), it will always have the same behavior. let increment = (63 ^ self.bits_used) >> 3; self.ptr = self.ptr.wrapping_add(increment as usize); self.bits_used |= 56;
}
#[inline(always)] pubfn bits(&mutself, n: usize) -> u64 { // debug_assert!( n <= self.bits_used, "{n} bits requested, but only {} avaliable", self.bits_used);
#[inline(always)] pubfn prime(&mutself, bits: u8, value: u64) { let value = value & ((1 << bits) - 1); self.bit_buffer += value << self.bits_used; self.bits_used += bits;
}
#[inline(always)] pubfn return_unused_bytes(&mutself) { let len = self.bits_used >> 3; // SAFETY: ptr is advanced whenever bits_used is incremented by 8, so this sub is always // in bounds. self.ptr = unsafe { self.ptr.sub(len as usize) }; self.bits_used -= len << 3; self.bit_buffer &= (1u64 << self.bits_used) - 1u64;
assert!(self.bits_used <= 32);
}
}
#[cfg(feature = "std")] impl std::io::Read for BitReader<'_> { fn read(&mutself, buf: &mut [u8]) -> std::io::Result<usize> {
assert_eq!(self.bits_used, 0, "bit buffer not cleared before read");
let number_of_bytes = Ord::min(buf.len(), self.bytes_remaining());
// SAFETY: `buf` is a mutable (exclusive) reference, so it cannot overlap the memory that // the reader contains unsafe { core::ptr::copy_nonoverlapping(self.ptr, buf.as_mut_ptr(), number_of_bytes) }
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.