|
|
|
|
|
by geofft
1979 days ago
|
|
I had a use case recently for serializing C data structures in Rust (i.e., being compatible with an existing protocol defined as "compile this C header, and send the structs down a UNIX socket"), and I was a little surprised that the straightforward way to do it is to unsafely cast a #[repr(C)] structure to a byte-slice, and there isn't a Serde serializer for C layouts. (Which would even let you serialize C layouts for a different platform!) I think you could also do something Serde-ish that handles the original use case where you can derive something on a structure as long as it contains only plain data types (no pointers) or nested such structures. Then it would be safe to "serialize" and "deserialize" the structure by just translating it into memory (via either mmap or direct reads/writes), without going through a copy step. The other complication here is multiple readers - you might want your accessor functions to be atomic operations, and you might want to figure out some way for multiple processes accessing the same file to coordinate ordering updates. I kind of wonder what Rust's capnproto and Arrow bindings do, now.... |
|
[1] - https://github.com/rust-lang/project-safe-transmute
[2] - https://github.com/jswrenn/project-safe-transmute/blob/rfc/r...
[3] - https://docs.rs/bytemuck/1.5.0/bytemuck/
[4] - https://docs.rs/zerocopy/0.3.0/zerocopy/index.html