1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 16:44:06 +02:00
This commit is contained in:
Robin Appelman 2019-03-03 02:33:59 +01:00
commit 48a0c85d19

View file

@ -7,10 +7,10 @@ use std::ops::BitOrAssign;
use num_traits::{Float, PrimInt}; use num_traits::{Float, PrimInt};
use crate::{ReadError, Result};
use crate::endianness::Endianness; use crate::endianness::Endianness;
use crate::is_signed::IsSigned; use crate::is_signed::IsSigned;
use crate::unchecked_primitive::{UncheckedPrimitiveFloat, UncheckedPrimitiveInt}; use crate::unchecked_primitive::{UncheckedPrimitiveFloat, UncheckedPrimitiveInt};
use crate::{ReadError, Result};
const USIZE_SIZE: usize = size_of::<usize>(); const USIZE_SIZE: usize = size_of::<usize>();
@ -34,8 +34,8 @@ const USIZE_SIZE: usize = size_of::<usize>();
/// # } /// # }
/// ``` /// ```
pub struct BitBuffer<E> pub struct BitBuffer<E>
where where
E: Endianness, E: Endianness,
{ {
bytes: Vec<u8>, bytes: Vec<u8>,
bit_len: usize, bit_len: usize,
@ -44,8 +44,8 @@ pub struct BitBuffer<E>
} }
impl<E> BitBuffer<E> impl<E> BitBuffer<E>
where where
E: Endianness, E: Endianness,
{ {
/// Create a new BitBuffer from a byte vector /// Create a new BitBuffer from a byte vector
/// ///
@ -72,8 +72,8 @@ impl<E> BitBuffer<E>
} }
impl<E> BitBuffer<E> impl<E> BitBuffer<E>
where where
E: Endianness, E: Endianness,
{ {
/// The available number of bits in the buffer /// The available number of bits in the buffer
pub fn bit_len(&self) -> usize { pub fn bit_len(&self) -> usize {
@ -178,8 +178,8 @@ impl<E> BitBuffer<E>
/// [`ReadError::NotEnoughData`]: enum.ReadError.html#variant.NotEnoughData /// [`ReadError::NotEnoughData`]: enum.ReadError.html#variant.NotEnoughData
/// [`ReadError::TooManyBits`]: enum.ReadError.html#variant.TooManyBits /// [`ReadError::TooManyBits`]: enum.ReadError.html#variant.TooManyBits
pub fn read_int<T>(&self, position: usize, count: usize) -> Result<T> pub fn read_int<T>(&self, position: usize, count: usize) -> Result<T>
where where
T: PrimInt + BitOrAssign + IsSigned + UncheckedPrimitiveInt, T: PrimInt + BitOrAssign + IsSigned + UncheckedPrimitiveInt,
{ {
let value = { let value = {
let type_bit_size = size_of::<T>() * 8; let type_bit_size = size_of::<T>() * 8;
@ -414,8 +414,8 @@ impl<E> BitBuffer<E>
/// ///
/// [`ReadError::NotEnoughData`]: enum.ReadError.html#variant.NotEnoughData /// [`ReadError::NotEnoughData`]: enum.ReadError.html#variant.NotEnoughData
pub fn read_float<T>(&self, position: usize) -> Result<T> pub fn read_float<T>(&self, position: usize) -> Result<T>
where where
T: Float + UncheckedPrimitiveFloat, T: Float + UncheckedPrimitiveFloat,
{ {
if position + size_of::<T>() * 8 > self.bit_len { if position + size_of::<T>() * 8 > self.bit_len {
if position > self.bit_len { if position > self.bit_len {