-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from CosmWasm/storage-plus-indexedmap
Add an `IndexedMap` article (without `MultiIndex`)
- Loading branch information
Showing
6 changed files
with
346 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,91 @@ | ||
#[allow(unused_imports)] | ||
mod imports { | ||
pub use cosmwasm_std::*; | ||
pub use cosmwasm_schema::cw_serde; | ||
pub use cosmwasm_std::*; | ||
} | ||
|
||
#[allow(unused_imports)] | ||
use imports::*; | ||
|
||
#[allow(dead_code, unused_variables)] | ||
mod users { | ||
use super::*; | ||
use cw_storage_plus::{index_list, IndexedMap, UniqueIndex}; | ||
|
||
pub type Handle = String; | ||
|
||
#[cw_serde] | ||
pub struct User { | ||
pub handle: String, | ||
pub country: String, | ||
} | ||
|
||
pub struct ExampleUsers { | ||
pub alice: User, | ||
pub bob: User, | ||
} | ||
|
||
pub fn example_users() -> ExampleUsers { | ||
ExampleUsers { | ||
alice: User { | ||
handle: "alice".to_string(), | ||
country: "Wonderland".to_string(), | ||
}, | ||
bob: User { | ||
handle: "bob".to_string(), | ||
country: "USA".to_string(), | ||
}, | ||
} | ||
} | ||
|
||
#[index_list(User)] | ||
pub struct UserIndexes<'a> { | ||
pub handle_ix: UniqueIndex<'a, Handle, User, Addr>, | ||
} | ||
pub fn user_indexes(prefix: &'static str) -> UserIndexes<'static> { | ||
UserIndexes { | ||
handle_ix: UniqueIndex::new(|user| user.handle.clone(), prefix), | ||
} | ||
} | ||
} | ||
#[test] | ||
fn doctest() { | ||
#[allow(unused_mut)] | ||
let mut storage = cosmwasm_std::testing::MockStorage::new(); | ||
{{code}} | ||
#[allow(unused_variables, unused_mut)] | ||
let mut storage = cosmwasm_std::testing::MockStorage::new(); | ||
let users = cw_storage_plus::IndexedMap::<Addr, _, _>::new("uu", users::user_indexes("uuh")); | ||
let users_data = [ | ||
( | ||
Addr::unchecked("aaa"), | ||
users::User { | ||
handle: "alice".to_string(), | ||
country: "Wonderland".to_string(), | ||
}, | ||
), | ||
( | ||
Addr::unchecked("bbb"), | ||
users::User { | ||
handle: "bob".to_string(), | ||
country: "USA".to_string(), | ||
}, | ||
), | ||
( | ||
Addr::unchecked("ccc"), | ||
users::User { | ||
handle: "carol".to_string(), | ||
country: "UK".to_string(), | ||
}, | ||
), | ||
]; | ||
for (addr, user) in users_data { | ||
users.save(&mut storage, addr, &user).unwrap(); | ||
} | ||
#[rustfmt::skip] | ||
{{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
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,5 +1,6 @@ | ||
{ | ||
"item": "Item", | ||
"map": "Map", | ||
"deque": "Deque" | ||
"deque": "Deque", | ||
"indexed-map": "IndexedMap" | ||
} |
Oops, something went wrong.