Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document Item #134

Merged
merged 6 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/pages/storey/containers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ container.

To avoid key collisions, you must provide a different byte to each container.

# Accessor
uint marked this conversation as resolved.
Show resolved Hide resolved

Every container has a corresponding "accessor" type that is used to perform all read/write
operations.

To get the accessor of a top-level container, the [`access` method] must be called. It takes a reference
to a storage backend.

```rust template="storage"
use cw_storey::containers::Item;
use cw_storey::CwStorage;

let item: Item<u32> = Item::new(0);

item.access(&mut CwStorage(&mut storage))
uint marked this conversation as resolved.
Show resolved Hide resolved
.set(&42)
.unwrap();

assert_eq!(
item.access(&CwStorage(&storage))
.get()
.unwrap(),
Some(42)
uint marked this conversation as resolved.
Show resolved Hide resolved
);
```

# Composition

Some containers can be composed together to create more complex data structures. Right now, the only
Expand Down Expand Up @@ -108,3 +134,6 @@ This is a binary encoding that should generally be a storage performance improve
If you need to use a different encoding, you can instead import the `Item`/`Column` type from the
`storey` crate and specify an alternative encoding. A guide to implementing your encoding can be
found in the [_Alternative encodings_](encodings) section.

[`access` method]:
https://docs.rs/cw-storey/latest/cw_storey/containers/type.Item.html#method.access
84 changes: 84 additions & 0 deletions src/pages/storey/containers/item.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,87 @@ tags: ["storey", "containers"]
import { Callout } from "nextra/components";

# Item

An [`Item`] is the simplest container. It stores a single value at a specific key.

An `Item` that's never been `set` is empty, and will return `None` upon a `get`.

<Callout>
For a more in-depth look at the `Item` container, check out the [API documentation].
</Callout>

## Usage examples

### Saving an admin address

```rust template="storage"
use cw_storey::containers::Item;
use cw_storey::CwStorage;

let admin: Item<String> = Item::new(0);
uint marked this conversation as resolved.
Show resolved Hide resolved
let mut cw_storage = CwStorage(&mut storage);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioned above already, but it would really be great if we could avoid that for every single storage access

let mut access = admin.access(&mut cw_storage);

assert_eq!(
admin.access(&cw_storage).get().unwrap(),
None,
);

admin.access(&mut cw_storage)
.set(&String::from("some_address"))
.unwrap();

assert_eq!(
admin.access(&cw_storage).get().unwrap(),
Some(String::from("some_address")),
);
```

### Maintaining a config structure

```rust template="storage"
use cw_storey::containers::Item;
use cw_storey::CwStorage;

#[cw_serde]
struct Config {
admin: String,
interest_rate: Decimal,
uint marked this conversation as resolved.
Show resolved Hide resolved
}

let item: Item<Config> = Item::new(0);
let mut cw_storage = CwStorage(&mut storage);
let mut access = item.access(&mut cw_storage);

let cfg = Config {
admin: "some_address".to_string(),
interest_rate: Decimal::percent(5),
};

access.set(&cfg).unwrap();
assert_eq!(access.get().unwrap(), Some(cfg));
```

### Default values

Sometimes you might like to read a value, but if it may have never been set, you want to provide a
default. This is a common pattern for counters or other numeric values.

```rust template="storage"
use cw_storey::containers::Item;
use cw_storey::CwStorage;

let counter: Item<u32> = Item::new(0);
let mut cw_storage = CwStorage(&mut storage);
let mut access = counter.access(&mut cw_storage);

let mut total = access.get().unwrap().unwrap_or(0);
uint marked this conversation as resolved.
Show resolved Hide resolved

assert_eq!(total, 0);
total += 1;

access.set(&total).unwrap();
```

[`Item`]: https://docs.rs/cw-storey/latest/cw_storey/containers/type.Item.html
[API documentation]: https://docs.rs/cw-storey/latest/cw_storey/containers/type.Item.html