Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize a struct/enum to bytes [duplicate]

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.

like image 290
SBSTP Avatar asked Jan 21 '15 04:01

SBSTP


1 Answers

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.

like image 71
Leonora Tindall Avatar answered Nov 18 '22 05:11

Leonora Tindall