Additional feature implementation to access cw-storage-plus
and cosmwasm-storage
Cow implementation of storage accessor in cw-storage-plus
to provide owned string variant of namespace, useful for constructing the accessor dynamically though format!
macro.
Also implements new useful storage accessor, such as ConditionalMultiIndex
and CustomDeseMultiIndex
Every struct will have access to new_owned
and new_ref
constant constructor function.
Like Item
from cw-storage-plus
but in Cow
.
rust
let item: ItemCow<Addr> = ItemCow::new_owned(String::from("g"));
const ITEM: ItemCow<Addr> = ItemCow::new_ref("g");
Like Map
from cw-storage-plus
but in Cow
.
rust
let addr_owne: MapCow<&Addr, u64> = MapCow::new_owned(String::from("g"));
const ADDR_REF: MapCow<&Addr, u64> = MapCow::new_ref("g");
Like IndexedMap
from cw-storage-plus
but in Cow
. Index
struct can be construct from normal Index
trait, like MultiIndex
and UniqueIndex
.
Like MultiIndex
from cw-storage-plus
but in Cow
. Also usable in normal IndexedMap
.
Like UniqueIndex
from cw-storage-plus
but in Cow
. Also usable in normal IndexedMap
.
```rust
struct ToIndex { id: u64, count: u64, address: Addr, }
struct ToIndexList<'a> {
count: MultiIndexCow<'a, (U64Key, Vec
const TO: IndexedMapCow
let to: IndexedMapCow
MultiIndexCow
with customizable index to pk deserialize function. Also usable in normal IndexedMap
.
Use deserialize_multi_kv_custom_pk
helper function to map old kv to new kv, also works with a fully customed one.
Use None
to operate as normal MultiIndexCow
.
```rust
struct Test { id: u64, val: Uint128, }
struct TestIndexes<'a> {
val: CustomDeseMultiIndex<'a, (U128Key, Vec
fn idm<'a>() -> IndexedMap<'a, U64Key, Test, TestIndexes<'a>> { IndexedMap::new( "test", TestIndexes { val: CustomDeseMultiIndex::newref( |t, _| { ( t.val.u128().into(), U64Key::new(u64::maxvalue() - t.id).joinedkey(), ) }, Some(|s, pk, kv| { deserializemultikvcustompk(s, pk, kv, |oldkv| { U64Key::new( u64::maxvalue() - u64::frombebytes(oldkv.asslice().tryinto().unwrap()), ) .joinedkey() }) }), "test", "test_val", ), }, ) }
```
CustomDeseMultiIndex
with addtional condition to save/remove from original indexed map. Useful for reducing composite key complexity. Also usable in normal IndexedMap
.
cond_fn
must be constant, otherwise might raise unexpected behavior.
```rust
struct Test { id: u64, val: Uint128, }
struct TestIndexes<'a> {
val: ConditionalMultiIndex<'a, (U128Key, Vec
fn idm<'a>() -> IndexedMap<'a, U64Key, Test, TestIndexes<'a>> { IndexedMap::new( "test", TestIndexes { val: ConditionalMultiIndex::newref( |t, k| (t.val.u128().into(), k), // only add to val if t.val > 100 |t| t.val.u128() > 100, None, "test", "test_val", ), }, ) }
```d