Crate rkyv

source ·
Expand description

rkyv is a zero-copy deserialization framework for Rust.

§Overview

rkyv uses Rust’s powerful trait system to serialize data without reflection. Many zero-copy deserialization frameworks use external schemas and heavily restrict the available data types. By contrast, rkyv allows all serialized types to be defined in code and can serialize a wide variety of types that other frameworks cannot.

rkyv scales to highly-capable as well as highly-restricted environments. Not only does rkyv support “no-std” builds for targets without a standard library implementation, it also supports “no-alloc” builds for targets where allocations cannot be made.

rkyv supports limited in-place data mutation, and so can access and update data without ever deserializing back to native types. When rkyv’s in-place mutation is too limited, rkyv also provides ergonomic and performant deserialization back into native types.

rkyv prioritizes performance, and is one of the fastest serialization frameworks available. All of rkyv’s features can be individually enabled and disabled, so you only pay for what you use. Additionally, all of rkyv’s zero-copy types are designed to have little to no overhead. In most cases, rkyv’s types will have exactly the same performance as native types.

See the rkyv book for guide-level documentation and usage examples.

§Components

rkyv has a hash map implementation that is built for zero-copy deserialization, with the same lookup and iteration performance as the standard library hash maps. The hash map implementation is based on Swiss Tables and uses a target-independent version of FxHash to ensure that all targets compute the same hashes.

It also has a B-tree implementation that has the same performance characteristics as the standard library B-tree maps. Its compact representation and localized data storage is best-suited for very large amounts of data.

rkyv supports shared pointers by default, and is able to serialize and deserialize them without duplicating the underlying data. Shared pointers which point to the same data when serialized will still point to the same data when deserialized. By default, rkyv only supports non-cyclic data structures.

Alongside its unchecked API, rkyv also provides optional validation so you can ensure safety and data integrity at the cost of some overhead. Because checking serialized data can generally be done without allocations, the cost of checking and zero-copy access can be much lower than that of traditional deserialization.

rkyv is trait-oriented from top to bottom, and is made to be extended with custom and specialized types. Serialization, deserialization, and validation traits all accept generic context types, making it easy to add new capabilities without degrading ergonomics.

§Features

rkyv has several feature flags which can be used to modify its behavior. By default, rkyv enables the std, alloc, and bytecheck features.

§Format control

These features control how rkyv formats its serialized data. Enabling and disabling these features may change rkyv’s serialized format, and as such can cause previously-serialized data to become unreadable. Enabling format control features that are not the default should be considered a breaking change to rkyv’s serialized format.

Binaries should consider explicitly choosing format control options from the start, even though doing so is not required. This ensures that developers stay informed about the specific choices being made, and prevents any unexpected compatibility issues with libraries they depend on.

Libraries should avoid enabling format control features unless they intend to only support rkyv when those specific format control features are enabled. In general, libraries should be able to support all format control options if they use rkyv’s exported types and aliases.

§Endianness

If an endianness feature is not enabled, rkyv will use little-endian byte ordering by default.

  • little_endian: Forces data serialization to use little-endian byte ordering. This optimizes serialized data for little-endian architectures.
  • big_endian: Forces data serialization to use big-endian byte ordering. This optimizes serialized data for big-endian architectures.
§Alignment

If an alignment feature is not enabled, rkyv will use aligned primitives by default.

  • aligned: Forces data serialization to use aligned primitives. This adds alignment requirements for accessing data and prevents rkyv from working with unaligned data.
  • unaligned: Forces data serialization to use unaligned primitives. This removes alignment requirements for accessing data and allows rkyv to work with unaligned data more easily.
§Pointer width

If a pointer width feature is not enabled, rkyv will serialize isize and usize as 32-bit integers by default.

  • pointer_width_16: Serializes isize and usize as 16-bit integers. This is intended to be used only for small data sizes and may not handle large amounts of data.
  • pointer_width_32: Serializes isize and usize as 32-bit integers. This is a good choice for most data, and balances the storage overhead with support for large data sizes.
  • pointer_width_64: Serializes isize and usize as 64-bit integers. This is intended to be used only for extremely large data sizes and may cause unnecessary data bloat for smaller amounts of data.

§Functionality

These features enable more built-in functionality and provide more powerful and ergonomic APIs. Enabling and disabling these features does not change rkyv’s serialized format.

  • alloc: Enables support for the alloc crate. Enabled by default.
  • std: Enables standard library support. Enabled by default.
  • bytecheck: Enables data validation through bytecheck. Enabled by default.

§Crates

rkyv provides integrations for some common crates by default. In the future, crates should depend on rkyv and provide their own integration. Enabling and disabling these features does not change rkyv’s serialized format.

§Compatibility

Serialized data can be accessed later as long as:

  • The underlying schema has not changed
  • The serialized format has not been changed by format control features
  • The data was serialized by a semver-compatible version of rkyv

Re-exports§

Modules§

  • APIs for producing and using archived data.
  • An archived version of Box.
  • Archived versions of standard library containers.
  • Deserialization traits, deserializers, and adapters.
  • Archived versions of FFI types.
  • Hashing support for archived hash maps and sets.
  • Archived versions of network types.
  • Manually niched type replacements.
  • Archived versions of ops types.
  • An archived version of Option.
  • An initialized, writeable location in memory.
  • Definitions of archived primitives and type aliases based on enabled features.
  • Archived versions of shared pointers.
  • Relative pointer implementations and options.
  • An archived version of Result.
  • Mutable references to values which may not be moved or de-initialized.
  • Serialization traits and adapters.
  • Archived versions of string types.
  • Archived versions of time types.
  • The core traits provided by rkyv.
  • Archived versions of tuple types.
  • Utilities for common operations.
  • validationbytecheck
    Validation implementations and helper types.
  • An archived version of Vec.
  • Wrapper type support and commonly used wrappers.

Structs§

  • A place to write a T paired with its position in the output buffer.

Traits§

Functions§

Type Aliases§

Derive Macros§

  • Derives Archive for the labeled type.
  • Derives Deserialize for the labeled type.
  • Derives Portable for the labeled type.
  • Derives Serialize for the labeled type.