rkyv is a zero-copy deserialization framework for rust.
It is similar to other zero-copy deserialization frameworks such as Cap’n Proto and FlatBuffers. However, the former has an external schema and strictly restricted data types, while rkyv allows all serializable types to be defined in code, and can serialize various types that cannot be serialized by other types. Furthermore, rkyv is designed to have almost no overhead, and will in most cases perform exactly the same operations as native types.
and Serde Likewise, rkyv uses Rust’s powerful trait system to serialize data without reflection. Despite the wide range of features, you also only pay for what you use.If your data is checked out, the serialization process can be done likememcpy
as simple as that. Like serde, this allows rkyv to execute at a speed similar to hand-written serializers.
Unlike serde, the data generated by rkyv is guaranteed not to be deserialized.If you’re writing data to disk, you simply copy the filemmap
Write to memory, cast a pointer, and your data is ready to use. This makes it ideal for high-performance and IO-intensive applications.
pass Pin API supports limited data mutation, if you need complete mutation function,Archived values can actually be deserialized via Deserialize.
rkyv has a hashmap implementation built for zero-copy deserialization, so you can serialize your hashmaps however you like. The implementation performs perfect hashing using compression, hashing, and permutation algorithms to use as little memory as possible while still performing fast lookups.
It also comes with a B+ tree implementation that achieves optimal performance by splitting data into easily pageable 4KB segments. This makes it ideal for building immutable databases and structures for bulk data.
rkyv also supports contextual serialization, deserialization and validation.It correctly serializes and deserializes shared pointers likeRc
andArc
and can be extended to support custom context types.
Finally, rkyv makes it possible to serialize trait objects and use them as trait objects without deserialization.see more detailsarchive_dyn
crate.
While rkyv is a great format for final data, it lacks a full schema system and doesn’t do data migration and schema upgrades well. If your use case requires these features, additional libraries may be required to build these features on top of rkyv. Other serialization frameworks of the same type as rkyv, such as serde, can be used without conflict.
example:
use rkyv::{Archive, Deserialize, Serialize}; // bytecheck can be used to validate your data if you want use bytecheck::CheckBytes; #[derive(Archive, Deserialize, Serialize, Debug, PartialEq)] // This will generate a PartialEq impl between our unarchived and archived types #[archive(compare(PartialEq))] // To use the safe API, you have to derive CheckBytes for the archived type #[archive_attr(derive(CheckBytes, 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::<_, 256>(&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);
#rkyv #Homepage #Documentation #Downloads #Zerocopy #deserialization #framework #Rust #News Fast Delivery