71 releases

0.8.0-alpha.2 Mar 22, 2024
0.7.44 Feb 4, 2024
0.7.43 Dec 17, 2023
0.7.42 May 13, 2023
0.2.0 Nov 19, 2020

#25 in Encoding

Download history 133767/week @ 2023-12-23 188737/week @ 2023-12-30 237095/week @ 2024-01-06 251453/week @ 2024-01-13 267073/week @ 2024-01-20 291774/week @ 2024-01-27 294797/week @ 2024-02-03 283294/week @ 2024-02-10 261307/week @ 2024-02-17 285153/week @ 2024-02-24 305169/week @ 2024-03-02 319432/week @ 2024-03-09 334871/week @ 2024-03-16 327100/week @ 2024-03-23 325763/week @ 2024-03-30 269790/week @ 2024-04-06

1,313,656 downloads per month
Used in 516 crates (196 directly)

MIT license

665KB
16K 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

  • rkyv, the core library
  • rkyv_dyn, which adds trait object support to rkyv

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

#[derive(Archive, Deserialize, Serialize, Debug, PartialEq)]
#[archive(
    // This will generate a PartialEq impl between our unarchived and archived
    // types:
    compare(PartialEq),
    // bytecheck can be used to validate your data if you want. To use the safe
    // API, you have to derive CheckBytes for the archived type:
    check_bytes,
)]
// Derives can be passed through to the generated type:
#[archive_attr(derive(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 bytecheck feature (enabled by default) Read more about available features.

Dependencies

~0.6–1.9MB
~41K SLoC