@@ -24,15 +24,16 @@ enum Denom {
2424
2525We can implement the ` Key ` trait for this enum to make it usable as a key in a map:
2626
27- ``` rust template="storage" {1, 8-14}
27+ ``` rust template="storage" {1-2, 9-15}
28+ use cw_storey :: containers :: CwKeySet ;
2829use storey :: containers :: map :: {key :: DynamicKey , Key };
2930
3031enum Denom {
3132 Native (String ),
3233 CW20 (String ),
3334}
3435
35- impl Key for Denom {
36+ impl Key < CwKeySet > for Denom {
3637 type Kind = DynamicKey ;
3738
3839 fn encode (& self ) -> Vec <u8 > {
@@ -56,15 +57,16 @@ bytes to eat - a more performant solution.
5657
5758The [ ` encode ` ] method is used to serialize the key into a byte vector. Let's implement it now!
5859
59- ``` rust template="storage" {12-21}
60+ ``` rust template="storage" {13-22}
61+ use cw_storey :: containers :: CwKeySet ;
6062use storey :: containers :: map :: {key :: DynamicKey , Key };
6163
6264enum Denom {
6365 Native (String ),
6466 CW20 (String ),
6567}
6668
67- impl Key for Denom {
69+ impl Key < CwKeySet > for Denom {
6870 type Kind = DynamicKey ;
6971
7072 fn encode (& self ) -> Vec <u8 > {
@@ -89,7 +91,8 @@ CW20 token.
8991One little improvement we can go for is to avoid hardcoding the discriminant. We'll want to reuse
9092these values in the decoding logic, so let's define them as constants:
9193
92- ``` rust template="storage" {8-11, 18-19}
94+ ``` rust template="storage" {9-12, 19-20}
95+ use cw_storey :: containers :: CwKeySet ;
9396use storey :: containers :: map :: {key :: DynamicKey , Key };
9497
9598enum Denom {
@@ -102,7 +105,7 @@ impl Denom {
102105 const CW20_DISCRIMINANT : u8 = 1 ;
103106}
104107
105- impl Key for Denom {
108+ impl Key < CwKeySet > for Denom {
106109 type Kind = DynamicKey ;
107110
108111 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
127130
128131Let's now implement the [ ` OwnedKey ` ] trait.
129132
130- ``` rust template="storage" {30-43}
133+ ``` rust template="storage" {31-44}
134+ use cw_storey :: containers :: CwKeySet ;
131135use storey :: containers :: map :: {key :: DynamicKey , Key , OwnedKey };
132136
133137enum Denom {
@@ -140,7 +144,7 @@ impl Denom {
140144 const CW20_DISCRIMINANT : u8 = 1 ;
141145}
142146
143- impl Key for Denom {
147+ impl Key < CwKeySet > for Denom {
144148 type Kind = DynamicKey ;
145149
146150 fn encode (& self ) -> Vec <u8 > {
@@ -157,7 +161,7 @@ impl Key for Denom {
157161 }
158162}
159163
160- impl OwnedKey for Denom {
164+ impl OwnedKey < CwKeySet > for Denom {
161165 type Error = ();
162166
163167 fn from_bytes (bytes : & [u8 ]) -> Result <Self , Self :: Error > {
@@ -191,9 +195,8 @@ invalid. Here it does the following:
191195
192196Now that we have our key type implemented, we can use it in a map:
193197
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 };
197200use storey :: containers :: IterableAccessor ;
198201use storey :: containers :: map :: {key :: DynamicKey , Key , OwnedKey };
199202
@@ -208,7 +211,7 @@ impl Denom {
208211 const CW20_DISCRIMINANT : u8 = 1 ;
209212}
210213
211- impl Key for Denom {
214+ impl Key < CwKeySet > for Denom {
212215 type Kind = DynamicKey ;
213216
214217 fn encode (& self ) -> Vec <u8 > {
@@ -225,7 +228,7 @@ impl Key for Denom {
225228 }
226229}
227230
228- impl OwnedKey for Denom {
231+ impl OwnedKey < CwKeySet > for Denom {
229232 type Error = ();
230233
231234 fn from_bytes (bytes : & [u8 ]) -> Result <Self , Self :: Error > {
@@ -243,8 +246,7 @@ impl OwnedKey for Denom {
243246const MAP_IX : u8 = 1 ;
244247
245248let 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 );
248250
249251access . entry_mut (& Denom :: Native (" USDT" . into ())). set (& 1000 ). unwrap ();
250252access . entry_mut (& Denom :: CW20 (" some_addr_3824792" . into ())). set (& 2000 ). unwrap ();
0 commit comments