12 个不稳定版本 (3 个破坏性更新)
0.6.2 | 2022年2月3日 |
---|---|
0.6.1 | 2022年1月15日 |
0.5.5 | 2022年1月8日 |
0.5.2 | 2021年11月2日 |
0.3.0 | 2021年10月19日 |
#117 in #binary-encoding
用于 binverse
21KB
352 代码行
BinVerSe (Binary VerSerializer)
提供带有版本控制的快速二进制序列化,以向后兼容和紧凑的方式存储数据。
目前,该库仍在开发中,不建议在大型项目中使用,因为可能会有破坏性更改和问题出现。 警告:直到版本 1.0,没有任何东西保证向后兼容!
功能
- 简单、快速的二进制序列化
- 使用修订号进行版本控制
- 错误处理
- 过程宏以避免样板代码
- 使用宏进行版本控制/大小属性
基本示例
use binverse::{serialize::Serialize, streams::{Deserializer, Serializer}};
use binverse_derive::serializable;
fn main() {
// Add #[serializable] for automatic Serialize/Deserialize
// implementations and version handling.
#[serializable]
#[derive(Debug, PartialEq)]
struct Example {
a: i32,
b: f32,
c: String
}
let example = Example {
a: -1253891,
b: 44223.125,
c: String::from("Hello binverse!")
};
// Create a serializer that writes into a Vec<u8>, could be replaced by
// a file/network stream etc.
let mut serializer = Serializer::new(Vec::new(), 0).unwrap();
// Serialize the example struct into the serializer.
example.serialize(&mut serializer).unwrap();
// Get back the Vec<u8>.
let data = serializer.finish();
// The length of the data is pretty predictable:
assert_eq!(data.len(),
4 // bytes for revision so the data can be read in future versions
+ 4 // a: i32
+ 4 // b: f32
+ 1 // length of the following string
// (saved using VarInt, can be changed to a constant byte size)
// the bytes of the string:
+ "Hello binverse!".len()
);
// Create a deserializer to recreate the Example instance using the data.
let mut deserializer = Deserializer::new(data.as_slice()).unwrap();
// Deserialize the struct.
let example_deserialized: Example = deserializer.deserialize().unwrap();
// Both versions match
assert_eq!(example, example_deserialized);
}
依赖项
~1.5MB
~35K SLoC