64 releases

0.7.39 Jun 26, 2022
0.7.37 Mar 30, 2022
0.7.28 Dec 19, 2021
0.7.24 Nov 29, 2021
0.2.0 Nov 19, 2020

#27 in Encoding

Download history 51858/week @ 2022-08-19 56212/week @ 2022-08-26 60955/week @ 2022-09-02 70014/week @ 2022-09-09 46384/week @ 2022-09-16 59492/week @ 2022-09-23 55147/week @ 2022-09-30 65600/week @ 2022-10-07 69887/week @ 2022-10-14 48620/week @ 2022-10-21 49466/week @ 2022-10-28 33649/week @ 2022-11-04 46561/week @ 2022-11-11 43100/week @ 2022-11-18 59956/week @ 2022-11-25 53489/week @ 2022-12-02

210,464 downloads per month
Used in 174 crates (85 directly)

MIT license

620KB
14K SLoC

rkyv (archive) is a zero-copy deserialization framework for Rust

Discord

Resources

Learning Materials

  • The rkyv book covers the motivation, architecture, and major features of rkyv
  • The rkyv discord is a great place to get help with specific issues and meet other people using rkyv

Documentation

Benchmarks

  • The rust serialization benchmark is a shootout style benchmark comparing many rust serialization solutions. It includes special benchmarks for zero-copy serialization solutions like rkyv.

Sister Crates

  • bytecheck, which rkyv uses for validation
  • ptr_meta, which rkyv uses for pointer manipulation
  • rend, which rkyv uses for endian-agnostic features

Example

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);

Note: the safe API requires the validation feature:

rkyv = { version = "0.7", features = ["validation"] }

Read more about available features.

Dependencies

~0.3–1MB
~23K SLoC