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

Correct `calc` documentation #208

Closed octylFractal closed 1 year ago

octylFractal commented 1 year ago

Calc works on any field that is present in the struct, since all fields are extracted at the start.

csnover commented 1 year ago

I don’t think this is a correct change? Codegen does not reorder calc to occur after all the other fields are read so I am confused by the comment in the commit. Only when writing can you access all fields of the structure, which is what the documentation says now. You can’t do this:

pub struct A {
    pub a: u8,
    #[br(calc(a + c))] // wrong, `c` does not exist yet
    pub b: u8,
    pub c: u8,
}

It will error:

error[E0425]: cannot find value `c` in this scope
  --> binrw/tests/example.rs:10:23
   |
10 |         #[br(calc(a + c))]
   |                       ^
   |                       |
   |                       a field by this name exists in `Self`
   |                       help: a local variable with a similar name exists: `a`