问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/28059483/serialize-a-struct-enum-to-bytes