Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ink_storage] Add missing docs for ink_storage_derive proc. macros #637 #711

Merged
merged 7 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/storage/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ proc-macro2 = "1.0"
synstructure = "0.12.4"

[dev-dependencies]
scale = { package = "parity-scale-codec", version = "2.0", default-features = false, features = ["derive", "full"] }
ink_env = { version = "3.0.0-rc2", path = "../../env" }
ink_primitives = { version = "3.0.0-rc2", path = "../../primitives" }
ink_metadata = { version = "3.0.0-rc2", path = "../../metadata" }
ink_storage = { version = "3.0.0-rc2", path = ".." }
149 changes: 144 additions & 5 deletions crates/storage/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,42 @@
// 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
hychen marked this conversation as resolved.
Show resolved Hide resolved
//! `SpreadLayout` and `PackedLayout` traits.
//!
//! See [Spread vs. Packed](https://paritytech.github.io/ink-docs/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;
Expand All @@ -22,10 +58,113 @@ mod storage_layout;
mod tests;

use self::{
packed_layout::packed_layout_derive,
spread_layout::spread_layout_derive,
packed_layout::packed_layout_derive, spread_layout::spread_layout_derive,
storage_layout::storage_layout_derive,
};
synstructure::decl_derive!([SpreadLayout] => spread_layout_derive);
synstructure::decl_derive!([PackedLayout] => packed_layout_derive);
synstructure::decl_derive!([StorageLayout] => storage_layout_derive);
synstructure::decl_derive!(
[SpreadLayout] =>
/// Derives `ink_storage`'s `SpreadLayout` trait for the given `struct` or `enum`.
///
/// # Examples
/// ```
hychen marked this conversation as resolved.
Show resolved Hide resolved
/// 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
/// ```
hychen marked this conversation as resolved.
Show resolved Hide resolved
/// 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!(
[StorageLayout] =>
/// Derives `ink_storage`'s `StorageLayout` trait for the given `struct` or `enum`.
///
/// # Examples
/// ```
hychen marked this conversation as resolved.
Show resolved Hide resolved
/// 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
);