What is the way to read arbitrary data from a file into arbitrary (simple) structs?
Let say: buffer a complete file into 'something' and then read from there.
I don't think there is something like a 'stream' or 'memorystream' in Rust (like in C#).
You can read the entire content of a file as a byte array with std::fs::read
, or stream it with std::fs::File
.
Mapping that data into structures is more complicated. Rust doesn't let you (safely) just cast any bytes into a structure: doing that correctly requires learning a bunch of pretty deep magic.
You can manually do it with u32::from_le_bytes
, though it's pretty ugly to use with slices since you need to give it an array type of exactly the right size, which requires things like either u32::from_le_bytes([data[0], data[1], data[2], data[3]])
or u32::from_le_bytes(data[0..4].try_into().unwrap ())
.
Fortunately, there's plenty of libraries to make this part easier:
- DataView — Rust parser // Lib.rs let's you prove to the compiler that it can just cast between the bytes
- binary-layout — Rust data encoding library // Lib.rs is similar, with a different API and features
- binrw — Rust data encoding library // Lib.rs let's you annotate a type with instructions on how to parse
- nom — Rust parser // Lib.rs is a very popular general parsing library with a focus on generality, and composition, and is quite suited to binary formats, though it's a bit clumsy to use due to Rust limitations.
Thanks.
And terrible
There are many crates with more or less the same functionality and leading to quite unreadable code.
Anyway... I will try and find the least terrible.
Argh, I had apparently forgotten the most popular:
Aha, I saw this one coming by somewhere. I will have a look at it and checkout if it can handle structs. Thanks.
And much like binrw
, deku is also designed for this.
I find bytemuck quite convenient!
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.