rkyv (archive) is a zero-copy deserialization framework for Rust
```rust use rkyv::{Archive, Deserialize, Serialize}; // bytecheck can be used to validate your data if you want use bytecheck::CheckBytes;
// This will generate a PartialEq impl between our unarchived and archived types
// To use the safe API, you have to derive CheckBytes for the archived type
struct Test {
int: u8,
string: String,
option: Option
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.serializevalue(&value).unwrap(); let bytes = serializer.intoserializer().into_inner();
// You can use the safe API for fast zero-copy deserialization
let archived = rkyv::checkarchivedroot::
// Or you can use the unsafe API for maximum performance
let archived = unsafe { rkyv::archivedroot::
// And you can always deserialize back to the original type let deserialized: Test = archived.deserialize(&mut rkyv::Infallible).unwrap(); assert_eq!(deserialized, value); ```
Note: the safe API requires the validation
feature:
toml
rkyv = { version = "0.7", features = ["validation"] }
Read more about available features.