11 releases (5 breaking)

0.5.1 Apr 7, 2021
0.4.1 Mar 12, 2021
0.2.1 Dec 12, 2020
0.2.0 Nov 19, 2020

#387 in Encoding

Download history 1/week @ 2020-12-24 5/week @ 2020-12-31 2/week @ 2021-01-07 24/week @ 2021-01-14 6/week @ 2021-01-21 13/week @ 2021-01-28 7/week @ 2021-02-04 29/week @ 2021-02-11 30/week @ 2021-02-18 5/week @ 2021-02-25 3/week @ 2021-03-04 16/week @ 2021-03-11 1/week @ 2021-03-18 36/week @ 2021-03-25 35/week @ 2021-04-01 22/week @ 2021-04-08

65 downloads per month

MIT license

260KB
5.5K SLoC

Trait object serialization for rkyv.


API Documentation

Book

  • The rkyv book covers the motivation and architecture of rkyv

Sister Crates:

  • bytecheck, which rkyv uses for validation
  • ptr_meta, which rkyv uses for pointer manipulation

rkyv_dyn in action

use rkyv::{
    archived_value,
    de::deserializers::AllocDeserializer,
    ser::{
        serializers::AlignedSerializer,
        Serializer,
    },
    AlignedVec,
    Archive,
    Archived,
    Deserialize,
    Serialize,
};
use rkyv_dyn::archive_dyn;
use rkyv_typename::TypeName;

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

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

#[archive_dyn]
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(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)
    }
}

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 = AlignedSerializer::new(AlignedVec::new());

    let int_pos = serializer.serialize_value(&boxed_int)
        .expect("failed to archive boxed int");
    let string_pos = serializer.serialize_value(&boxed_string)
        .expect("failed to archive boxed string");
    let buf = 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 AllocDeserializer).unwrap();
    let deserialized_string: Box<dyn SerializeExampleTrait> = archived_string.deserialize(&mut AllocDeserializer).unwrap();
    assert_eq!(deserialized_int.value(), "42");
    assert_eq!(deserialized_string.value(), "hello world");
}

Dependencies

~0.4–0.9MB
~21K SLoC