Trait rkyv::Archive[][src]

pub trait Archive {
    type Archived;
    type Resolver;
    fn resolve(
        &self,
        pos: usize,
        resolver: Self::Resolver,
        out: &mut MaybeUninit<Self::Archived>
    ); }
Expand description

A type that can be used without deserializing.

Archiving is done depth-first, writing any data owned by a type before writing the data for the type itself. The type must be able to create the archived type from only its own data and its resolver.

Examples

Most of the time, #[derive(Archive)] will create an acceptable implementation. You can use the #[archive(...)] attribute to control how the implementation is generated. See the Archive derive macro for more details.

use rkyv::{
    archived_root,
    de::deserializers::AllocDeserializer,
    ser::{Serializer, serializers::AlignedSerializer},
    AlignedVec,
    Archive,
    Archived,
    Deserialize,
    Serialize,
};

#[derive(Archive, Serialize, Deserialize, Debug, PartialEq)]
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]),
};

let mut serializer = AlignedSerializer::new(AlignedVec::new());
serializer.serialize_value(&value).expect("failed to archive test");
let buf = serializer.into_inner();

let archived = unsafe { archived_root::<Test>(buf.as_slice()) };
assert_eq!(archived.int, value.int);
assert_eq!(archived.string, value.string);
assert_eq!(archived.option, value.option);

let deserialized = archived.deserialize(&mut AllocDeserializer).unwrap();
assert_eq!(value, deserialized);

Many of the core and standard library types already have Archive implementations available, but you may need to implement Archive for your own types in some cases the derive macro cannot handle.

In this example, we add our own wrapper that serializes a &'static str as if it’s owned. Normally you can lean on the archived version of String to do most of the work, but this example does everything to demonstrate how to implement Archive for your own types.

use core::{mem::MaybeUninit, slice, str};
use rkyv::{
    archived_root,
    offset_of,
    project_struct,
    ser::{Serializer, serializers::AlignedSerializer},
    AlignedVec,
    Archive,
    Archived,
    ArchiveUnsized,
    MetadataResolver,
    RelPtr,
    Serialize,
    SerializeUnsized,
};

struct OwnedStr {
    inner: &'static str,
}

struct ArchivedOwnedStr {
    // This will be a relative pointer to our string
    ptr: RelPtr<str>,
}

impl ArchivedOwnedStr {
    // This will help us get the bytes of our type as a str again.
    fn as_str(&self) -> &str {
        unsafe {
            // The as_ptr() function of RelPtr will get a pointer the str
            &*self.ptr.as_ptr()
        }
    }
}

struct OwnedStrResolver {
    // This will be the position that the bytes of our string are stored at.
    // We'll use this to resolve the relative pointer of our
    // ArchivedOwnedStr.
    pos: usize,
    // The archived metadata for our str may also need a resolver.
    metadata_resolver: MetadataResolver<str>,
}

// The Archive implementation defines the archived version of our type and
// determines how to turn the resolver into the archived form. The Serialize
// implementations determine how to make a resolver from the original value.
impl Archive for OwnedStr {
    type Archived = ArchivedOwnedStr;
    // This is the resolver we can create our Archived verison from.
    type Resolver = OwnedStrResolver;

    // The resolve function consumes the resolver and produces the archived
    // value at the given position.
    fn resolve(
        &self,
        pos: usize,
        resolver: Self::Resolver,
        out: &mut MaybeUninit<Self::Archived>
    ) {
        // We have to be careful to add the offset of the ptr field,
        // otherwise we'll be using the position of the ArchivedOwnedStr
        // instead of the position of the relative pointer.
        unsafe {
            self.inner.resolve_unsized(
                pos + offset_of!(Self::Archived, ptr),
                resolver.pos,
                resolver.metadata_resolver,
                project_struct!(out: Self::Archived => ptr),
            );
        }
    }
}

// We restrict our serializer types with Serializer because we need its
// capabilities to archive our type. For other types, we might need more or
// less restrictive bounds on the type of S.
impl<S: Serializer + ?Sized> Serialize<S> for OwnedStr {
    fn serialize(
        &self,
        serializer: &mut S
    ) -> Result<Self::Resolver, S::Error> {
        // This is where we want to write the bytes of our string and return
        // a resolver that knows where those bytes were written.
        // We also need to serialize the metadata for our str.
        Ok(OwnedStrResolver {
            pos: self.inner.serialize_unsized(serializer)?,
            metadata_resolver: self.inner.serialize_metadata(serializer)?
        })
    }
}

let mut serializer = AlignedSerializer::new(AlignedVec::new());
const STR_VAL: &'static str = "I'm in an OwnedStr!";
let value = OwnedStr { inner: STR_VAL };
// It works!
serializer.serialize_value(&value).expect("failed to archive test");
let buf = serializer.into_inner();
let archived = unsafe { archived_root::<OwnedStr>(buf.as_ref()) };
// Let's make sure our data got written correctly
assert_eq!(archived.as_str(), STR_VAL);

Associated Types

The archived version of this type.

The resolver for this type. It must contain all the information needed to make the archived type from the normal type.

Required methods

Creates the archived version of this value at the given position and writes it to the given output.

The output should be initialized field-by-field rather than by writing a whole struct. This is because performing a typed copy will set all of the padding bytes to uninitialized, but they must remain whatever value they currently have. This is so that uninitialized memory doesn’t get leaked to the final archive.

Implementations on Foreign Types

Implementors