6

I'd like to serialize my struct to binary and de-serialize it on the other end of the pipe. Is there a way to achieve this with the serialize crate? It seems to only support JSON, hex and base64.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
SBSTP
  • 3,479
  • 6
  • 30
  • 41
  • 1
    Can you elucidate on what you mean by “binary”? – Chris Morgan Jan 21 '15 at 04:14
  • A collection of u8, vector or array. I'm looking for something like Java's ObjectOutput/InputStream, essentially. Give it an object and it gives you bytes. – SBSTP Jan 21 '15 at 04:18
  • 3
    You might want to take a look at [bincode](https://github.com/TyOverby/bincode) – Renato Zannon Jan 21 '15 at 05:01
  • 1
    @SBSTP: so, you don’t care what precise format it’s in? – Chris Morgan Jan 21 '15 at 05:54
  • @ChrisMorgan not really. What I want to do is send messages to another process via its `stdin`. I'd like to send serialized enums/variants through the pipe, and deserialize them on the receiving end. Basically, IPC using UNIX pipes. – SBSTP Jan 21 '15 at 21:32
  • … so something like JSON, while potentially suboptimal, is not untenable. – Chris Morgan Jan 22 '15 at 05:44

1 Answers1

2

I would suggest bincode.

It provides encode() and decode() functions which operate on anything with RustcEncodable & RustcDecodable traits, which can generally be #[derive]d, and return Vec<u8>.

It has a few quirks (isize and usize become i64 and u64, for example), but they are mostly there to improve portability and it tends to work as you would expect.

Leonora Tindall
  • 1,391
  • 2
  • 12
  • 30