Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary serialisation of Rust data strucutures [closed]

What is the current state of serialisation-to-binary in Rust?

I have some large (1-10MB) data structure to be sent across a network, and don't want to encode them as JSON or hex (the two serialisers I have found).

I have found #[repr(packed)]. Is this what I should use, or is there something more portable?

like image 825
fadedbee Avatar asked Apr 07 '15 13:04

fadedbee


1 Answers

#[repr(packed)] only makes your data small. It does not offer any format guarantees or serialization help.

You have a few choices here (ordered by my opinion from best to worst solution):

  1. You can use the Cap'n proto implementation for Rust
    • https://github.com/dwrensha/capnproto-rust
    • It's not really serialization, more of a forced format for structs that are then sent over the network without any conversion
    • fast
  2. You could write your own Serializer and Deserializer.
    • you have full control over the format
    • runtime overhead for every single datum
    • you need to implement lots of stuff
  3. You can transmute your structs to a [u8] and send that
    • probably the fastest solution
    • you need to make sure that the compiler for the program on both sides is exactly the same, otherwise the formats don't match up.
    • Someone evil may send you bad data. When you transmute that back, you get buffer overflows and stuff
    • references in your data-structure will cause wild pointers and undefined behaviour
      • Don't use references
like image 198
oli_obk Avatar answered Nov 15 '22 09:11

oli_obk