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]),
};
let _bytes = rkyv::to_bytes::<Error>(&value).unwrap();
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();
let archived = rkyv::access::<ArchivedTest, Error>(&bytes[..]).unwrap();
assert_eq!(archived, &value);
let archived =
unsafe { rkyv::access_unchecked::<ArchivedTest>(&bytes[..]) };
assert_eq!(archived, &value);
let deserialized = deserialize::<Test, Error>(archived).unwrap();
assert_eq!(deserialized, value);
}