Serialize a struct/enum to bytes [duplicate]

蹲街弑〆低调 提交于 2019-12-22 08:17:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!