binrw
binrw copied to clipboard
Add `dbg` directive
Purely out of curiosity of how an implementation would feel, I implemented a dbg
directive.
To explain, here's the documentation for it:
The dbg
directive prints out information to stderr to help with quick-and-dirty debugging.
The information printed out:
- Source filename
- Line number in source file
- Offset in reader (in hex)
- Contents of parsed value (using hex format when available)
#[derive(BinRead)]
struct DbgExample {
first_value: u32,
#[br(dbg)]
second_value: u32,
}
Which will output something like...
[src/main.rs:5 | offset 0x4] second_value = 0xFFFE76
This is not necessarily something we need to add, just putting this here to (a) collect thoughts and (b) have an implementation if ever desired.
Just had to figure out a bug and dumping thoughts here about things a dbg implementation probably should have beyond what is in this PR:
- Show the position of the reader both before and after the read (if there is an IO error indicate that in place of the end-read position). This would be implicit in the start position of the next field, except that they can be padded/aligned and also the last field doesn’t have a next field
- Show padding/alignment operations when they occur
- Show errors when they occur
- Show any applied directive values, e.g. if someone has
if(foo)
show the value offoo
, if someone haspad_size_to(foo)
show thefoo
and what actually happened - Recursion through inner types?
I do believe this was superseded by https://github.com/jam1garner/binrw/pull/162.