`pad_XXX`/`align_XX` attributes write unexpected zeros.
- Dominant language
- Rust
- Stars
- 853
- Forks
- 56
- PR merge metrics
- No merged PRs in 30d
Description
Take the following:
```rust
pub struct BootImage {
pub header: BootImageHeader,
// Skip reading the individual ramdisk fragments.
#[brw(pad_size_to = header.ramdisk_size, align_after = header.page_size)]
pub ramdisk_fragments: (),
...
}
```
I'm reading `BootImage` from a file, changing a couple properties, and writing `BootImage` back to the same file. I was surprised to find out that the `ramdisk_fragments` part of my file was all zeros. I wanted to skip reading/writing the `ramdisk_fragments` since it's not relevant to my use case.
Reading the [documentation](https://docs.rs/binrw/latest/binrw/docs/attribute/index.html) for `align_before`, `align_after`, and `pad_size_to`, it says "Aligns the reader/writer to the Nth byte before a field." and "Ensures the reader/writer is always advanced at least N bytes.". It doesn't mention that it zero fills the space. Reading the [pad_after](https://github.com/jam1garner/binrw/blob/master/binrw_derive/src/binrw/codegen/write_options/struct_field.rs#L353) function shows the zeroing code.
I doubt we can change the behavior at this point, but maybe we can update the documentation?
I can work around my issue by using `seek_before`, it just makes the struct less readable.
Thanks!
Contributor guide
Research direction
Start with the binrw attribute documentation for align_before, align_after, and pad_size_to, then compare it with the linked pad_after implementation in binrw_derive. Clarify that these write operations zero-fill the skipped space, and consider mentioning seek_before as the alternative; the documentation should accurately describe the observed behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100