Skip to content

Commit

Permalink
Add doc entry about key usage in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Sep 10, 2021
1 parent ec8219a commit 6566aff
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/storage-plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,32 @@ fn demo() -> StdResult<()> {
}
```

### Key types

A `Map` key can be anything that implements the `PrimaryKey` trait. There are a series of implementations of
`PrimaryKey` already provided (see `packages/storage-plus/src/keys.rs`):

- `impl<'a> PrimaryKey<'a> for &'a [u8]`
- `impl<'a> PrimaryKey<'a> for &'a str`
- `impl<'a> PrimaryKey<'a> for Vec<u8>`
- `impl<'a> PrimaryKey<'a> for String`
- `impl<'a> PrimaryKey<'a> for Addr`
- `impl<'a> PrimaryKey<'a> for &'a Addr`
- `impl<'a, T: PrimaryKey<'a> + Prefixer<'a>, U: PrimaryKey<'a>> PrimaryKey<'a> for (T, U)`
- `impl<'a, T: PrimaryKey<'a> + Prefixer<'a>, U: PrimaryKey<'a> + Prefixer<'a>, V: PrimaryKey<'a>> PrimaryKey<'a> for (T, U, V)`
- `impl<'a, T: Endian + Clone> PrimaryKey<'a> for IntKey<T>`

That means that byte and string slices, byte vectors, and strings, can be conveniently used as keys.
Moreover, some other types can be used as well, like addresses and addresses references, tuples and triples, and
integer types.

We suggest using `Addr` for keys in storage, instead of `String` or string slices. This implies doing address validation
through `addr_validate` on any address passed in via a message, to ensure it's a legitimate address, and not random text
which will fail later.

Thus, `pub fn addr_validate(&self, &str) -> Addr` in `deps.api` can be used for address validation, and the returned
`Addr` can be conveniently used as key in a `Map` or a similar structure.

### Composite Keys

There are times when we want to use multiple items as a key, for example, when
Expand Down

0 comments on commit 6566aff

Please sign in to comment.