mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 16:44:06 +02:00
format
This commit is contained in:
parent
7f491a7793
commit
aba52326be
1 changed files with 23 additions and 20 deletions
37
src/lib.rs
37
src/lib.rs
|
|
@ -5,8 +5,8 @@
|
||||||
// for bench on nightly
|
// for bench on nightly
|
||||||
//extern crate test;
|
//extern crate test;
|
||||||
|
|
||||||
pub use endianness::{BigEndian, LittleEndian};
|
|
||||||
use endianness::Endianness;
|
use endianness::Endianness;
|
||||||
|
pub use endianness::{BigEndian, LittleEndian};
|
||||||
use is_signed::IsSigned;
|
use is_signed::IsSigned;
|
||||||
use num_traits::{Float, PrimInt};
|
use num_traits::{Float, PrimInt};
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
|
|
@ -66,7 +66,6 @@ impl IsPadded for Padded {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Either the read bits in the requested format or a [`ReadError`](enum.ReadError.html)
|
/// Either the read bits in the requested format or a [`ReadError`](enum.ReadError.html)
|
||||||
pub type Result<T> = std::result::Result<T, ReadError>;
|
pub type Result<T> = std::result::Result<T, ReadError>;
|
||||||
|
|
||||||
|
|
@ -98,9 +97,9 @@ pub type Result<T> = std::result::Result<T, ReadError>;
|
||||||
/// let buffer = BitBuffer::from_padded_slice(bytes, 8, LittleEndian);
|
/// let buffer = BitBuffer::from_padded_slice(bytes, 8, LittleEndian);
|
||||||
/// ```
|
/// ```
|
||||||
pub struct BitBuffer<'a, E, S>
|
pub struct BitBuffer<'a, E, S>
|
||||||
where
|
where
|
||||||
E: Endianness,
|
E: Endianness,
|
||||||
S: IsPadded
|
S: IsPadded,
|
||||||
{
|
{
|
||||||
bytes: &'a [u8],
|
bytes: &'a [u8],
|
||||||
bit_len: usize,
|
bit_len: usize,
|
||||||
|
|
@ -110,8 +109,8 @@ pub struct BitBuffer<'a, E, S>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E> BitBuffer<'a, E, NonPadded>
|
impl<'a, E> BitBuffer<'a, E, NonPadded>
|
||||||
where
|
where
|
||||||
E: Endianness
|
E: Endianness,
|
||||||
{
|
{
|
||||||
/// Create a new BitBuffer from a byte slice
|
/// Create a new BitBuffer from a byte slice
|
||||||
///
|
///
|
||||||
|
|
@ -139,8 +138,8 @@ impl<'a, E> BitBuffer<'a, E, NonPadded>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E> BitBuffer<'a, E, Padded>
|
impl<'a, E> BitBuffer<'a, E, Padded>
|
||||||
where
|
where
|
||||||
E: Endianness
|
E: Endianness,
|
||||||
{
|
{
|
||||||
/// Create a new BitBuffer from a byte slice with included padding
|
/// Create a new BitBuffer from a byte slice with included padding
|
||||||
///
|
///
|
||||||
|
|
@ -164,7 +163,11 @@ impl<'a, E> BitBuffer<'a, E, Padded>
|
||||||
/// ```
|
/// ```
|
||||||
pub fn from_padded_slice(bytes: &'a [u8], byte_len: usize, _endianness: E) -> Self {
|
pub fn from_padded_slice(bytes: &'a [u8], byte_len: usize, _endianness: E) -> Self {
|
||||||
if bytes.len() < byte_len + USIZE_SIZE - 1 {
|
if bytes.len() < byte_len + USIZE_SIZE - 1 {
|
||||||
panic!("not enough padding bytes, {} required, {} provided", USIZE_SIZE - 1, byte_len - bytes.len())
|
panic!(
|
||||||
|
"not enough padding bytes, {} required, {} provided",
|
||||||
|
USIZE_SIZE - 1,
|
||||||
|
byte_len - bytes.len()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
BitBuffer {
|
BitBuffer {
|
||||||
bytes,
|
bytes,
|
||||||
|
|
@ -177,9 +180,9 @@ impl<'a, E> BitBuffer<'a, E, Padded>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E, S> BitBuffer<'a, E, S>
|
impl<'a, E, S> BitBuffer<'a, E, S>
|
||||||
where
|
where
|
||||||
E: Endianness,
|
E: Endianness,
|
||||||
S: IsPadded
|
S: IsPadded,
|
||||||
{
|
{
|
||||||
/// 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 {
|
||||||
|
|
@ -277,8 +280,8 @@ impl<'a, E, S> BitBuffer<'a, E, S>
|
||||||
/// assert_eq!(result, 0b100_0110_10);
|
/// assert_eq!(result, 0b100_0110_10);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn read<T>(&self, position: usize, count: usize) -> Result<T>
|
pub fn read<T>(&self, position: usize, count: usize) -> Result<T>
|
||||||
where
|
where
|
||||||
T: PrimInt + BitOrAssign + IsSigned,
|
T: PrimInt + BitOrAssign + IsSigned,
|
||||||
{
|
{
|
||||||
let value = {
|
let value = {
|
||||||
let type_bit_size = size_of::<T>() * 8;
|
let type_bit_size = size_of::<T>() * 8;
|
||||||
|
|
@ -391,8 +394,8 @@ impl<'a, E, S> BitBuffer<'a, E, S>
|
||||||
/// let result = buffer.read_float::<f32>(10).unwrap();
|
/// let result = buffer.read_float::<f32>(10).unwrap();
|
||||||
/// ```
|
/// ```
|
||||||
pub fn read_float<T>(&self, position: usize) -> Result<T>
|
pub fn read_float<T>(&self, position: usize) -> Result<T>
|
||||||
where
|
where
|
||||||
T: Float,
|
T: Float,
|
||||||
{
|
{
|
||||||
if size_of::<T>() == 4 {
|
if size_of::<T>() == 4 {
|
||||||
let int = self.read::<u32>(position, 32)?;
|
let int = self.read::<u32>(position, 32)?;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue