Skip to content

Commit

Permalink
Emit dbg code after field padding/alignment is applied
Browse files Browse the repository at this point in the history
Fixes #188.
  • Loading branch information
csnover committed Jun 29, 2023
1 parent e906d23 commit 8232754
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
8 changes: 4 additions & 4 deletions binrw/tests/dbg.rs
Expand Up @@ -11,15 +11,15 @@ fn dbg() {
#[br(big)]
struct Test {
before: u16,
#[br(dbg)]
#[br(dbg, pad_before = 2)]
value: u32,
#[br(dbg)]
inner: Inner,
}

// 🥴
if let Some("1") = option_env!("BINRW_IN_CHILD_PROC") {
Test::read(&mut Cursor::new(b"\0\0\0\0\0\x04\0\x0e\xff\xed")).unwrap();
Test::read(&mut Cursor::new(b"\0\0\xff\xff\0\0\0\x04\0\x0e\xff\xed")).unwrap();
} else {
use std::process::{Command, Stdio};

Expand All @@ -36,8 +36,8 @@ fn dbg() {
std::str::from_utf8(&result).unwrap(),
format!(
concat!(
"[{file}:{offset_0} | offset 0x2] value = 0x4\n",
"[{file}:{offset_1} | offset 0x6] inner = Inner(\n",
"[{file}:{offset_0} | offset 0x4] value = 0x4\n",
"[{file}:{offset_1} | offset 0x8] inner = Inner(\n",
" 0xeffed,\n",
")\n"
),
Expand Down
15 changes: 7 additions & 8 deletions binrw_derive/src/binrw/codegen/read_options/struct.rs
Expand Up @@ -151,10 +151,10 @@ fn generate_field(
.try_conversion(name, variant_name)
.map_value()
.deref_now()
.wrap_debug()
.wrap_seek()
.wrap_condition()
.assign_to_var()
.append_debug()
.append_assertions()
.wrap_restore_position()
.prefix_magic()
Expand Down Expand Up @@ -251,7 +251,7 @@ impl<'field> FieldGenerator<'field> {
}
}

fn append_debug(mut self) -> Self {
fn wrap_debug(mut self) -> Self {
// Unwrapping the proc-macro2 Span is undesirable but necessary until its API
// is updated to allow retrieving line/column again. Using a separate function
// to unwrap just to make it clearer what needs to be undone later.
Expand All @@ -276,16 +276,15 @@ impl<'field> FieldGenerator<'field> {
start_line.to_token_stream()
};

self.out = quote! {
self.out = quote! {{
let #SAVED_POSITION = #SEEK_TRAIT::seek(#reader_var, #SEEK_FROM::Current(0))?;

#head

let #TEMP = #head;
#DBG_EPRINTLN!(
"[{}:{} | offset {:#x}] {} = {:#x?}",
::core::file!(), #at, #SAVED_POSITION, ::core::stringify!(#ident), &#ident
::core::file!(), #at, #SAVED_POSITION, ::core::stringify!(#ident), &#TEMP
);
};
#TEMP
}};
}

self
Expand Down

0 comments on commit 8232754

Please sign in to comment.