Skip to content

Commit 24fa9c2

Browse files
committed
storey: update key-impl guide
1 parent 1c2b3d7 commit 24fa9c2

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/pages/storey/containers/map/key-impl.mdx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ enum Denom {
2424

2525
We 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;
2829
use storey::containers::map::{key::DynamicKey, Key};
2930

3031
enum 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

5758
The [`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;
6062
use storey::containers::map::{key::DynamicKey, Key};
6163

6264
enum 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.
8991
One little improvement we can go for is to avoid hardcoding the discriminant. We'll want to reuse
9092
these 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;
9396
use storey::containers::map::{key::DynamicKey, Key};
9497

9598
enum 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

128131
Let'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;
131135
use storey::containers::map::{key::DynamicKey, Key, OwnedKey};
132136

133137
enum 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

192196
Now 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};
197200
use storey::containers::IterableAccessor;
198201
use 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 {
243246
const MAP_IX: u8 = 1;
244247

245248
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);
248250

249251
access.entry_mut(&Denom::Native("USDT".into())).set(&1000).unwrap();
250252
access.entry_mut(&Denom::CW20("some_addr_3824792".into())).set(&2000).unwrap();

0 commit comments

Comments
 (0)