-
Notifications
You must be signed in to change notification settings - Fork 15
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
storage-plus
: containers
#23
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2a8f676
storage-plus: `Item` docs
uint 8df2e51
storage-plus: `Map` docs
uint 625d896
storage-plus: `Deque` docs
uint 19a5efa
storage-plus: rm Iteration page, add note to `Map`
uint 748431d
update deps in CI
uint 04735dc
address some review comments
uint b367e1c
apply some Vercel suggestions
uint File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#[allow(unused_imports)] | ||
mod imports { | ||
pub use cosmwasm_std::*; | ||
pub use cosmwasm_schema::cw_serde; | ||
} | ||
|
||
#[allow(unused_imports)] | ||
use imports::*; | ||
|
||
#[test] | ||
fn doctest() { | ||
#[allow(unused_mut)] | ||
let mut storage = cosmwasm_std::testing::MockStorage::new(); | ||
{{code}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
set -o nounset -o pipefail | ||
|
||
inotifywait -e modify,move,create,delete --recursive --monitor --format "%e %w%f" src | while read changed; do echo $changed; scripts/test-rust.sh; done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
"basics": "Basics", | ||
"containers": "Containers", | ||
"iteration": "Iteration", | ||
"snapshots": "Snapshots", | ||
"multi-indexes": "Multi index collections" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,104 @@ | ||
import { Callout } from "nextra/components"; | ||
|
||
# `Deque` | ||
|
||
## Overview | ||
A `Deque` imitates a traditional double-ended queue. This collection is designed | ||
for efficient pushes and pops from either the beginning or end, but not for | ||
insertions/deletions from the middle. It can easily serve as a queue or stack. | ||
|
||
The main operations available here are [`push_back`], [`push_front`], | ||
[`pop_back`], and [`pop_front`]. It is also possible to check the [`len`]gth of | ||
the deque, [`get`] an element by index, and [`iter`]ate over the elements. | ||
|
||
[`push_back`]: | ||
https://docs.rs/cw-storage-plus/latest/cw_storage_plus/struct.Deque.html#method.push_back | ||
[`push_front`]: | ||
https://docs.rs/cw-storage-plus/latest/cw_storage_plus/struct.Deque.html#method.push_front | ||
[`pop_back`]: | ||
https://docs.rs/cw-storage-plus/latest/cw_storage_plus/struct.Deque.html#method.pop_back | ||
[`pop_front`]: | ||
https://docs.rs/cw-storage-plus/latest/cw_storage_plus/struct.Deque.html#method.pop_front | ||
[`len`]: | ||
https://docs.rs/cw-storage-plus/latest/cw_storage_plus/struct.Deque.html#method.len | ||
[`get`]: | ||
https://docs.rs/cw-storage-plus/latest/cw_storage_plus/struct.Deque.html#method.get | ||
[`iter`]: | ||
https://docs.rs/cw-storage-plus/latest/cw_storage_plus/struct.Deque.html#method.iter | ||
|
||
<Callout type="warning"> | ||
The maximum capacity of a `Deque` is `u32::MAX - 1` elements. Trying to push | ||
more elements is considered Undefined Behavior💀. | ||
</Callout> | ||
|
||
More information can be found in the | ||
[API docs](https://docs.rs/cw-storage-plus/latest/cw_storage_plus/struct.Deque.html). | ||
|
||
## Examples | ||
|
||
### ? | ||
### Pushing and popping | ||
|
||
```rust template="storage" | ||
use cw_storage_plus::Deque; | ||
|
||
let deque: Deque<u32> = Deque::new("d"); | ||
|
||
deque.push_back(&mut storage, &2).unwrap(); | ||
deque.push_back(&mut storage, &3).unwrap(); | ||
deque.push_front(&mut storage, &1).unwrap(); | ||
|
||
// at this point, we have [1, 2, 3] | ||
|
||
assert_eq!(deque.pop_back(&mut storage).unwrap(), Some(3)); | ||
assert_eq!(deque.pop_front(&mut storage).unwrap(), Some(1)); | ||
assert_eq!(deque.pop_back(&mut storage).unwrap(), Some(2)); | ||
assert_eq!(deque.pop_back(&mut storage).unwrap(), None); | ||
``` | ||
|
||
### Checking length | ||
|
||
```rust template="storage" | ||
use cw_storage_plus::Deque; | ||
|
||
let deque: Deque<u32> = Deque::new("d"); | ||
|
||
assert_eq!(deque.len(&storage).unwrap(), 0); | ||
|
||
deque.push_back(&mut storage, &1).unwrap(); | ||
deque.push_back(&mut storage, &2).unwrap(); | ||
|
||
assert_eq!(deque.len(&storage).unwrap(), 2); | ||
``` | ||
|
||
### Getting an element by index | ||
|
||
```rust template="storage" | ||
use cw_storage_plus::Deque; | ||
|
||
let deque: Deque<u32> = Deque::new("d"); | ||
|
||
deque.push_back(&mut storage, &1).unwrap(); | ||
deque.push_back(&mut storage, &2).unwrap(); | ||
|
||
assert_eq!(deque.get(&storage, 0).unwrap(), Some(1)); | ||
assert_eq!(deque.get(&storage, 1).unwrap(), Some(2)); | ||
assert_eq!(deque.get(&storage, 2).unwrap(), None); | ||
``` | ||
|
||
### Iterating over elements | ||
|
||
```rust template="storage" | ||
use cw_storage_plus::Deque; | ||
|
||
let deque: Deque<u32> = Deque::new("d"); | ||
|
||
deque.push_back(&mut storage, &2).unwrap(); | ||
deque.push_back(&mut storage, &3).unwrap(); | ||
deque.push_front(&mut storage, &1).unwrap(); | ||
|
||
let mut iter = deque.iter(&storage).unwrap(); | ||
|
||
assert_eq!(iter.next(), Some(Ok(1))); | ||
assert_eq!(iter.next(), Some(Ok(2))); | ||
assert_eq!(iter.next(), Some(Ok(3))); | ||
assert_eq!(iter.next(), None); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,80 @@ | ||
# `Item` | ||
|
||
## Overview | ||
An `Item` is a container that stores a single value under a specific key in | ||
storage. | ||
|
||
## Examples | ||
Merely constructing the `Item` object does not commit anything to storage. If an | ||
`Item` has never been written to before (or the value has been | ||
[removed](https://docs.rs/cw-storage-plus/latest/cw_storage_plus/struct.Item.html#method.remove)), | ||
it will be empty. | ||
|
||
Under the hood, values are serialized with [`serde`](https://serde.rs/) and | ||
[`serde_json_wasm`](https://docs.rs/serde_json_wasm/). | ||
|
||
Use `save` to write to an `Item`. | ||
|
||
Use `load` to read from the `Item`, producing an error if the `Item` is empty or | ||
if deserialization fails. | ||
|
||
Use `may_load` if you want to explicitly handle the possibility the `Item` is | ||
empty - this will produce an | ||
[`StdError`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/enum.StdError.html) | ||
if deserialization fails, but produce an `Ok(None)` if it is empty. | ||
|
||
More information can be found in the | ||
[API docs](https://docs.rs/cw-storage-plus/latest/cw_storage_plus/struct.Item.html). | ||
|
||
## Usage examples | ||
|
||
### Saving an admin address | ||
|
||
### Saving a config structure | ||
```rust template="storage" | ||
use cw_storage_plus::Item; | ||
|
||
let admin: Item<String> = Item::new("a"); | ||
assert_eq!(admin.may_load(&storage).unwrap(), None); | ||
|
||
admin.save(&mut storage, &"some_address".to_string()).unwrap(); | ||
assert_eq!(admin.load(&storage).unwrap(), "some_address"); | ||
``` | ||
|
||
### Maintaining a config structure | ||
|
||
```rust template="storage" | ||
use cw_storage_plus::Item; | ||
use serde::{Serialize, Deserialize}; | ||
|
||
#[cw_serde] | ||
struct Config { | ||
admin: String, | ||
interest_rate: Decimal, | ||
} | ||
|
||
let cfg = Config { | ||
admin: "some_address".to_string(), | ||
interest_rate: Decimal::percent(5), | ||
}; | ||
let cfg_storage: Item<Config> = Item::new("c"); | ||
cfg_storage.save(&mut storage, &cfg).unwrap(); | ||
|
||
assert_eq!(cfg_storage.load(&storage).unwrap(), 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_storage_plus::Item; | ||
|
||
let counter: Item<u128> = Item::new("t"); | ||
|
||
let mut total = counter.may_load(&storage).unwrap().unwrap_or(0); | ||
|
||
assert_eq!(total, 0); | ||
total += 1; | ||
|
||
counter.save(&mut storage, &total).unwrap(); | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this way instead of importing stuff directly in the file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that we don't have to add
#[allow(unused_imports)]
to everyuse
directive if we decide to add more. It looks awful.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#![allow(unused_imports)]
at the top of the file onceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aumetra My instinct is to be more granular so that we still get warnings in other places, where they might be useful feedback.