difftreelog
Merge pull request #434 from UniqueNetwork/doc/struct-versioning
in: master
Doc/struct versioning
2 files changed
crates/struct-versioning/README.mddiffbeforeafterboth1# struct-versioning23The crate contains procedural macros for versioning data structures.4Macros [`versioned`] generate versioned variants of a struct.56Example:7```8# use struct_versioning::versioned;9#[versioned(version = 5, first_version = 2)]10struct Example {}1112// versioned macro will generate suffixed versions of example struct,13// starting from `Version{first_version or 1}` to `Version{version}` inclusive14let _ver2 = ExampleVersion2 {};15let _ver3 = ExampleVersion3 {};16let _ver4 = ExampleVersion4 {};17let _ver5 = ExampleVersion5 {};1819// last version will also be aliased with original struct name20let _orig: Example = ExampleVersion5 {};2122#[versioned(version = 2, upper)]23#[derive(PartialEq, Debug)]24struct Upper {25 #[version(..2)]26 removed: u32,27 #[version(2.., upper(10))]28 added: u32,2930 #[version(..2)]31 retyped: u32,32 #[version(2.., upper(retyped as u64))]33 retyped: u64,34}3536// #[version] attribute on field allows to specify, in which versions of structs this field should present37// versions here works as standard rust ranges, start is inclusive, end is exclusive38let _up1 = UpperVersion1 {removed: 1, retyped: 0};39let _up2 = UpperVersion2 {added: 1, retyped: 0};4041// and upper() allows to specify, which value should be assigned to this field in `From<OldVersion>` impl42assert_eq!(43 UpperVersion2::from(UpperVersion1 {removed: 0, retyped: 6}),44 UpperVersion2 {added: 10, retyped: 6},45);46```4748In this case, the upgrade is described in `on_runtime_upgrade` using the `translate_values` substrate feature4950```ignore51#[pallet::hooks]52impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {53 fn on_runtime_upgrade() -> Weight {54 if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {55 <TokenData<T>>::translate_values::<ItemDataVersion1, _>(|v| {56 Some(<ItemDataVersion2>::from(v))57 })58 }59 060 }61}62```crates/struct-versioning/src/lib.rsdiffbeforeafterboth--- a/crates/struct-versioning/src/lib.rs
+++ b/crates/struct-versioning/src/lib.rs
@@ -1,3 +1,21 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+#![doc = include_str!("../README.md")]
+
use proc_macro::TokenStream;
use quote::format_ident;
use syn::{