git.delta.rocks / unique-network / refs/commits / af9745c0f31d

difftreelog

Documentation for struct-versioning

Dev2022-07-12parent: #83d3baa.patch.diff
in: master

1 file changed

modifiedcrates/struct-versioning/src/lib.rsdiffbeforeafterboth
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
2// This file is part of Unique Network.
3
4// Unique Network is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Unique Network is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
16
17//! struct-versioning
18//! 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` function
32//! *upper* - generate From impls, which converts old version of structs to new
33//! In this case, the upgrade is described in `on_runtime_upgrade` using the `translate_values` substrate feature
34//!
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//! 0
44//! }
45//! }
46
1use 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 struct
159///205///
160/// Each field may have version attribute206/// Each field may have version attribute
161/// `#[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 appeared
163/// - *2* - version, in which this field was removed209/// - *2* - version, in which this field was removed
164/// (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)