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

Add compile-time error when using brw on if attribute #148

Open
jam1garner opened this issue Aug 30, 2022 · 4 comments
Open

Add compile-time error when using brw on if attribute #148

jam1garner opened this issue Aug 30, 2022 · 4 comments

Comments

@jam1garner
Copy link
Owner

No description provided.

@csnover
Copy link
Collaborator

csnover commented Aug 30, 2022

The problem alluded to by this ticket exists for any rw directive that accesses fields because read fields are by-value whereas write fields are by-reference. It is actually fine to have #[brw(if)] when it doesn’t try to use fields (e.g. imports, globals).

@kitlith
Copy link
Collaborator

kitlith commented Aug 31, 2022

There are also ways to make it work by using auto(de)ref. Take this example:

#[binrw]
struct Test {
    #[brw(assert(field.ne(&0u8)))]
    field: u8,
}

by writing the condition as field.ne(&0u8) instead of field != 0u8 as usual, we can take advantage of autoref to write code that works in both cases. Alternatively, you could imagine an impl of PartialEq that allows you to compare a value and a ref.

@csnover
Copy link
Collaborator

csnover commented Sep 2, 2022

Is there a way to handle this in the general case (ab)using auto-deref so users don’t have to, or is this just some thing that should be documented?

@kitlith
Copy link
Collaborator

kitlith commented Sep 2, 2022

Unsure. For operators we might be able to get something to work by parsing and replacing uses of operators with uses of traits. This strikes me as error-prone, though.

I'd probably just go ahead and document it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

3 participants