Best crate/method to interact with byte-defined ad-hoc encodings

I have to write some code that is going to be using an ad-hoc defined encodings a lot. Like: "File starts with a "file header" that is 2 bytes of magic number, 1 bytes of some id, 4 bytes of allocated size". "TcpStream has a one byte of command, then depending on the command 2 bytes of this, or 8 bytes of that". The rawest, crudest of raw encoding, most stuff fixed sized or size-prefixed. I started writing it by hand, but pretty quickly realized that this is not very idiomatic way.

Any recommendations for approaches to take here? Any crate that could do it?

The binread crate provides a really nice custom derive for binary formats. The author also put out a great blog post explaining how it works.

Otherwise, nom works pretty well for building parsers.


Yay! That looks perfect!

PSA: There was binread, then binwrite and they are being merged into binrw :smiley:


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.