Skip to content

Commit 95d5126

Browse files
[autofix.ci] apply automated fixes
1 parent a267db9 commit 95d5126

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Callout } from "nextra/components";
99
In this section, we will implement a custom key type for use with [maps].
1010

1111
Let's say we have a `Denom` enum to represent different kinds of tokens:
12+
1213
- native tokens
1314
- [CW20] tokens
1415

@@ -40,13 +41,17 @@ impl Key for Denom {
4041
}
4142
```
4243

43-
The [`Kind`] associated type is used to signal to the framework whether the key is dynamically sized or not. In this case, we use [`DynamicKey`] because the key size is not fixed.
44-
If it was always exactly 8 bytes, we could use [`FixedSizeKey<8>`] instead.
44+
The [`Kind`] associated type is used to signal to the framework whether the key is dynamically sized
45+
or not. In this case, we use [`DynamicKey`] because the key size is not fixed. If it was always
46+
exactly 8 bytes, we could use [`FixedSizeKey<8>`] instead.
4547

4648
<Callout>
4749
Why does this matter? The framework uses this information to determine how to encode the key. If there are more keys following this one (e.g. in a multi-level map), the framework needs to know how to tell where this one ends during decoding.
4850

49-
For a dynamically sized key, the framework will length-prefix it when necessary. For a fixed-size key, it will use the static information you provide with `FixedSizeKey<L>` to figure out how many bytes to eat - a more performant solution.
51+
For a dynamically sized key, the framework will length-prefix it when necessary. For a fixed-size
52+
key, it will use the static information you provide with `FixedSizeKey<L>` to figure out how many
53+
bytes to eat - a more performant solution.
54+
5055
</Callout>
5156

5257
The [`encode`] method is used to serialize the key into a byte vector. Let's implement it now!
@@ -77,9 +82,12 @@ impl Key for Denom {
7782
}
7883
```
7984

80-
The code should be pretty self-explanatory. We use a simple encoding scheme where we write a single byte discriminant followed by the actual data. The discriminant is how we tell a native token from a CW20 token.
85+
The code should be pretty self-explanatory. We use a simple encoding scheme where we write a single
86+
byte discriminant followed by the actual data. The discriminant is how we tell a native token from a
87+
CW20 token.
8188

82-
One little improvement we can go for is to avoid hardcoding the discriminant. We'll want to reuse these values in the decoding logic, so let's define them as constants:
89+
One little improvement we can go for is to avoid hardcoding the discriminant. We'll want to reuse
90+
these values in the decoding logic, so let's define them as constants:
8391

8492
```rust template="storage" {8-11, 18-19}
8593
use storey::containers::map::{key::DynamicKey, Key};
@@ -112,7 +120,8 @@ impl Key for Denom {
112120
}
113121
```
114122

115-
Alright. The `Key` trait allows us to access the data in a map using our custom key type. We still need a way to decode the key back into the enum. This is used for example in iteration.
123+
Alright. The `Key` trait allows us to access the data in a map using our custom key type. We still
124+
need a way to decode the key back into the enum. This is used for example in iteration.
116125

117126
## The OwnedKey trait
118127

@@ -164,15 +173,18 @@ impl OwnedKey for Denom {
164173
}
165174
```
166175

167-
The [`from_bytes`] method should return an instance of the key type or an error if the data is invalid. Here it does the following:
176+
The [`from_bytes`] method should return an instance of the key type or an error if the data is
177+
invalid. Here it does the following:
178+
168179
- read the discriminant byte
169180
- read the data bytes as a UTF-8 string, erroring out if it's not valid
170181
- match the discriminant to the enum variant, or error if invalid
171182
- return the deserialized key
172183

173184
<Callout>
174-
What we have is functional. There's one last improvement you could make here - a proper error type. We used `()` as a placeholder, but in production code it's good practice to define an enum. In this case, the enum could hold variants
175-
like `InvalidDiscriminant` and `InvalidUTF8`.
185+
What we have is functional. There's one last improvement you could make here - a proper error
186+
type. We used `()` as a placeholder, but in production code it's good practice to define an enum.
187+
In this case, the enum could hold variants like `InvalidDiscriminant` and `InvalidUTF8`.
176188
</Callout>
177189

178190
## Using the thing
@@ -247,7 +259,9 @@ Voilà! It works just like it would with any other key.
247259
[CW20]: /docs/getting-started/cw20
248260
[`Kind`]: https://docs.rs/storey/latest/storey/containers/map/key/trait.Key.html#associatedtype.Kind
249261
[`DynamicKey`]: https://docs.rs/storey/latest/storey/containers/map/key/struct.DynamicKey.html
250-
[`FixedSizeKey<8>`]: https://docs.rs/storey/latest/storey/containers/map/key/struct.FixedSizeKey.html
262+
[`FixedSizeKey<8>`]:
263+
https://docs.rs/storey/latest/storey/containers/map/key/struct.FixedSizeKey.html
251264
[`encode`]: https://docs.rs/storey/latest/storey/containers/map/key/trait.Key.html#tymethod.encode
252265
[`OwnedKey`]: https://docs.rs/storey/latest/storey/containers/map/key/trait.OwnedKey.html
253-
[`from_bytes`]: https://docs.rs/storey/latest/storey/containers/map/key/trait.OwnedKey.html#tymethod.from_bytes
266+
[`from_bytes`]:
267+
https://docs.rs/storey/latest/storey/containers/map/key/trait.OwnedKey.html#tymethod.from_bytes

0 commit comments

Comments
 (0)