Serialize
Types implement Serialize
separately from
Archive
. Serialize
creates a resolver for some object, then Archive
turns the value and that
resolver into an archived type. Having a separate Serialize
trait is necessary because although a
type may have only one archived representation, you may have options of what requirements to meet in
order to create one.
The
Serialize
trait is parameterized over the serializer. The serializer is just a mutable object that helps the type serialize itself. The most basic types likeu32
orchar
don't bound their serializer type because they can serialize themselves with any kind of serializer. More complex types likeBox
andString
require a serializer that implementsSerializer
, and even more complex types likeRc
andVec
require a serializer that additionally implementSharedSerializeRegistry
orScratchSpace
.
Unlike Serialize
, Archive
doesn't parameterize over the serializer used to make it. It shouldn't
matter what serializer a resolver was made with, only that it's made correctly.
Serializer
rkyv provides serializers that provide all the functionality needed to serialize standard library types, as well as serializers that combine other serializers into a single object with all of the components' capabilities.
The provided serializers offer a wide
range of strategies and capabilities, but most use cases will be best suited by
AllocSerializer
.
Many types require scratch space to serialize. This is some extra allocated space that they can use temporarily and return when they're done. For example,
Vec
might request scratch space to store the resolvers for its elements until it can serialize all of them. Requesting scratch space from the serializer allows scratch space to be reused many times, which reduces the number of slow memory allocations performed while serializing.