1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 08:34:07 +02:00

implement Debug

This commit is contained in:
Robin Appelman 2019-02-28 22:34:06 +01:00
commit 6e36d00c1e
6 changed files with 48 additions and 14 deletions

View file

@ -107,7 +107,10 @@ extern crate proc_macro;
use proc_macro2::{Span, TokenStream};
use quote::{quote, quote_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
#[proc_macro_derive(BitRead, attributes(size, size_bits, discriminant_bits, discriminant))]

View file

@ -211,4 +211,4 @@ fn test_read_struct2() {
},
stream.read().unwrap()
);
}
}

View file

@ -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::fmt;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::mem::size_of;
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>();
/// 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() {
&bytes[0..read]
} else {
&bytes[8-read..8]
&bytes[8 - read..8]
};
data.extend_from_slice(usable_bytes);
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"
}
)
}
}

View file

@ -7,9 +7,11 @@ pub trait Endianness {
}
/// Marks the buffer or stream as big endian
#[derive(Debug)]
pub struct BigEndian;
/// Marks the buffer or stream as little endian
#[derive(Debug)]
pub struct LittleEndian;
macro_rules! impl_endianness {

View file

@ -25,6 +25,7 @@ use crate::{BitRead, BitReadSized, ReadError, Result};
/// ```
///
/// [`BitBuffer`]: struct.BitBuffer.html
#[derive(Debug)]
pub struct BitStream<E>
where
E: Endianness,

View file

@ -237,15 +237,21 @@ fn test_from() {
#[test]
fn test_read_str_be() {
let bytes = vec![
0x48, 0x65, 0x6c, 0x6c,
0x6f, 0x20, 0x77, 0x6f,
0x72, 0x6c, 0x64, 0,
0, 0, 0, 0
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0, 0, 0, 0, 0,
];
let buffer = BitBuffer::new(bytes, BigEndian);
assert_eq!(buffer.read_string(0, Some(13)).unwrap(), "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());
assert_eq!(
buffer.read_string(0, Some(13)).unwrap(),
"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]
@ -292,6 +298,9 @@ fn read_sized_trait() {
hashmap!(0b1011_0101 => 0b0110_1010, 0b1010_1100 => 0b1001_1001),
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)]