枚举序列化为字节

枚举序列化为字节

本文介绍了将结构/枚举序列化为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的结构序列化为二进制文件并在管道的另一端对其进行反序列化.有没有办法用序列化板条箱来实现这一点?好像只支持JSON、hex和base64.

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.

推荐答案

我建议 bincode.

它提供了 encode()decode() 函数,它们可以对任何带有 RustcEncodable 的东西进行操作 &RustcDecodable traits,一般可以是#[derive]d,返回Vec.

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

它有一些怪癖(例如,isizeusize 变成了 i64u64),但是它们主要是为了提高便携性,而且通常会按您的预期工作.

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.

这篇关于将结构/枚举序列化为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:52