Add optional binrw/BinRead implementation #370
No reviewers
Labels
No labels
bug
good first issue
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-bitflags/bitflags!370
Loading…
Reference in a new issue
No description provided.
Delete branch "binrw_binread"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The goal here is ultimately to teach serde to deserialize raw bitfields from a binary file format, being mindful of the expected endian-ness of the structs. This doesn't get it there from the start, afaik -- but it does seem like a usable starting point.
I don't write a ton of Rust code, so I won't be surprised if this isn't the best way to get this done. Please let me know if there's a better way to do this.
Hi @caitp 👋
Thanks for working on this!
Since
binrwis unstable, we unfortunately can't add direct support for it inbitflags, but you can still definebitflagstypes that you can apply its attributes to. We have an example using thezerocopylibrary here. I think the principle should be the same forbinrw.This is an unfortunate degradation of ergonomics compared to 1.x release, where the
derive(BinRead)worked without theimplmode@DCNick3 Yes it is a difference with
1.x, but trade-offs have to be made. The1.xbehavior of simply forwarding to the underlying integer type is only useful for cases like this where you're treating them as transparent.Is it possible to have the textual
DebugandDisplayimplementations when using theimplmode?You can yes, you just have to implement traits for formatting and parsing based on the functions in the
bitflags::parsermodule. We've got an example that shows it.It's been a while here so I'll go ahead and close this one to keep our PR list current. I'd be happy to take this with unstable support based on the approach we have for
zerocopyin the future though.Thanks again for the PR!
Until the
Debugimpl can be derived without boilerplate (tracked in #395), I'll have to stick to bitflags 1.x.Need this feature
For my use-case I've been doing this:
It's a mouthful, but that's that's (probably) the best you can do with the 2.0 design. You can probably wrap it into a decl macro if you need to repeat it a bunch.
Unlike the (naive) 1.0 derive, it will error out on incorrect flags, which is a bonus I guess.
Btw, @KodrAus, how do you feel about a
binrwintegration feature that would be explicitly exempt from semver guarantees (to reflectbinrw's unstable nature)? I think it would still be better than nothing forbinrwusers, even with potential breakages@DCNick3 I'm not really keen to add semver-exempt features directly to
bitflags. I am keen though to write a proc-macro library likebitflags_derive, that could include this sort of thing in it to reduce boilerplate of deriving traits from other libraries in a flags-aware way.Pull request closed