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

count-less vectors at tail of struct #244

Closed sempervictus closed 6 months ago

sempervictus commented 6 months ago

Thanks for writing this, very neat library - took about an hour to convert a small project from doku. I'm running into the same problem though: buffers/streams containing bytes which map to

struct Abort {
    user_reason: u8,
    system_reason: u8,
    data: Vec<u8>
}

don't have any means by which to map the size of the tail byte vector. The ::read_to_end method seems the right way to handle the IO, but i'm having difficulty annotating the struct to let it compile since it seem to expect a count. Bit opaque with the error message:

   |
145 |     data: Vec<u8>
    |           ^^^ the trait `Default` is not implemented for `VecArgs<()>`

since there's no default value to speak-of; just a few bytes to read into a Vec<u8>. Am i missing something simple, or do i need to implement a reader to cover the constraints involved?

v1gnesh commented 6 months ago

Check this - https://docs.rs/binrw/latest/binrw/helpers/fn.until_eof.html

sempervictus commented 6 months ago

Oooh, shiny! Thank you very much. This is a reasonably common thing to process when handling nested protocol frames - might the use case merit some up-front-documented count sugar (count = helpers::remainder or such)?