Reading binary data to structs

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#).

io::{Read, Write}

1 Like

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:

2 Likes

Thanks.
And terrible :frowning:
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:

2 Likes

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.