jam1garner / binrw

A Rust crate for helping parse and rebuild binary data using ✨macro magic✨.
https://binrw.rs
MIT License
545 stars 35 forks source link

[Feature] Optionally specify padding value in derive directives #205

Open widberg opened 1 year ago

widberg commented 1 year ago

The align_after, align_before, pad_after, pad_before, and pad_size_to directives always write 0x00 bytes. I propose that these directives optionally accept a byte value to use when writing padding, keeping 0x00 as the default value when one is not present. For example #[bw(align_after(2048, 0xFF))] would write 0xFF instead of 0x00. The exact syntax is not important. It is possible that a user may want to use a different padding value for the before and after padding, so this should be specified for each directive rather than at the field or struct level.

This form of the directives does not make much sense in br since the value would serve no purpose when reading; however, the following code sample

#[br(align_after(2048))]
#[bw(align_after(2048, 0xFF))]

strikes me as odd and needlessly duplicated in the case where the padding size is the same for both reading and writing. Perhaps #[brw(align_after(2048, 0xFF))] should be allowed?

The feature would improve the quality of my binrw code by eliminating unnecessary write_withs that only serve to pad with a different value. The format I am implementing a writer for uses different values when padding different fields and I need to match this in order for the hashes to match when roundtripping a file.