-
Notifications
You must be signed in to change notification settings - Fork 40
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 bw(if(expr))
and brw(if(expr))
to skip writing conditionally
#113
Comments
@Holzhaus the currently recommended pattern for this is using the behavior of optionals in binrw (which is that on so one potential option is using: #[bw(map = |field| (version >= 5).then(|| field))]] or if that's a bit too verbose/polluting, you can get even more fancy and have the following helper function: fn write_if<T>(condition: impl FnOnce() -> bool) -> impl FnOnce(T) -> Option<T> {
move |field| condition().then(move || field)
} Which can then be used like so: #[bw(map = write_if(|| version >= 5))] I do think your idea make sense, however, as that makes sense for bidirectionality of the attribute. I had originally gotten it in my head that it didn't make sense as "not involving Default for writing doesn't make sense", however having given it more thought it sorta does? The only concern being that populating the field with a non-default value then being confused it isn't written seems possible. (Also I should note that presently if you use |
I had been hoping to use |
It would be great if it would be possible to skip writing of individual fields conditionally, similar to what
br(if(...))
does for reading.API could look like this:
And for brevity:
The text was updated successfully, but these errors were encountered: