Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with id_pat and read_bytes_const #479

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deku-derive/src/macros/deku_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
};

let variant_read = quote! {
__deku_reader.last_bits_read_amt = 0;
#variant_id_read

#(#pre_match_tokens)*
Expand Down
52 changes: 50 additions & 2 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct Reader<'a, R: Read + Seek> {
inner: &'a mut R,
/// bits stored from previous reads that didn't read to the end of a byte size
leftover: Option<Leftover>,
/// Amount of bits read during the last call to [read_bits](Reader::read_bits) and [read_bytes](Reader::read_bytes)
/// Amount of bits read after last read, reseted before reading enum ids
pub last_bits_read_amt: usize,
/// Amount of bits read during the use of [read_bits](Reader::read_bits) and [read_bytes](Reader::read_bytes)
pub bits_read: usize,
Expand Down Expand Up @@ -289,7 +289,7 @@ impl<'a, R: Read + Seek> Reader<'a, R> {
}

let bits_read = ret.len();
self.last_bits_read_amt = bits_read;
self.last_bits_read_amt += bits_read;
self.bits_read += bits_read;

#[cfg(feature = "logging")]
Expand Down Expand Up @@ -518,4 +518,52 @@ mod tests {
let _ = reader.read_bytes(1, &mut buf);
assert_eq!([0xaa], buf);
}

#[test]
fn test_seek_last_read_bytes() {
// bytes
let input = hex!("aa");
let mut cursor = Cursor::new(input);
let mut reader = Reader::new(&mut cursor);
let mut buf = [0; 1];
let _ = reader.read_bytes(1, &mut buf);
assert_eq!([0xaa], buf);
reader.seek_last_read().unwrap();
let _ = reader.read_bytes(1, &mut buf);
assert_eq!([0xaa], buf);

// 2 bytes (and const)
let input = hex!("aabb");
let mut cursor = Cursor::new(input);
let mut reader = Reader::new(&mut cursor);
let mut buf = [0; 2];
let _ = reader.read_bytes_const::<2>(&mut buf);
assert_eq!([0xaa, 0xbb], buf);
reader.seek_last_read().unwrap();
let _ = reader.read_bytes_const::<2>(&mut buf);
assert_eq!([0xaa, 0xbb], buf);
}

#[cfg(feature = "bits")]
#[test]
fn test_seek_last_read_bits() {
let input = hex!("ab");
let mut cursor = Cursor::new(input);
let mut reader = Reader::new(&mut cursor);
let bits = reader.read_bits(4).unwrap();
assert_eq!(bits, Some(bitvec![u8, Msb0; 1, 0, 1, 0]));
reader.seek_last_read().unwrap();
let bits = reader.read_bits(4).unwrap();
assert_eq!(bits, Some(bitvec![u8, Msb0; 1, 0, 1, 0]));

// more than byte
let input = hex!("abd0");
let mut cursor = Cursor::new(input);
let mut reader = Reader::new(&mut cursor);
let bits = reader.read_bits(9).unwrap();
assert_eq!(bits, Some(bitvec![u8, Msb0; 1, 0, 1, 0, 1, 0, 1, 1, 1]));
reader.seek_last_read().unwrap();
let bits = reader.read_bits(9).unwrap();
assert_eq!(bits, Some(bitvec![u8, Msb0; 1, 0, 1, 0, 1, 0, 1, 1, 1]));
}
}
Loading