mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 16:44:06 +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 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))]
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
#[derive(Debug)]
|
||||
pub struct BigEndian;
|
||||
|
||||
/// Marks the buffer or stream as little endian
|
||||
#[derive(Debug)]
|
||||
pub struct LittleEndian;
|
||||
|
||||
macro_rules! impl_endianness {
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ use crate::{BitRead, BitReadSized, ReadError, Result};
|
|||
/// ```
|
||||
///
|
||||
/// [`BitBuffer`]: struct.BitBuffer.html
|
||||
#[derive(Debug)]
|
||||
pub struct BitStream<E>
|
||||
where
|
||||
E: Endianness,
|
||||
|
|
|
|||
|
|
@ -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)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue