difftreelog
Documentation for struct-versioning
in: master
1 file changed
crates/struct-versioning/src/lib.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! struct-versioning18//! The crate contains procedural macros for versioning data structures.19//! Macros `versioned` generate versioned variants of a struct.20//!21//! Example:22//! #[struct_versioning::versioned(version = 2, upper)]23//! ...24//! pub struct ItemData {25//! pub const_data: BoundedVec<u8, CustomDataLimit>,26//!27//! #[version(..2)]28//! pub variable_data: BoundedVec<u8, CustomDataLimit>,29//! }30//! ...31//! `#[version(..2)]` that means that any version before 2 will be upgraded to 2 via the `upper` function32//! *upper* - generate From impls, which converts old version of structs to new33//! In this case, the upgrade is described in `on_runtime_upgrade` using the `translate_values` substrate feature34//!35//! #[pallet::hooks]36//! impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {37//! fn on_runtime_upgrade() -> Weight {38//! if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {39//! <TokenData<T>>::translate_values::<ItemDataVersion1, _>(|v| {40//! Some(<ItemDataVersion2>::from(v))41//! })42//! }43//! 044//! }45//! }461use proc_macro::TokenStream;47use proc_macro::TokenStream;2use quote::format_ident;48use quote::format_ident;158/// - *versions* - generate enum, which contains all possible versions of struct204/// - *versions* - generate enum, which contains all possible versions of struct159///205///160/// Each field may have version attribute206/// Each field may have version attribute161/// `#[version([1]..[2][, upper(old)])]`207/// `#[[1]..[2][, upper(old)])]`162/// - *1* - version, on which this field is appeared208/// - *1* - version, on which this field is appeared163/// - *2* - version, in which this field was removed209/// - *2* - version, in which this field was removed164/// (i.e if set to 2, this field was exist on version 1, and no longer exist on version 2)210/// (i.e if set to 2, this field was exist on version 1, and no longer exist on version 2)