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

Add implementations for usize and isize #209

Closed caitp closed 1 year ago

csnover commented 1 year ago

Hi there, and thanks for your contribution!

These types deliberately do not have any built-in implementations in binrw because their size varies depending on the target architecture, so using them directly is not portable. One of the goals of binrw is to avoid footguns like this where it is possible to accidentally use the library in a non-portable way.

If your goal is to read/write data into a field that is usize or isize, use e.g. #[br(try_map = u64::try_into)] to convert from the correct underlying fixed-size type.

If you are reading from a local socket or shared memory where the incoming data is itself target-machine-dependent, use parse_with and write_with and a helper function, or a newtype wrapper. A patch to add helper functions for reading/writing native usize/isize instead seems like it would be a fine addition to the library, since this is a use case that has come up in the past.

Hopefully this makes sense. Let me know if you have any questions or other feedback. Thank you again!

caitp commented 1 year ago

Fair enough -- I figured this was the reason, the only reason I wanted this was to get some tests passing in bitflags, without changing anything