1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
// Copyright 2018-2022 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Custom derive for `ink_storage` traits.
//!
//! This crate provides helpers to define your very own custom storage data
//! structures that work along the `ink_storage` data structures by implementing
//! `SpreadLayout` and `PackedLayout` traits.
//!
//! See [Spread vs. Packed](https://ink.substrate.io/datastructures/spread-packed-layout)
//! for more details of these two root strategies.
//!
//! # Examples
//!
//! ```no_run
//! use ink_storage::traits::{SpreadLayout, push_spread_root};
//! use ink_primitives::Key;
//!
//! # ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
//! // Enum
//! #[derive(SpreadLayout)]
//! enum Vote {
//! Yes,
//! No
//! }
//!
//! // Strucut
//! #[derive(SpreadLayout)]
//! struct NamedFields {
//! a: u32,
//! b: [u32; 32],
//! };
//!
//! // Created a custom structure and told which key to update.
//! push_spread_root(&NamedFields{ a: 123, b: [22; 32] }, &mut Key::from([0x42; 32]));
//! # Ok(())
//! # });
//! ```
extern crate proc_macro;
mod packed_layout;
mod spread_allocate;
mod spread_layout;
mod storage_layout;
#[cfg(test)]
mod tests;
use self::{
packed_layout::packed_layout_derive,
spread_allocate::spread_allocate_derive,
spread_layout::spread_layout_derive,
storage_layout::storage_layout_derive,
};
synstructure::decl_derive!(
[SpreadLayout] =>
/// Derives `ink_storage`'s `SpreadLayout` trait for the given `struct` or `enum`.
///
/// # Examples
///
/// ```
/// use ink_primitives::Key;
/// use ink_storage::traits::{
/// SpreadLayout,
/// push_spread_root,
/// pull_spread_root,
///};
///
/// # ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
/// #[derive(SpreadLayout)]
/// struct NamedFields {
/// a: u32,
/// b: [u32; 32],
/// }
///
/// let value = NamedFields {
/// a: 123,
/// b: [22; 32],
/// };
///
/// push_spread_root(&value, &mut Key::from([0x42; 32]));
/// let value2: NamedFields = pull_spread_root(&mut Key::from([0x42; 32]));
/// assert_eq!(value.a, value2.a);
/// # Ok(())
/// # });
/// ```
spread_layout_derive
);
synstructure::decl_derive!(
[PackedLayout] =>
/// Derives `ink_storage`'s `PackedLayout` trait for the given `struct` or `enum`.
///
/// # Examples
///
/// ```
/// use scale::{Encode, Decode};
/// use ink_primitives::Key;
/// use ink_storage::traits::{
/// SpreadLayout,
/// PackedLayout,
/// push_packed_root,
/// pull_packed_root
/// };
///
/// # ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
/// #[derive(Encode, Decode, SpreadLayout, PackedLayout)]
/// struct NamedFields {
/// a: u32,
/// b: [u32; 32],
/// }
///
/// let mut value = NamedFields {
/// a: 123,
/// b: [22; 32],
/// };
///
/// push_packed_root(&value, &mut Key::from([0x42; 32]));
/// let value2: NamedFields = pull_packed_root(&mut Key::from([0x42; 32]));
/// assert_eq!(value.a, value2.a);
/// # Ok(())
/// # });
/// ```
packed_layout_derive
);
synstructure::decl_derive!(
[SpreadAllocate] =>
/// Derives `ink_storage`'s `SpreadAllocate` trait for the given `struct`.
///
/// # Note
///
/// As of now `enum` types are not supported!
///
/// # Examples
///
/// ```
/// use ink_primitives::Key;
/// use ink_storage::traits::{
/// SpreadAllocate,
/// SpreadLayout,
/// allocate_spread_root,
///};
///
/// #[derive(SpreadAllocate, SpreadLayout)]
/// # #[derive(Debug, PartialEq)]
/// struct NamedFields {
/// a: u32,
/// b: [u32; 32],
/// }
///
/// let allocated: NamedFields = allocate_spread_root(&Key::from([0x42; 32]));
/// assert_eq!(
/// allocated,
/// NamedFields {
/// a: 0,
/// b: [0; 32],
/// }
/// );
/// ```
spread_allocate_derive
);
synstructure::decl_derive!(
[StorageLayout] =>
/// Derives `ink_storage`'s `StorageLayout` trait for the given `struct` or `enum`.
///
/// # Examples
///
/// ```
/// use ink_metadata::layout::Layout::Struct;
/// use ink_primitives::Key;
/// use ink_storage::traits::{
/// SpreadLayout,
/// StorageLayout,
/// push_spread_root,
/// KeyPtr,
/// };
///
/// # ink_env::test::run_test::<ink_env::DefaultEnvironment, _>(|_| {
/// #[derive(SpreadLayout, StorageLayout)]
/// struct NamedFields {
/// a: u32,
/// b: [u32; 32],
/// }
///
/// let mut key = Key::from([0x42; 32]);
/// let mut value = NamedFields {
/// a: 123,
/// b: [22; 32],
/// };
///
/// push_spread_root(&value, &key);
///
/// if let Struct(layout) = <NamedFields as StorageLayout>::layout(&mut KeyPtr::from(key)) {
/// assert_eq!(*layout.fields()[0].name().unwrap(), "a");
/// assert_eq!(*layout.fields()[1].name().unwrap(), "b");
/// }
/// # Ok(())
/// # });
/// ```
storage_layout_derive
);