Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct calc documentation #208

Closed
wants to merge 1 commit into from

Conversation

octylFractal
Copy link
Contributor

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

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

csnover commented Jun 28, 2023

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`

@csnover csnover closed this in 021e302 Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants