git.delta.rocks / unique-network / refs/commits / 03866db9b34b

difftreelog

doc(struct-versioning): explain semantics by example

Yaroslav Bolyukin2022-08-16parent: #ec76b9c.patch.diff
in: master

2 files changed

addedcrates/struct-versioning/README.mddiffbeforeafterboth

no changes

modifiedcrates/struct-versioning/src/lib.rsdiffbeforeafterboth
14// You should have received a copy of the GNU General Public License14// 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/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
1616
17//! struct-versioning17#![doc = include_str!("../README.md")]
18//! The crate contains procedural macros for versioning data structures.18
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//! When version become 3 `#[struct_versioning::versioned(version = 3, upper)]` this field will be removed.
33//! *upper* - generate From impls, which converts old version of structs to new
34//! In this case, the upgrade is described in `on_runtime_upgrade` using the `translate_values` substrate feature
35//!
36//! #[pallet::hooks]
37//! impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
38//! fn on_runtime_upgrade() -> Weight {
39//! if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {
40//! <TokenData<T>>::translate_values::<ItemDataVersion1, _>(|v| {
41//! Some(<ItemDataVersion2>::from(v))
42//! })
43//! }
44//! 0
45//! }
46//! }
47//!
48//! Another functionality
49///
50/// `#[versioned(version = 1[, first_version = 1][, upper][, versions])]`
51/// - *first_version* - allows to skip generation of structs, which predates first supported version
52/// - *versions* - generate enum, which contains all possible versions of struct
53use proc_macro::TokenStream;19use proc_macro::TokenStream;
54use quote::format_ident;20use quote::format_ident;
55use syn::{21use syn::{