| Crates.io | rkyv |
| lib.rs | rkyv |
| version | 0.8.14 |
| created_at | 2020-11-11 16:36:21.012106+00 |
| updated_at | 2026-01-13 18:13:11.234905+00 |
| description | Zero-copy deserialization framework for Rust |
| homepage | |
| repository | https://github.com/rkyv/rkyv |
| max_upload_size | |
| id | 311298 |
| size | 915,624 |
rkyv (archive) is a zero-copy deserialization framework for Rust
use rkyv::{deserialize, rancor::Error, Archive, Deserialize, Serialize};
#[derive(Archive, Deserialize, Serialize, Debug, PartialEq)]
#[rkyv(
// This will generate a PartialEq impl between our unarchived
// and archived types
compare(PartialEq),
// Derives can be passed through to the generated type:
derive(Debug),
)]
struct Test {
int: u8,
string: String,
option: Option<Vec<i32>>,
}
fn main() {
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::<Error>(&value).unwrap();
// Or you can customize your serialization for better performance or control
// over resource usage
use rkyv::{api::high::to_bytes_with_alloc, ser::allocator::Arena};
let mut arena = Arena::new();
let bytes =
to_bytes_with_alloc::<_, Error>(&value, arena.acquire()).unwrap();
// You can use the safe API for fast zero-copy deserialization
let archived = rkyv::access::<ArchivedTest, Error>(&bytes[..]).unwrap();
assert_eq!(archived, &value);
// Or you can use the unsafe API for maximum performance
let archived =
unsafe { rkyv::access_unchecked::<ArchivedTest>(&bytes[..]) };
assert_eq!(archived, &value);
// And you can always deserialize back to the original type
let deserialized = deserialize::<Test, Error>(archived).unwrap();
assert_eq!(deserialized, value);
}
Note: the safe API requires the bytecheck feature (enabled by default)
Read more about available features.
Thanks to all the sponsors that keep development sustainable.