7 releases

Uses old Rust 2015

0.2.0 Jan 2, 2024
0.1.10 Mar 24, 2023
0.1.9 Nov 25, 2022
0.1.6 Mar 12, 2021
0.1.1 May 22, 2020

#148 in Encoding

Download history 7589/week @ 2023-12-23 9547/week @ 2023-12-30 11013/week @ 2024-01-06 6842/week @ 2024-01-13 3778/week @ 2024-01-20 3163/week @ 2024-01-27 4650/week @ 2024-02-03 3116/week @ 2024-02-10 3723/week @ 2024-02-17 3928/week @ 2024-02-24 3852/week @ 2024-03-02 3956/week @ 2024-03-09 3762/week @ 2024-03-16 3121/week @ 2024-03-23 2568/week @ 2024-03-30 2474/week @ 2024-04-06

13,178 downloads per month
Used in 4 crates

Apache-2.0

50KB
1K SLoC

Versionize is a framework for version tolerant serializion/deserialization of Rust data structures, designed for usecases that need fast deserialization times and minimal size overhead. It does not aim to be a generic serialization framework and only the bincode backend is supported.

Important note

This crate is currently used for cross-version serialization with the Firecracker snapshot-restore dev preview, but has not been tested for other use cases. It should be considered experimental software outside the Firecracker context. It’s likely that this crate will see both interface and implementation changes in the future.

Versionize in action

extern crate versionize;
extern crate versionize_derive;

use versionize::{VersionMap, Versionize, VersionizeResult};
use versionize_derive::Versionize;

// The test structure is at version 3.
#[derive(Debug, PartialEq, Versionize)]
pub struct Test {
    a: u32,
    #[version(start = 2, end = 3)]
    b: u8,
    #[version(start = 3, default_fn = "default_c")]
    c: String,
}

impl Test {
    // Default value for field `c`.
    // The callback is invoked when deserializing an older version
    // where the field did not exist.
    fn default_c(_source_version: u16) -> String {
        "test_string".to_owned()
    }
}

// Memory to hold the serialization output.
let mut mem = vec![0u8; 512];
// Create a new version map - it will start at app version 1.
let mut version_map = VersionMap::new();
// Map structure versions to app version.
version_map
    .new_version() // App version 2.
    .set_type_version(Test::type_id(), 2) // Struct(2) -> App(2).
    .new_version() // App version 3.
    .set_type_version(Test::type_id(), 3); // Struct(3) -> App(3).

let test_struct = Test {
    a: 1337,
    b: 0xFF,
    c: "c_value".to_owned(),
};

// Serialize to app version 2 - field c will not be serialized.
test_struct
    .serialize(&mut mem.as_mut_slice(), &version_map, 2)
    .unwrap();

// Deserialize from app version 2 - c should contain the default_fn() return value.
let restored_test_struct = Test::deserialize(&mut mem.as_slice(), &version_map, 2).unwrap();

assert_eq!(
    restored_test_struct,
    Test {
        a: 1337,
        b: 255,
        c: "test_string".to_owned()
    }
);

Dependencies

~1–1.6MB
~35K SLoC