32 releases

0.7.44 Feb 4, 2024
0.7.43 Dec 17, 2023
0.7.42 May 13, 2023
0.7.41 Mar 31, 2023
0.2.0 Nov 19, 2020

#994 in Encoding

Download history 693/week @ 2024-07-20 658/week @ 2024-07-27 620/week @ 2024-08-03 193/week @ 2024-08-10 466/week @ 2024-08-17 419/week @ 2024-08-24 386/week @ 2024-08-31 461/week @ 2024-09-07 451/week @ 2024-09-14 461/week @ 2024-09-21 328/week @ 2024-09-28 277/week @ 2024-10-05 588/week @ 2024-10-12 908/week @ 2024-10-19 544/week @ 2024-10-26 1101/week @ 2024-11-02

3,214 downloads per month
Used in 22 crates (via farmfe_core)

MIT license

1MB
19K SLoC

Trait object serialization for rkyv.

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::{
    archived_value,
    ser::{
        serializers::AllocSerializer,
        Serializer,
    },
    Archive,
    Archived,
    Deserialize,
    Infallible,
    Serialize,
};
use rkyv_dyn::archive_dyn;
use rkyv_typename::TypeName;

#[archive_dyn(deserialize)]
trait ExampleTrait {
    fn value(&self) -> String;
}

#[derive(Archive, Serialize, Deserialize)]
#[archive_attr(derive(TypeName))]
struct StringStruct(String);

#[archive_dyn(deserialize)]
impl ExampleTrait for StringStruct {
    fn value(&self) -> String {
        self.0.clone()
    }
}

impl ExampleTrait for Archived<StringStruct> {
    fn value(&self) -> String {
        self.0.as_str().to_string()
    }
}

#[derive(Archive, Serialize, Deserialize)]
#[archive_attr(derive(TypeName))]
struct IntStruct(i32);

#[archive_dyn(deserialize)]
impl ExampleTrait for IntStruct {
    fn value(&self) -> String {
        format!("{}", self.0)
    }
}

impl ExampleTrait for Archived<IntStruct> {
    fn value(&self) -> String {
        format!("{}", self.0)
    }
}

#[test]
fn main() {
    let boxed_int = Box::new(IntStruct(42)) as Box<dyn SerializeExampleTrait>;
    let boxed_string = Box::new(StringStruct("hello world".to_string())) as Box<dyn SerializeExampleTrait>;
    let mut serializer = AllocSerializer::<256>::default();

    let int_pos = serializer.serialize_value(&boxed_int).unwrap();
    let string_pos = serializer.serialize_value(&boxed_string).unwrap();
    let buf = serializer.into_serializer().into_inner();

    let archived_int = unsafe { archived_value::<Box<dyn SerializeExampleTrait>>(buf.as_ref(), int_pos) };
    let archived_string = unsafe { archived_value::<Box<dyn SerializeExampleTrait>>(buf.as_ref(), string_pos) };
    assert_eq!(archived_int.value(), "42");
    assert_eq!(archived_string.value(), "hello world");

    let deserialized_int: Box<dyn SerializeExampleTrait> = archived_int.deserialize(&mut Infallible).unwrap();
    let deserialized_string: Box<dyn SerializeExampleTrait> = archived_string.deserialize(&mut Infallible).unwrap();
    assert_eq!(deserialized_int.value(), "42");
    assert_eq!(deserialized_string.value(), "hello world");
}

Dependencies

~2.5MB
~50K SLoC