Skip to content

Commit 437f19b

Browse files
committed
Readme updated. Clone implemented for AppendStore and DequeStore
1 parent 3adaf18 commit 437f19b

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

packages/storage/Readme.md

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Secret Contract Development Toolkit - Storage Tools
22

3-
⚠️ This package is a sub-package of the `secret-toolkit` package. Please see its crate page for more context. You need Rust 1.61+ to compile this package.
3+
⚠️ This package is a sub-package of the `secret-toolkit` package. Please see its crate page for more context. You need Rust 1.63+ to compile this package.
44

55
This package contains many tools related to storage access patterns. This readme file assumes basic familiarity with basic cosmwasm storage, [click here to learn about this](https://docs.scrt.network/secret-network-documentation/development/secret-contracts/storage).
66

@@ -28,7 +28,7 @@ This is the simplest storage object in this toolkit. It based on the similarly n
2828

2929
#### **Initialize**
3030

31-
This object is meant to be initialized as a constant in `state.rs`. However, it would also work perfectly fine if it was initialized during run time with a variable key (in this case though, you'd have to remind it what type of object is stored and its serde). Import it using the following lines:
31+
This object is meant to be initialized as a static constant in `state.rs`. However, it would also work perfectly fine if it was initialized during run time with a variable key (in this case though, you'd have to remind it what type of object is stored and its serde). Import it using the following lines:
3232

3333
```rust
3434
use secret_toolkit_storage::{Item}
@@ -37,7 +37,7 @@ use secret_toolkit_storage::{Item}
3737
And initialize it using the following lines:
3838

3939
```rust
40-
pub const OWNER: Item<HumanAddr> = Item::new(b"owner");
40+
pub static OWNER: Item<HumanAddr> = Item::new(b"owner");
4141
```
4242

4343
This uses Bincode2 to serde HumanAddr by default. To specify the Serde algorithm as Json, first import it from `secret-toolkit-serialization`
@@ -49,7 +49,7 @@ use secret_toolkit_serialization::{Bincode2, Json};
4949
then
5050

5151
```rust
52-
pub const SOME_ENUM: Item<SomeEnum, Json> = Item::new(b"some_enum");
52+
pub static SOME_ENUM: Item<SomeEnum, Json> = Item::new(b"some_enum");
5353
```
5454

5555
#### **Read/Write**
@@ -95,16 +95,18 @@ The same conventions from `Item` also apply here, that is:
9595

9696
#### **Initialize**
9797

98-
To import and intialize this storage object as a constant in `state.rs`, do the following:
98+
To import and intialize this storage object as a static constant in `state.rs`, do the following:
9999

100100
```rust
101101
use secret_toolkit::storage::{AppendStore}
102102
```
103103

104104
```rust
105-
pub const COUNT_STORE: AppendStore<i32> = AppendStore::new(b"count");
105+
pub static COUNT_STORE: AppendStore<i32> = AppendStore::new(b"count");
106106
```
107107

108+
> ❗ 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.
109+
108110
Often times we need these storage objects to be associated to a user address or some other key that is variable. In this case, you need not initialize a completely new AppendStore inside `contract.rs`. Instead, you can create a new AppendStore by adding a suffix to an already existing AppendStore. This has the benefit of preventing you from having to rewrite the signature of the AppendStore. For example
109111

110112
```rust
@@ -133,35 +135,24 @@ let page_size: u32 = 5;
133135
let values = user_count_store.paging(&deps.storage, start_page, page_size)?;
134136
```
135137

136-
> ❗ When using any iterators in any of the storage objects, the following will result in a compiling error.
137-
138-
```rust
139-
let iterator = COUNT_STORE.iter(&deps.storage)?;
140-
```
141-
142-
However, the follwoing will not result in an error:
143-
144-
```rust
145-
let append_store = COUNT_STORE
146-
let iterator = append_store.iter(&deps.storage)?;
147-
```
148-
149138
### **DequeStore**
150139

151140
This is a storage wrapper based on AppendStore that replicates a double ended list. This storage object allows the user to efficiently pop/push items to either end of the list.
152141

153142
#### **Init**
154143

155-
To import and intialize this storage object as a constant in `state.rs`, do the following:
144+
To import and intialize this storage object as a static constant in `state.rs`, do the following:
156145

157146
```rust
158147
use secret_toolkit_storage::{DequeStore}
159148
```
160149

161150
```rust
162-
pub const COUNT_STORE: DequeStore<i32> = DequeStore::new(b"count");
151+
pub static COUNT_STORE: DequeStore<i32> = DequeStore::new(b"count");
163152
```
164153

154+
> ❗ 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.
155+
165156
#### **Read/Write**
166157

167158
The main user facing methods to read/write to DequeStore are `pop_back`, `pop_front`, `push_back`, `push_front`, `get_len`, `get_off`, `set_at` (which replaces data at a position within the length bound), `clear` (which deletes all data in the storage), `remove` (which removes an item in an arbitrary position, this is very inefficient). An extensive list of examples of these being used can be found inside the unit tests of DequeStore found in `deque_store.rs`.
@@ -179,17 +170,19 @@ be returned in each page.
179170

180171
#### **Init**
181172

182-
To import and intialize this storage object as a constant in `state.rs`, do the following:
173+
To import and intialize this storage object as a static constant in `state.rs`, do the following:
183174

184175
```rust
185176
use secret_toolkit_storage::{Keymap}
186177
```
187178

188179
```rust
189-
pub const ADDR_VOTE: Keymap<HumanAddr, Foo> = Keymap::new(b"vote");
190-
pub const BET_STORE: Keymap<u32, BetInfo> = Keymap::new(b"vote");
180+
pub static ADDR_VOTE: Keymap<HumanAddr, Foo> = Keymap::new(b"vote");
181+
pub static BET_STORE: Keymap<u32, BetInfo> = Keymap::new(b"vote");
191182
```
192183

184+
> ❗ 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.
185+
193186
You can use Json serde algorithm by changing the signature to `Keymap<HumanAddr, Uint128, Json>`, similar to all the other storage objects above. However, keep in mind that the Serde algorthm is used to serde both the stored object (`Uint128`) AND the key (`HumanAddr`).
194187

195188
If you need to associate a keymap to a user address (or any other variable), then you can also do this using the `.add_suffix` method.

packages/storage/src/append_store.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
170170
}
171171
}
172172

173+
impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> Clone for AppendStore<'a, T, Ser> {
174+
fn clone(&self) -> Self {
175+
Self {
176+
namespace: self.namespace.clone(),
177+
prefix: self.prefix.clone(),
178+
length: Mutex::new(None),
179+
item_type: self.item_type.clone(),
180+
serialization_type: self.serialization_type.clone() }
181+
}
182+
}
183+
173184
impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
174185
fn as_slice(&self) -> &[u8] {
175186
if let Some(prefix) = &self.prefix {

packages/storage/src/deque_store.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> DequeStore<'a, T, Ser> {
283283
}
284284
}
285285

286+
impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> Clone for DequeStore<'a, T, Ser> {
287+
fn clone(&self) -> Self {
288+
Self {
289+
namespace: self.namespace.clone(),
290+
prefix: self.prefix.clone(),
291+
length: Mutex::new(None),
292+
offset: Mutex::new(None),
293+
item_type: self.item_type.clone(),
294+
serialization_type: self.serialization_type.clone() }
295+
}
296+
}
297+
286298
/// An iterator over the contents of the deque store.
287299
pub struct DequeStoreIter<'a, T, S, Ser>
288300
where

packages/storage/src/keymap.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,18 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser:
370370
}
371371
}
372372

373+
impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser: Serde> Clone for Keymap<'a, K, T, Ser> {
374+
fn clone(&self) -> Self {
375+
Self {
376+
namespace: self.namespace.clone(),
377+
prefix: self.prefix.clone(),
378+
length: Mutex::new(None),
379+
key_type: self.key_type.clone(),
380+
item_type: self.item_type.clone(),
381+
serialization_type: self.serialization_type.clone() }
382+
}
383+
}
384+
373385
/// An iterator over the keys of the Keymap.
374386
pub struct KeyIter<'a, K, T, S, Ser>
375387
where

0 commit comments

Comments
 (0)