mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 08:34:07 +02:00
implement Debug
This commit is contained in:
parent
29ca7e359e
commit
6e36d00c1e
6 changed files with 48 additions and 14 deletions
|
|
@ -107,7 +107,10 @@ extern crate proc_macro;
|
||||||
use proc_macro2::{Span, TokenStream};
|
use proc_macro2::{Span, TokenStream};
|
||||||
use quote::{quote, quote_spanned};
|
use quote::{quote, quote_spanned};
|
||||||
use syn::spanned::Spanned;
|
use syn::spanned::Spanned;
|
||||||
use syn::{parse_macro_input, parse_quote, Attribute, Data, DeriveInput, Expr, Fields, GenericParam, Generics, Ident, Lit, LitStr, Meta, parse_str};
|
use syn::{
|
||||||
|
parse_macro_input, parse_quote, parse_str, Attribute, Data, DeriveInput, Expr, Fields,
|
||||||
|
GenericParam, Generics, Ident, Lit, LitStr, Meta,
|
||||||
|
};
|
||||||
|
|
||||||
/// See the [crate documentation](index.html) for details
|
/// See the [crate documentation](index.html) for details
|
||||||
#[proc_macro_derive(BitRead, attributes(size, size_bits, discriminant_bits, discriminant))]
|
#[proc_macro_derive(BitRead, attributes(size, size_bits, discriminant_bits, discriminant))]
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,16 @@
|
||||||
use crate::endianness::Endianness;
|
|
||||||
use crate::is_signed::IsSigned;
|
|
||||||
use crate::{ReadError, Result};
|
|
||||||
use num_traits::{Float, PrimInt};
|
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::Debug;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::mem::size_of;
|
use std::mem::size_of;
|
||||||
use std::ops::BitOrAssign;
|
use std::ops::BitOrAssign;
|
||||||
|
|
||||||
|
use num_traits::{Float, PrimInt};
|
||||||
|
|
||||||
|
use crate::endianness::Endianness;
|
||||||
|
use crate::is_signed::IsSigned;
|
||||||
|
use crate::{ReadError, Result};
|
||||||
|
|
||||||
const USIZE_SIZE: usize = size_of::<usize>();
|
const USIZE_SIZE: usize = size_of::<usize>();
|
||||||
|
|
||||||
/// Buffer that allows reading integers of arbitrary bit length and non byte-aligned integers
|
/// Buffer that allows reading integers of arbitrary bit length and non byte-aligned integers
|
||||||
|
|
@ -290,7 +294,7 @@ where
|
||||||
let usable_bytes = if E::is_le() {
|
let usable_bytes = if E::is_le() {
|
||||||
&bytes[0..read]
|
&bytes[0..read]
|
||||||
} else {
|
} else {
|
||||||
&bytes[8-read..8]
|
&bytes[8 - read..8]
|
||||||
};
|
};
|
||||||
data.extend_from_slice(usable_bytes);
|
data.extend_from_slice(usable_bytes);
|
||||||
byte_left -= read;
|
byte_left -= read;
|
||||||
|
|
@ -406,3 +410,18 @@ impl<E: Endianness> From<Vec<u8>> for BitBuffer<E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<E: Endianness> Debug for BitBuffer<E> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"BitBuffer {{ bit_len: {}, endianness: {} }}",
|
||||||
|
self.bit_len,
|
||||||
|
if E::is_le() {
|
||||||
|
"LittleEndian"
|
||||||
|
} else {
|
||||||
|
"BigEndian"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,11 @@ pub trait Endianness {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Marks the buffer or stream as big endian
|
/// Marks the buffer or stream as big endian
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct BigEndian;
|
pub struct BigEndian;
|
||||||
|
|
||||||
/// Marks the buffer or stream as little endian
|
/// Marks the buffer or stream as little endian
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct LittleEndian;
|
pub struct LittleEndian;
|
||||||
|
|
||||||
macro_rules! impl_endianness {
|
macro_rules! impl_endianness {
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ use crate::{BitRead, BitReadSized, ReadError, Result};
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`BitBuffer`]: struct.BitBuffer.html
|
/// [`BitBuffer`]: struct.BitBuffer.html
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct BitStream<E>
|
pub struct BitStream<E>
|
||||||
where
|
where
|
||||||
E: Endianness,
|
E: Endianness,
|
||||||
|
|
|
||||||
|
|
@ -237,15 +237,21 @@ fn test_from() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_read_str_be() {
|
fn test_read_str_be() {
|
||||||
let bytes = vec![
|
let bytes = vec![
|
||||||
0x48, 0x65, 0x6c, 0x6c,
|
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0, 0, 0, 0, 0,
|
||||||
0x6f, 0x20, 0x77, 0x6f,
|
|
||||||
0x72, 0x6c, 0x64, 0,
|
|
||||||
0, 0, 0, 0
|
|
||||||
];
|
];
|
||||||
let buffer = BitBuffer::new(bytes, BigEndian);
|
let buffer = BitBuffer::new(bytes, BigEndian);
|
||||||
assert_eq!(buffer.read_string(0, Some(13)).unwrap(), "Hello world".to_owned());
|
assert_eq!(
|
||||||
assert_eq!(buffer.read_string(0, Some(16)).unwrap(), "Hello world".to_owned());
|
buffer.read_string(0, Some(13)).unwrap(),
|
||||||
assert_eq!(buffer.read_string(0, None).unwrap(), "Hello world".to_owned());
|
"Hello world".to_owned()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
buffer.read_string(0, Some(16)).unwrap(),
|
||||||
|
"Hello world".to_owned()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
buffer.read_string(0, None).unwrap(),
|
||||||
|
"Hello world".to_owned()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -292,6 +298,9 @@ fn read_sized_trait() {
|
||||||
hashmap!(0b1011_0101 => 0b0110_1010, 0b1010_1100 => 0b1001_1001),
|
hashmap!(0b1011_0101 => 0b0110_1010, 0b1010_1100 => 0b1001_1001),
|
||||||
result
|
result
|
||||||
);
|
);
|
||||||
|
stream.set_pos(0).unwrap();
|
||||||
|
let mut result: BitStream<BigEndian> = stream.read_sized(4).unwrap();
|
||||||
|
assert_eq!(0b10u8, result.read_int(2).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(BitRead, PartialEq, Debug)]
|
#[derive(BitRead, PartialEq, Debug)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue