Skip to content

Commit

Permalink
[autofix.ci] apply automated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
autofix-ci[bot] authored Nov 21, 2024
1 parent 91e8630 commit 53d0dda
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/pages/storey/containers/map.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { Callout } from "nextra/components";

A `Map` is a container that maps keys to instances of another container.

Most commonly, you will use a `Map` to store a list of items. For example, the signature could be a `Map<String, Item<String>>` to store a list of signatures for each user. (See the first example.)
Most commonly, you will use a `Map` to store a list of items. For example, the signature could be a
`Map<String, Item<String>>` to store a list of signatures for each user. (See the first example.)

## Examples

### Keeping balances

Let's say you want to keep track of the balances of each user. You can do this with a `Map<String, Item<Uint128>>`.
Let's say you want to keep track of the balances of each user. You can do this with a
`Map<String, Item<Uint128>>`.

```rust template="storage" showLineNumbers {6,8,10,12,14}
use cw_storey::containers::{Item, Map};
Expand All @@ -33,17 +35,20 @@ access.entry_mut("alice").set(&Uint128::new(1000)).unwrap();
assert_eq!(access.entry("alice").get().unwrap(), Some(Uint128::new(1000)));
```

- _line 6:_ Here we construct the `Map` facade. The constructor takes a key, which is the prefix of the keys in the underlying storage backend.
- _line 6:_ Here we construct the `Map` facade. The constructor takes a key, which is the prefix of
the keys in the underlying storage backend.
- _line 8:_ The `access` method returns a `MapAccess` entity, which allows manipulating the map.
- _line 10:_ Here we try to access the balance of `alice`. Since she doesn't have one yet, it returns `None`.
The `entry` method returns an `ItemAccess` entity, which allows manipulating the item stored under the key `alice`.
- _line 10:_ Here we try to access the balance of `alice`. Since she doesn't have one yet, it
returns `None`. The `entry` method returns an `ItemAccess` entity, which allows manipulating the
item stored under the key `alice`.
- _line 12:_ Here we set Alice's balance to `1000`.
- _line 14:_ We check that the balance is now `1000`.

#### Iterating over the balances

Iterating over the balances is pretty straightforward. The `keys` method returns an iterator over the keys,
the `values` method returns an iterator over the values, and the `pairs` method returns an iterator over both.
Iterating over the balances is pretty straightforward. The `keys` method returns an iterator over
the keys, the `values` method returns an iterator over the values, and the `pairs` method returns an
iterator over both.

```rust template="storage" showLineNumbers {4, 17, 19}
use cw_storey::containers::{Item, Map};
Expand Down Expand Up @@ -83,20 +88,20 @@ assert_eq!(

- _line 4:_ Here we import the `IterableAccessor` trait. This trait provides unbounded iteration.
- _line 17:_ The `pairs` method returns an iterator over the key-value pairs.
- _line 19:_ Notice the key type is `(String, ())`. This is likely to become just `String` in the future. For now, consider this a quirk of the design. This will make more sense once you get to composite maps.


- _line 19:_ Notice the key type is `(String, ())`. This is likely to become just `String` in the
future. For now, consider this a quirk of the design. This will make more sense once you get to
composite maps.

<Callout type="warning">
When iterating over the entries in a `Map` using these methods, the order of the keys is not guaranteed to be sensible.
Currently if you need a sensible order, try using the bounded iterators instead. If one does not exist, sensibly ordered iteration is not possible.
When iterating over the entries in a `Map` using these methods, the order of the keys is not
guaranteed to be sensible. Currently if you need a sensible order, try using the bounded iterators
instead. If one does not exist, sensibly ordered iteration is not possible.
</Callout>


### Keeping balances with composition

Alright, let's say this time you'd also like to keep track of the balances of each user,
but each can have multiple different tokens. This is where composition comes in.
Alright, let's say this time you'd also like to keep track of the balances of each user, but each
can have multiple different tokens. This is where composition comes in.

```rust template="storage" showLineNumbers {6, 10-14}
use cw_storey::containers::{Item, Map};
Expand All @@ -117,9 +122,10 @@ assert_eq!(access.entry("alice").entry("OSMO").get().unwrap(), Some(Uint128::new

This example is similar to the previous one, but this time we have an extra level of nesting.

First of all, our type is `Map<String, Map<String, Item<Uint128>>>`.
The outer map maps user addresses to an inner map. The inner map maps token denominations to actual balances.
First of all, our type is `Map<String, Map<String, Item<Uint128>>>`. The outer map maps user
addresses to an inner map. The inner map maps token denominations to actual balances.

When we access the stuff, the first `entry`/`entry_mut` call accesses a record in the outer map, and the second one accesses a record in the inner map.
When we access the stuff, the first `entry`/`entry_mut` call accesses a record in the outer map, and
the second one accesses a record in the inner map.

#### Iterating over the balances

0 comments on commit 53d0dda

Please sign in to comment.