#serialization #version

macro versionize_derive

Implements the Versionize derive proc macro

4 releases

Uses old Rust 2015

0.1.6 Aug 24, 2023
0.1.5 Mar 24, 2023
0.1.4 Jan 13, 2021
0.1.3 Sep 17, 2020
0.1.1 May 20, 2020

#147 in Procedural macros

Download history 10489/week @ 2023-10-26 16297/week @ 2023-11-02 19731/week @ 2023-11-09 11128/week @ 2023-11-16 22152/week @ 2023-11-23 11345/week @ 2023-11-30 15405/week @ 2023-12-07 17121/week @ 2023-12-14 8744/week @ 2023-12-21 10045/week @ 2023-12-28 7512/week @ 2024-01-04 7406/week @ 2024-01-11 2908/week @ 2024-01-18 1999/week @ 2024-01-25 1671/week @ 2024-02-01 2018/week @ 2024-02-08

8,977 downloads per month
Used in 5 crates

Apache-2.0

37KB
574 lines

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.

You may be looking for:

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 deserialization from and 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 version 1.
    let mut version_map = VersionMap::new();
    // Add new version and mark changes for Test struct: Set the current version
    // to point to Test struct version 2.
    version_map
        .new_version()
        .set_type_version(Test::type_id(), 2)
        .new_version()
        .set_type_version(Test::type_id(), 3);

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

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

    // Deserialize from 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.5MB
~32K SLoC