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

{Keymap, DequeStore, AppendStore, Item, KeySet}::new() accepts str instead of bytes #73

Open
wants to merge 5 commits into
base: match-cw-storage-plus-base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
52 changes: 26 additions & 26 deletions packages/storage/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ And initialize it using the following lines:
```rust
# use cosmwasm_std::Addr;
# use secret_toolkit_storage::Item;
pub static OWNER: Item<Addr> = Item::new(b"owner");
pub static OWNER: Item<Addr> = Item::new("owner");
```

This uses Bincode2 to serde Addr by default. To specify the Serde algorithm as Json, first import it from `secret-toolkit::serialization`
Expand All @@ -58,8 +58,8 @@ then
# #[derive(Serialize, Deserialize)]
# enum SomeEnum {};
#
pub static OWNER: Item<Addr> = Item::new(b"owner");
pub static SOME_ENUM: Item<SomeEnum, Json> = Item::new(b"some_enum");
pub static OWNER: Item<Addr> = Item::new("owner");
pub static SOME_ENUM: Item<SomeEnum, Json> = Item::new("some_enum");
```

#### **Read/Write**
Expand All @@ -70,7 +70,7 @@ The way to read/write to/from storage is to use its methods. These methods are `
# use cosmwasm_std::{Addr, testing::mock_dependencies, StdError};
# use secret_toolkit_storage::Item;
#
# pub static OWNER: Item<Addr> = Item::new(b"owner");
# pub static OWNER: Item<Addr> = Item::new("owner");
#
# let mut deps = mock_dependencies();
# OWNER.save(&mut deps.storage, &Addr::unchecked("owner-addr"))?;
Expand All @@ -84,7 +84,7 @@ let owner_addr = OWNER.load(&deps.storage)?;
# use cosmwasm_std::{Addr, testing::{mock_dependencies, mock_info}, StdError};
# use secret_toolkit_storage::Item;
#
# pub static OWNER: Item<Addr> = Item::new(b"owner");
# pub static OWNER: Item<Addr> = Item::new("owner");
#
# let mut deps = mock_dependencies();
# let info = mock_info("sender", &[]);
Expand All @@ -97,7 +97,7 @@ OWNER.save(&mut deps.storage, &info.sender)?;
# use cosmwasm_std::{Addr, testing::mock_dependencies, StdError};
# use secret_toolkit_storage::Item;
#
# pub static OWNER: Item<Addr> = Item::new(b"owner");
# pub static OWNER: Item<Addr> = Item::new("owner");
#
# let mut deps = mock_dependencies();
#
Expand All @@ -110,7 +110,7 @@ let may_addr = OWNER.may_load(&deps.storage)?;
# use cosmwasm_std::{Addr, testing::mock_dependencies, StdError};
# use secret_toolkit_storage::Item;
#
# pub static OWNER: Item<Addr> = Item::new(b"owner");
# pub static OWNER: Item<Addr> = Item::new("owner");
#
# let mut deps = mock_dependencies();
#
Expand All @@ -122,7 +122,7 @@ let may_addr = OWNER.remove(&mut deps.storage);
# use cosmwasm_std::{Addr, testing::{mock_dependencies, mock_info}, StdError};
# use secret_toolkit_storage::Item;
#
# pub static OWNER: Item<Addr> = Item::new(b"owner");
# pub static OWNER: Item<Addr> = Item::new("owner");
#
# let mut deps = mock_dependencies();
# let info = mock_info("sender", &[]);
Expand Down Expand Up @@ -157,7 +157,7 @@ use secret_toolkit::storage::{AppendStore};
```rust
# use secret_toolkit_storage::AppendStore;
# use cosmwasm_std::StdError;
pub static COUNT_STORE: AppendStore<i32> = AppendStore::new(b"count");
pub static COUNT_STORE: AppendStore<i32> = AppendStore::new("count");
# Ok::<(), StdError>(())
```

Expand All @@ -169,7 +169,7 @@ Often times we need these storage objects to be associated to a user address or
# use secret_toolkit_storage::AppendStore;
# use cosmwasm_std::testing::mock_info;
# let info = mock_info("sender", &[]);
# pub static COUNT_STORE: AppendStore<i32> = AppendStore::new(b"count");
# pub static COUNT_STORE: AppendStore<i32> = AppendStore::new("count");
#
// The compiler knows that user_count_store is AppendStore<i32, Bincode2>
let user_count_store = COUNT_STORE.add_suffix(info.sender.to_string().as_bytes());
Expand All @@ -179,7 +179,7 @@ Sometimes when iterating these objects, we may want to load the next `n` objects

```rust
# use secret_toolkit_storage::AppendStore;
pub static COUNT_STORE: AppendStore<i32> = AppendStore::new_with_page_size(b"count", 5);
pub static COUNT_STORE: AppendStore<i32> = AppendStore::new_with_page_size("count", 5);
```

#### **Read/Write**
Expand All @@ -193,7 +193,7 @@ AppendStore also implements a readonly iterator feature. This feature is also us
```rust
# use cosmwasm_std::{StdError, testing::mock_dependencies};
# use secret_toolkit_storage::AppendStore;
# pub static COUNT_STORE: AppendStore<i32> = AppendStore::new_with_page_size(b"count", 5);
# pub static COUNT_STORE: AppendStore<i32> = AppendStore::new_with_page_size("count", 5);
# let deps = mock_dependencies();
#
let iter = COUNT_STORE.iter(&deps.storage)?;
Expand All @@ -205,7 +205,7 @@ More examples can be found in the unit tests. And the paging wrapper is used in
```rust
# use cosmwasm_std::{StdError, testing::mock_dependencies};
# use secret_toolkit_storage::AppendStore;
# pub static COUNT_STORE: AppendStore<i32> = AppendStore::new_with_page_size(b"count", 5);
# pub static COUNT_STORE: AppendStore<i32> = AppendStore::new_with_page_size("count", 5);
# let deps = mock_dependencies();
#
let start_page: u32 = 0;
Expand All @@ -229,7 +229,7 @@ use secret_toolkit::storage::{DequeStore};

```rust
# use secret_toolkit_storage::DequeStore;
pub static COUNT_STORE: DequeStore<i32> = DequeStore::new(b"count");
pub static COUNT_STORE: DequeStore<i32> = DequeStore::new("count");
```

> ❗ Initializing the object as const instead of static will also work but be less efficient since the variable won't be able to cache length data.
Expand Down Expand Up @@ -268,8 +268,8 @@ use secret_toolkit::storage::{Keymap, KeymapBuilder};
# #[derive(Serialize, Deserialize)]
# struct Foo { vote_for: String };
#
pub static ADDR_VOTE: Keymap<Addr, Foo> = Keymap::new(b"vote");
pub static BET_STORE: Keymap<u32, BetInfo> = Keymap::new(b"bet");
pub static ADDR_VOTE: Keymap<Addr, Foo> = Keymap::new("vote");
pub static BET_STORE: Keymap<u32, BetInfo> = Keymap::new("bet");
```

> ❗ Initializing the object as const instead of static will also work but be less efficient since the variable won't be able to cache length data.
Expand All @@ -288,9 +288,9 @@ For example, suppose that in your contract, a user can make multiple bets. Then,
# struct BetInfo { bet_outcome: u32, amount: u32 };
# let info = mock_info("sender", &[]);
#
pub static BET_STORE: Keymap<u32, BetInfo> = Keymap::new(b"bet");
pub static BET_STORE: Keymap<u32, BetInfo> = Keymap::new("bet");
// The compiler knows that user_bet_store is AppendStore<u32, BetInfo>
let user_count_store = BET_STORE.add_suffix(info.sender.to_string().as_bytes());
let user_count_store = BET_STORE.add_suffix(info.sender.as_str());
```

#### **Advanced Init**
Expand All @@ -311,10 +311,10 @@ The following is used to produce a Keymap without an iterator in `state.rs`
# struct Foo { vote: u32 };
#
pub static JSON_ADDR_VOTE: Keymap<String, Foo, Json, WithoutIter> =
KeymapBuilder::new(b"json_vote").without_iter().build();
KeymapBuilder::new("json_vote").without_iter().build();

pub static BINCODE_ADDR_VOTE: Keymap<String, Foo, Bincode2, WithoutIter> =
KeymapBuilder::new(b"bincode_vote").without_iter().build();
KeymapBuilder::new("bincode_vote").without_iter().build();
```

The following is used to produce a Keymap with modified index page size:
Expand All @@ -327,10 +327,10 @@ The following is used to produce a Keymap with modified index page size:
# #[derive(Serialize, Deserialize)]
# struct Foo { vote: u32 };
#
pub static ADDR_VOTE: Keymap<Addr, Foo> = KeymapBuilder::new(b"page_vote").with_page_size(13).build();
pub static ADDR_VOTE: Keymap<Addr, Foo> = KeymapBuilder::new("page_vote").with_page_size(13).build();

pub static JSON_VOTE: Keymap<Addr, Foo, Json> =
KeymapBuilder::new(b"page_vote").with_page_size(3).build();
KeymapBuilder::new("page_vote").with_page_size(3).build();
```

#### **Read/Write**
Expand All @@ -348,7 +348,7 @@ To insert, remove, read from the keymap, do the following:
#
# let mut deps = mock_dependencies();
# let info = mock_info("sender", &[]);
# pub static ADDR_VOTE: Keymap<Addr, Foo> = KeymapBuilder::new(b"page_vote").with_page_size(13).build();
# pub static ADDR_VOTE: Keymap<Addr, Foo> = KeymapBuilder::new("page_vote").with_page_size(13).build();
#
let user_addr: Addr = info.sender;

Expand Down Expand Up @@ -384,7 +384,7 @@ Here are some select examples from the unit tests:
fn test_keymap_iter_keys() -> StdResult<()> {
let mut storage = MockStorage::new();

let keymap: Keymap<String, Foo> = Keymap::new(b"test");
let keymap: Keymap<String, Foo> = Keymap::new("test");
let foo1 = Foo {
string: "string one".to_string(),
number: 1111,
Expand Down Expand Up @@ -422,7 +422,7 @@ fn test_keymap_iter_keys() -> StdResult<()> {
fn test_keymap_iter() -> StdResult<()> {
let mut storage = MockStorage::new();

let keymap: Keymap<Vec<u8>, Foo> = Keymap::new(b"test");
let keymap: Keymap<Vec<u8>, Foo> = Keymap::new("test");
let foo1 = Foo {
string: "string one".to_string(),
number: 1111,
Expand Down Expand Up @@ -463,7 +463,7 @@ use secret_toolkit::storage::{Keyset, KeysetBuilder};
```rust
# use secret_toolkit_storage::Keyset;
# use cosmwasm_std::Addr;
pub static WHITELIST: Keyset<Addr> = Keyset::new(b"whitelist");
pub static WHITELIST: Keyset<Addr> = Keyset::new("whitelist");
```

> ❗ Initializing the object as const instead of static will also work but be less efficient since the variable won't be able to cache length data.
Expand Down
32 changes: 16 additions & 16 deletions packages/storage/src/append_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ where

impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
/// constructor
pub const fn new(namespace: &'a [u8]) -> Self {
pub const fn new(namespace: &'a str) -> Self {
Self {
namespace,
namespace: namespace.as_bytes(),
prefix: None,
page_size: DEFAULT_PAGE_SIZE,
length: Mutex::new(None),
Expand All @@ -47,12 +47,12 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
}
}

pub const fn new_with_page_size(namespace: &'a [u8], page_size: u32) -> Self {
pub const fn new_with_page_size(namespace: &'a str, page_size: u32) -> Self {
if page_size == 0 {
panic!("zero index page size used in append_store")
}
Self {
namespace,
namespace: namespace.as_bytes(),
prefix: None,
page_size,
length: Mutex::new(None),
Expand Down Expand Up @@ -447,7 +447,7 @@ mod tests {
#[test]
fn test_push_pop() -> StdResult<()> {
let mut storage = MockStorage::new();
let append_store: AppendStore<i32> = AppendStore::new(b"test");
let append_store: AppendStore<i32> = AppendStore::new("test");
append_store.push(&mut storage, &1234)?;
append_store.push(&mut storage, &2143)?;
append_store.push(&mut storage, &3412)?;
Expand All @@ -465,7 +465,7 @@ mod tests {
#[test]
fn test_length() -> StdResult<()> {
let mut storage = MockStorage::new();
let append_store: AppendStore<i32> = AppendStore::new_with_page_size(b"test", 3);
let append_store: AppendStore<i32> = AppendStore::new_with_page_size("test", 3);

assert!(append_store.length.lock().unwrap().eq(&None));
assert_eq!(append_store.get_len(&storage)?, 0);
Expand Down Expand Up @@ -498,7 +498,7 @@ mod tests {
#[test]
fn test_iterator() -> StdResult<()> {
let mut storage = MockStorage::new();
let append_store: AppendStore<i32> = AppendStore::new(b"test");
let append_store: AppendStore<i32> = AppendStore::new("test");
append_store.push(&mut storage, &1234)?;
append_store.push(&mut storage, &2143)?;
append_store.push(&mut storage, &3412)?;
Expand Down Expand Up @@ -531,7 +531,7 @@ mod tests {
#[test]
fn test_reverse_iterator() -> StdResult<()> {
let mut storage = MockStorage::new();
let append_store: AppendStore<i32> = AppendStore::new(b"test");
let append_store: AppendStore<i32> = AppendStore::new("test");
append_store.push(&mut storage, &1234)?;
append_store.push(&mut storage, &2143)?;
append_store.push(&mut storage, &3412)?;
Expand Down Expand Up @@ -570,7 +570,7 @@ mod tests {
#[test]
fn test_json_push_pop() -> StdResult<()> {
let mut storage = MockStorage::new();
let append_store: AppendStore<i32, Json> = AppendStore::new(b"test");
let append_store: AppendStore<i32, Json> = AppendStore::new("test");
append_store.push(&mut storage, &1234)?;
append_store.push(&mut storage, &2143)?;
append_store.push(&mut storage, &3412)?;
Expand All @@ -589,7 +589,7 @@ mod tests {
fn test_suffixed_pop() -> StdResult<()> {
let mut storage = MockStorage::new();
let suffix: &[u8] = b"test_suffix";
let original_store: AppendStore<i32> = AppendStore::new(b"test");
let original_store: AppendStore<i32> = AppendStore::new("test");
let append_store = original_store.add_suffix(suffix);
append_store.push(&mut storage, &1234)?;
append_store.push(&mut storage, &2143)?;
Expand All @@ -616,7 +616,7 @@ mod tests {
fn test_suffixed_reverse_iter_with_size(page_size: u32) -> StdResult<()> {
let mut storage = MockStorage::new();
let suffix: &[u8] = b"test_suffix";
let original_store: AppendStore<i32> = AppendStore::new_with_page_size(b"test", page_size);
let original_store: AppendStore<i32> = AppendStore::new_with_page_size("test", page_size);
let append_store = original_store.add_suffix(suffix);

append_store.push(&mut storage, &1234)?;
Expand Down Expand Up @@ -660,7 +660,7 @@ mod tests {
fn test_suffix_iter() -> StdResult<()> {
let mut storage = MockStorage::new();
let suffix: &[u8] = b"test_suffix";
let original_store: AppendStore<i32> = AppendStore::new(b"test");
let original_store: AppendStore<i32> = AppendStore::new("test");
let append_store = original_store.add_suffix(suffix);

append_store.push(&mut storage, &1234)?;
Expand Down Expand Up @@ -704,7 +704,7 @@ mod tests {
// Check the default behavior is Bincode2
let mut storage = MockStorage::new();

let append_store: AppendStore<i32> = AppendStore::new_with_page_size(b"test", page_size);
let append_store: AppendStore<i32> = AppendStore::new_with_page_size("test", page_size);
append_store.push(&mut storage, &1234)?;

let key = [append_store.as_slice(), INDEXES, &0_u32.to_be_bytes()].concat();
Expand All @@ -721,7 +721,7 @@ mod tests {
// Check that overriding the serializer with Json works
let mut storage = MockStorage::new();
let json_append_store: AppendStore<i32, Json> =
AppendStore::new_with_page_size(b"test2", page_size);
AppendStore::new_with_page_size("test2", page_size);
json_append_store.push(&mut storage, &1234)?;

let key = [json_append_store.as_slice(), INDEXES, &0_u32.to_be_bytes()].concat();
Expand Down Expand Up @@ -751,7 +751,7 @@ mod tests {

fn test_removes_with_size(page_size: u32) -> StdResult<()> {
let mut storage = MockStorage::new();
let deque_store: AppendStore<i32> = AppendStore::new_with_page_size(b"test", page_size);
let deque_store: AppendStore<i32> = AppendStore::new_with_page_size("test", page_size);
deque_store.push(&mut storage, &1)?;
deque_store.push(&mut storage, &2)?;
deque_store.push(&mut storage, &3)?;
Expand Down Expand Up @@ -815,7 +815,7 @@ mod tests {
#[test]
fn test_paging() -> StdResult<()> {
let mut storage = MockStorage::new();
let append_store: AppendStore<u32> = AppendStore::new(b"test");
let append_store: AppendStore<u32> = AppendStore::new("test");

let page_size: u32 = 5;
let total_items: u32 = 50;
Expand Down
Loading