Skip to content

Commit eb05dd4

Browse files
committed
Update packed storage examples
1 parent 0dc2379 commit eb05dd4

File tree

3 files changed

+16
-30
lines changed

3 files changed

+16
-30
lines changed

complex-storage-structures/README.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,18 @@ struct MyStruct {
8080
}
8181
```
8282

83-
For [packed structs](https://use.ink/datastructures/storage-layout#packed-vs-non-packed-layout), a new trait was introduced - `Packed`. It represents structs,
84-
all fields of which occupy a single storage cell. Any type that implements
85-
`scale::Encode` and `scale::Decode` receives a `Packed` implementation:
83+
For [packed structs][Packed], a new trait was introduced - `Packed`.
84+
It represents structs, all fields of which occupy a single storage cell.
85+
Any type that implements `scale::Encode` and `scale::Decode` receives a `Packed` implementation:
8686

87-
Unlike non-packed types created with `#[ink::storage_item]`, packed types don't have
88-
their own storage keys.
87+
Unlike non-packed types created with `#[ink::storage_item]`,
88+
packed types don't have their own storage keys,
89+
and they're created with `#[ink::storage_item(packed)]`.
90+
91+
[Packed]: https://use.ink/datastructures/storage-layout#packed-vs-non-packed-layout
8992

9093
```rust
91-
#[derive(scale::Encode, scale::Decode)]
92-
#[cfg_attr(
93-
feature = "std",
94-
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
95-
)]
94+
#[ink::storage_item(packed)]
9695
struct MyPackedStruct {
9796
first_field: u32,
9897
second_field: Vec<u8>,
@@ -108,11 +107,7 @@ struct NonPacked {
108107
s2: Lazy<u128>,
109108
}
110109

111-
#[derive(scale::Decode, scale::Encode)]
112-
#[cfg_attr(
113-
feature = "std",
114-
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
115-
)]
110+
#[ink::storage_item(packed)]
116111
struct Packed {
117112
s1: u128,
118113
s2: Vec<u128>,

complex-storage-structures/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ pub mod complex_structures {
3131
allowances: Mapping<(AccountId, AccountId), Balance, AutoKey>,
3232
}
3333

34-
#[derive(ink::scale::Encode, ink::scale::Decode, Default, Debug)]
35-
#[cfg_attr(
36-
feature = "std",
37-
derive(ink::scale_info::TypeInfo, ink::storage::traits::StorageLayout)
38-
)]
34+
#[derive(Default, Debug)]
35+
#[ink::storage_item(packed)]
3936
pub struct Balances {
4037
pub balance_state: u128,
4138
}

multisig/lib.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,8 @@ mod multisig {
110110
/// A Transaction is what every `owner` can submit for confirmation by other owners.
111111
/// If enough owners agree it will be executed by the contract.
112112
#[derive(Clone)]
113-
#[cfg_attr(
114-
feature = "std",
115-
derive(Debug, PartialEq, Eq, ink::storage::traits::StorageLayout)
116-
)]
117-
#[ink::scale_derive(Encode, Decode, TypeInfo)]
113+
#[cfg_attr(feature = "std", derive(Debug, PartialEq, Eq))]
114+
#[ink::storage_item(packed)]
118115
pub struct Transaction {
119116
/// The address of the contract that is called in this transaction.
120117
pub callee: Address,
@@ -144,11 +141,8 @@ mod multisig {
144141
/// This is a book keeping struct that stores a list of all transaction ids and
145142
/// also the next id to use. We need it for cleaning up the storage.
146143
#[derive(Clone, Default)]
147-
#[cfg_attr(
148-
feature = "std",
149-
derive(Debug, PartialEq, Eq, ink::storage::traits::StorageLayout)
150-
)]
151-
#[ink::scale_derive(Encode, Decode, TypeInfo)]
144+
#[cfg_attr(feature = "std", derive(Debug, PartialEq, Eq))]
145+
#[ink::storage_item(packed)]
152146
pub struct Transactions {
153147
/// Just store all transaction ids packed.
154148
transactions: Vec<TransactionId>,

0 commit comments

Comments
 (0)