3 个版本
0.7.39 |
|
---|---|
0.7.38 |
|
0.7.38-test.2 | 2022 年 9 月 7 日 |
#1856 in 编码
每月 242 次下载
605KB
14K SLoC
rkyv (归档) 是一个为 Rust 提供的零拷贝反序列化框架
资源
学习资料
- rkyv 书籍涵盖了 rkyv 的动机、架构和主要功能
- rkyv discord 是一个获取特定问题帮助和结识其他使用 rkyv 的人的好地方
文档
- rkyv,核心库
- rkyv_dyn,为 rkyv 添加了 trait object 支持
- rkyv_typename,一个类型命名库
基准测试
- rust 序列化基准测试是一种射击风格基准测试,比较了许多 rust 序列化解决方案。它包括针对 rkyv 等零拷贝序列化解决方案的特殊基准测试。
相关 crate
示例
use rkyv::{Archive, Deserialize, Serialize};
// bytecheck can be used to validate your data if you want
use bytecheck::CheckBytes;
#[derive(Archive, Deserialize, Serialize, Debug, PartialEq)]
// This will generate a PartialEq impl between our unarchived and archived types
#[archive(compare(PartialEq))]
// To use the safe API, you have to derive CheckBytes for the archived type
#[archive_attr(derive(CheckBytes, Debug))]
struct Test {
int: u8,
string: String,
option: Option<Vec<i32>>,
}
let value = Test {
int: 42,
string: "hello world".to_string(),
option: Some(vec![1, 2, 3, 4]),
};
// Serializing is as easy as a single function call
let bytes = rkyv::to_bytes::<_, 256>(&value).unwrap();
// Or you can customize your serialization for better performance
// and compatibility with #![no_std] environments
use rkyv::ser::{Serializer, serializers::AllocSerializer};
let mut serializer = AllocSerializer::<0>::default();
serializer.serialize_value(&value).unwrap();
let bytes = serializer.into_serializer().into_inner();
// You can use the safe API for fast zero-copy deserialization
let archived = rkyv::check_archived_root::<Test>(&bytes[..]).unwrap();
assert_eq!(archived, &value);
// Or you can use the unsafe API for maximum performance
let archived = unsafe { rkyv::archived_root::<Test>(&bytes[..]) };
assert_eq!(archived, &value);
// And you can always deserialize back to the original type
let deserialized: Test = archived.deserialize(&mut rkyv::Infallible).unwrap();
assert_eq!(deserialized, value);
注意:安全的 API 需要 validation
功能
rkyv = { version = "0.7", features = ["validation"] }
了解更多关于 可用功能。
依赖关系
~1.2–2MB
~47K SLoC