Giter Site home page Giter Site logo

rkyv

rkyv (archive) is a zero-copy deserialization framework for Rust

Discord docs.rs crates.io MIT license

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

  • rkyv, the core library
  • rkyv_dyn, which adds trait object support to rkyv

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::{Archive, Deserialize, Serialize, rancor::Error};

#[derive(Archive, Deserialize, Serialize, Debug, PartialEq)]
#[archive(
    // This will generate a PartialEq impl between our unarchived and archived
    // types:
    compare(PartialEq),
    // bytecheck can be used to validate your data if you want. To use the safe
    // API, you have to derive CheckBytes for the archived type:
    check_bytes,
)]
// Derives can be passed through to the generated type:
#[archive_attr(derive(Debug))]
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]),
};

// Serializing is as easy as a single function call
let bytes = rkyv::to_bytes::<Error>(&value).unwrap();

// Or you can customize your serialization for better performance
// and compatibility with #![no_std] environments
use rkyv::ser::{Serializer, serializers::AllocSerializer};

let mut serializer = AllocSerializer::<0>::default();
serializer.serialize_value(&value).unwrap();
let bytes = serializer.into_serializer().into_inner();

// You can use the safe API for fast zero-copy deserialization
let archived = rkyv::check_archived_root::<Test>(&bytes[..]).unwrap();
assert_eq!(archived, &value);

// Or you can use the unsafe API for maximum performance
let archived = unsafe { rkyv::archived_root::<Test>(&bytes[..]) };
assert_eq!(archived, &value);

// And you can always deserialize back to the original type
let deserialized: Test = archived.deserialize(&mut rkyv::Infallible).unwrap();
assert_eq!(deserialized, value);

Note: the safe API requires the bytecheck feature (enabled by default) Read more about available features.

Thanks

Thanks to all the sponsors that keep development sustainable. Special thanks to the following sponsors for going above and beyond supporting rkyv:

Platinum Sponsors

Dusk Network

Dusk Network is the first privacy blockchain for financial applications. Our mission is to enable any size enterprise to collaborate at scale, meet compliance requirements, and ensure that transaction data remains confidential.

rkyv's Projects

ptr_meta icon ptr_meta

A radioactive stabilization of the ptr_meta RFC.

rancor icon rancor

Scalable and efficient error handling without using type composition.

rel icon rel

An object system for Rust based on relative pointers

rend icon rend

Endian-aware primitives for Rust

rkyv icon rkyv

Zero-copy deserialization framework for Rust

rkyv_contrib icon rkyv_contrib

Community-contributed utilities and extensions for rkyv

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.