Replies: 4 comments 4 replies
-
https://docs.rs/binrw/latest/binrw/ |
Beta Was this translation helpful? Give feedback.
-
I've not done binary file parsing in Rust yet, but I've done some quite complex text parsing before (C++ entity names) with a crate/library that claims to support binary input too, and from my experience with text I would strongly recommend that crate. The crate is nom. It works by using a parser combinator architecture: you design your parser as a tree of greedy parsing functions, where each function is tasked with a simple goal: assert if the input matches a certain pattern, if so return parsed output and unparsed input, otherwise signal error to caller. The routine can easily defer to lower-level parsing subroutines to do its job, and where the input grammar is ambiguous, it is easy to have a branching design where multiple parsers are tried successively and the first that matches is kept. Anecdotically, it is also the most frequently downloaded general-purpose parsing crate, which shows that it's quite a popular choice and it shouldn't be too hard to find help on websites like users.rust-lang.org if you get stuck. You may also want to skim through the crates.io search linked above as although overly common keywords like "parsing" produce a fair amount of noise, it's still possible to find gems that way in a reasonable amount of time. Recent downloads is my favorite metric personally as its meaning is transparent (unlike "relevance") and it's a fairly good indicator of popularity in other projects, which itself is not bad to keep in mind for the reason given above. Another field to watch is last update date, which may help notice abandoned projects (no update for 1 year may mean a project is very mature, but often it just means there is no maintainer anymore). |
Beta Was this translation helpful? Give feedback.
-
For binary zerocopy encoding/decoding, you can definitely do it safely. I also suggest looking at the |
Beta Was this translation helpful? Give feedback.
-
The author is considering re-implementing a structured binary files codec library in Rust due to its potential benefits, such as parallel loading and memory safety. They are unsure whether it can be done without using unsafe sections and are seeking advice on patterns for mapping structures on memory and loading binary files. |
Beta Was this translation helpful? Give feedback.
-
@HadrienG2 @pwnorbitals I'm considering re-implementing this lib in RUST. This is basically a structured binary files codec.
I feel like RUST could helps on some aspects like loading several variables in parallel and fore sure ensure 0 memory hazards as long as I can write this in "safe mode". What is not clear to me is if such piece of code can be written in RUST without unsafe sections? Are there any patterns to map structures on memory or to load such binary files?
Beta Was this translation helpful? Give feedback.
All reactions