@@ -24,15 +24,16 @@ enum Denom {
24
24
25
25
We can implement the ` Key ` trait for this enum to make it usable as a key in a map:
26
26
27
- ``` rust template="storage" {1, 8-14}
27
+ ``` rust template="storage" {1-2, 9-15}
28
+ use cw_storey :: containers :: CwKeySet ;
28
29
use storey :: containers :: map :: {key :: DynamicKey , Key };
29
30
30
31
enum Denom {
31
32
Native (String ),
32
33
CW20 (String ),
33
34
}
34
35
35
- impl Key for Denom {
36
+ impl Key < CwKeySet > for Denom {
36
37
type Kind = DynamicKey ;
37
38
38
39
fn encode (& self ) -> Vec <u8 > {
@@ -56,15 +57,16 @@ bytes to eat - a more performant solution.
56
57
57
58
The [ ` encode ` ] method is used to serialize the key into a byte vector. Let's implement it now!
58
59
59
- ``` rust template="storage" {12-21}
60
+ ``` rust template="storage" {13-22}
61
+ use cw_storey :: containers :: CwKeySet ;
60
62
use storey :: containers :: map :: {key :: DynamicKey , Key };
61
63
62
64
enum Denom {
63
65
Native (String ),
64
66
CW20 (String ),
65
67
}
66
68
67
- impl Key for Denom {
69
+ impl Key < CwKeySet > for Denom {
68
70
type Kind = DynamicKey ;
69
71
70
72
fn encode (& self ) -> Vec <u8 > {
@@ -89,7 +91,8 @@ CW20 token.
89
91
One little improvement we can go for is to avoid hardcoding the discriminant. We'll want to reuse
90
92
these values in the decoding logic, so let's define them as constants:
91
93
92
- ``` rust template="storage" {8-11, 18-19}
94
+ ``` rust template="storage" {9-12, 19-20}
95
+ use cw_storey :: containers :: CwKeySet ;
93
96
use storey :: containers :: map :: {key :: DynamicKey , Key };
94
97
95
98
enum Denom {
@@ -102,7 +105,7 @@ impl Denom {
102
105
const CW20_DISCRIMINANT : u8 = 1 ;
103
106
}
104
107
105
- impl Key for Denom {
108
+ impl Key < CwKeySet > for Denom {
106
109
type Kind = DynamicKey ;
107
110
108
111
fn encode (& self ) -> Vec <u8 > {
@@ -127,7 +130,8 @@ need a way to decode the key back into the enum. This is used for example in ite
127
130
128
131
Let's now implement the [ ` OwnedKey ` ] trait.
129
132
130
- ``` rust template="storage" {30-43}
133
+ ``` rust template="storage" {31-44}
134
+ use cw_storey :: containers :: CwKeySet ;
131
135
use storey :: containers :: map :: {key :: DynamicKey , Key , OwnedKey };
132
136
133
137
enum Denom {
@@ -140,7 +144,7 @@ impl Denom {
140
144
const CW20_DISCRIMINANT : u8 = 1 ;
141
145
}
142
146
143
- impl Key for Denom {
147
+ impl Key < CwKeySet > for Denom {
144
148
type Kind = DynamicKey ;
145
149
146
150
fn encode (& self ) -> Vec <u8 > {
@@ -157,7 +161,7 @@ impl Key for Denom {
157
161
}
158
162
}
159
163
160
- impl OwnedKey for Denom {
164
+ impl OwnedKey < CwKeySet > for Denom {
161
165
type Error = ();
162
166
163
167
fn from_bytes (bytes : & [u8 ]) -> Result <Self , Self :: Error > {
@@ -191,9 +195,8 @@ invalid. Here it does the following:
191
195
192
196
Now that we have our key type implemented, we can use it in a map:
193
197
194
- ``` rust template="storage" {1-3, 49-64}
195
- use cw_storey :: CwStorage ;
196
- use cw_storey :: containers :: {Item , Map };
198
+ ``` rust template="storage" showLineNumbers {1-3, 48-57}
199
+ use cw_storey :: containers :: {Item , Map , CwKeySet };
197
200
use storey :: containers :: IterableAccessor ;
198
201
use storey :: containers :: map :: {key :: DynamicKey , Key , OwnedKey };
199
202
@@ -208,7 +211,7 @@ impl Denom {
208
211
const CW20_DISCRIMINANT : u8 = 1 ;
209
212
}
210
213
211
- impl Key for Denom {
214
+ impl Key < CwKeySet > for Denom {
212
215
type Kind = DynamicKey ;
213
216
214
217
fn encode (& self ) -> Vec <u8 > {
@@ -225,7 +228,7 @@ impl Key for Denom {
225
228
}
226
229
}
227
230
228
- impl OwnedKey for Denom {
231
+ impl OwnedKey < CwKeySet > for Denom {
229
232
type Error = ();
230
233
231
234
fn from_bytes (bytes : & [u8 ]) -> Result <Self , Self :: Error > {
@@ -243,8 +246,7 @@ impl OwnedKey for Denom {
243
246
const MAP_IX : u8 = 1 ;
244
247
245
248
let map : Map <Denom , Item <u64 >> = Map :: new (MAP_IX );
246
- let mut cw_storage = CwStorage (& mut storage );
247
- let mut access = map . access (& mut cw_storage );
249
+ let mut access = map . access (& mut storage );
248
250
249
251
access . entry_mut (& Denom :: Native (" USDT" . into ())). set (& 1000 ). unwrap ();
250
252
access . entry_mut (& Denom :: CW20 (" some_addr_3824792" . into ())). set (& 2000 ). unwrap ();
0 commit comments