Live data from Hacker News

Deserializing Binary Data Files in Rust

adventures.michaelfbryan.com

11–20 of 23 posts

Re: Deserializing Binary Data Files in Rust

#11
post #7

For such a problem, I really recommend https://kaitai.io/ From their website: > Kaitai Struct is a declarative language used to describe various binary data structures, laid out in files or in memory: i.e. binary file formats, network stream packet formats, etc. They even have a Rust interface: https://github.com/kaitai-io/kaitai_struct_rust_runtime

A native Rust solution, with meta-programming: https://crates.io/crates/packed_struct (written by me)

Re: Deserializing Binary Data Files in Rust

#12
post #9

I really wish there was a safe way to initialize structs with arbitrary binary data in rust. Perhaps I'm missing something, but it seems intuitively safe, especially if the struct only contains integer primitives.

How would it be possible to make it safe? The compiler can't verify that N random bytes input from an arbitrary source are going to be valid for the types in the struct - you have to tell tell the compiler "trust this even if you can't verify it from the source code" - hence the unsafe keyword.

Re: Deserializing Binary Data Files in Rust

#13
post #9

I really wish there was a safe way to initialize structs with arbitrary binary data in rust. Perhaps I'm missing something, but it seems intuitively safe, especially if the struct only contains integer primitives.

How would it be possible to make it safe? The compiler can't verify that N random bytes input from an arbitrary source are going to be valid for the types in the struct - you have to tell tell the compiler "trust this even if you can't verify it from the source code" - hence the unsafe keyword.

Enter safe transmute - https://github.com/rust-lang/project-safe-transmute/blob/mas...

Re: Deserializing Binary Data Files in Rust

#14
post #9

I really wish there was a safe way to initialize structs with arbitrary binary data in rust. Perhaps I'm missing something, but it seems intuitively safe, especially if the struct only contains integer primitives.

How would it be possible to make it safe? The compiler can't verify that N random bytes input from an arbitrary source are going to be valid for the types in the struct - you have to tell tell the compiler "trust this even if you can't verify it from the source code" - hence the unsafe keyword.

For integer primitives, all possible values are valid. Just allowing binary initialization for structs that consist of integer types would allow compatibility with C structure. As it is, I have to either resort to unsafe code or manually deserialize every struct

Re: Deserializing Binary Data Files in Rust

#15
post #5

Earlier quoted context omitted.

The rust structure is defined as repr(C) so there is no reordering.

Does the C spec actually specify it needs to be in that order? Been awhile since I messed that that sort of thing. I know in practice they typically just put them in the same order and align on some sort of default packing depending on CPU. Usually to minimize on extra instructions. I have eeked out some extra perf on some CPUs and structs just by moving stuff around and making sure it fits into a cacheline. Sometime…

Yes, it does

Re: Deserializing Binary Data Files in Rust

#16
post #3

I think c struct is missing alignment attribute? This idea has been explored in flatbuffers, capnproto and probably others.

C structs are generally assumed to be laid out in the order defined with proper padding automatically inserted (as most platforms at best dislike unaligned accesses). Alignment information should only be necessary if you want to pack the structure or need to apply wider than standard alignment (e.g. need to align a 32 or 64b type to 128 bytes). The one possible issue is that reading padding is UB (i think). But here…

Wider than standard alignment can be pretty important though. Especially for usage with SIMD instructions.

Re: Deserializing Binary Data Files in Rust

#19

Earlier quoted context omitted.

How would it be possible to make it safe? The compiler can't verify that N random bytes input from an arbitrary source are going to be valid for the types in the struct - you have to tell tell the compiler "trust this even if you can't verify it from the source code" - hence the unsafe keyword.

For integer primitives, all possible values are valid. Just allowing binary initialization for structs that consist of integer types would allow compatibility with C structure. As it is, I have to either resort to unsafe code or manually deserialize every struct

Oh I see, that makes sense. Also there's a sibling reply to yours for a cool looking proposal to do just that if you haven't seen it yet.

Re: Deserializing Binary Data Files in Rust

#20

Earlier quoted context omitted.

How would it be possible to make it safe? The compiler can't verify that N random bytes input from an arbitrary source are going to be valid for the types in the struct - you have to tell tell the compiler "trust this even if you can't verify it from the source code" - hence the unsafe keyword.

Enter safe transmute - https://github.com/rust-lang/project-safe-transmute/blob/mas...

Oh cool! Thanks for the pointer.
Post reply on HN