-
Notifications
You must be signed in to change notification settings - Fork 2
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 #46 from CosmWasm/cw-fix-approach-2
Make cw-storey work with &mut dyn cosmwasm_std::Storage
- Loading branch information
Showing
3 changed files
with
260 additions
and
14 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,151 @@ | ||
use storey::storage::{StorageBackend, StorageBackendMut}; | ||
use storey::storage::{IterableStorage, RevIterableStorage, StorageBackend, StorageBackendMut}; | ||
|
||
/// A wrapper around a type implementing [`cosmwasm_std::Storage`] that integrates it with [`storey`]. | ||
pub struct CwStorage<S>(pub S); | ||
|
||
impl<S> StorageBackend for CwStorage<S> | ||
impl<S> StorageBackend for CwStorage<&S> | ||
where | ||
S: cosmwasm_std::Storage, | ||
S: cosmwasm_std::Storage + ?Sized, | ||
{ | ||
fn get(&self, key: &[u8]) -> Option<Vec<u8>> { | ||
cosmwasm_std::Storage::get(&self.0, key) | ||
cosmwasm_std::Storage::get(self.0, key) | ||
} | ||
} | ||
|
||
impl<S> StorageBackendMut for CwStorage<S> | ||
impl<S> StorageBackend for CwStorage<&mut S> | ||
where | ||
S: cosmwasm_std::Storage, | ||
S: cosmwasm_std::Storage + ?Sized, | ||
{ | ||
fn get(&self, key: &[u8]) -> Option<Vec<u8>> { | ||
cosmwasm_std::Storage::get(self.0, key) | ||
} | ||
} | ||
|
||
impl<S> StorageBackendMut for CwStorage<&mut S> | ||
where | ||
S: cosmwasm_std::Storage + ?Sized, | ||
{ | ||
fn set(&mut self, key: &[u8], value: &[u8]) { | ||
cosmwasm_std::Storage::set(&mut self.0, key, value) | ||
cosmwasm_std::Storage::set(self.0, key, value) | ||
} | ||
|
||
fn remove(&mut self, key: &[u8]) { | ||
cosmwasm_std::Storage::remove(&mut self.0, key) | ||
cosmwasm_std::Storage::remove(self.0, key) | ||
} | ||
} | ||
|
||
impl<S> IterableStorage for CwStorage<&S> | ||
where | ||
S: cosmwasm_std::Storage + ?Sized, | ||
{ | ||
type KeysIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a; | ||
type ValuesIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a; | ||
type PairsIterator<'a> = Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a> where Self: 'a; | ||
|
||
fn keys<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::KeysIterator<'a> { | ||
self.0 | ||
.range_keys(start, end, cosmwasm_std::Order::Ascending) | ||
} | ||
|
||
fn values<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::ValuesIterator<'a> { | ||
self.0 | ||
.range_values(start, end, cosmwasm_std::Order::Ascending) | ||
} | ||
|
||
fn pairs<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::PairsIterator<'a> { | ||
self.0.range(start, end, cosmwasm_std::Order::Ascending) | ||
} | ||
} | ||
|
||
impl<S> IterableStorage for CwStorage<&mut S> | ||
where | ||
S: cosmwasm_std::Storage + ?Sized, | ||
{ | ||
type KeysIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a; | ||
type ValuesIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a; | ||
type PairsIterator<'a> = Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a> where Self: 'a; | ||
|
||
fn keys<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::KeysIterator<'a> { | ||
self.0 | ||
.range_keys(start, end, cosmwasm_std::Order::Ascending) | ||
} | ||
|
||
fn values<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::ValuesIterator<'a> { | ||
self.0 | ||
.range_values(start, end, cosmwasm_std::Order::Ascending) | ||
} | ||
|
||
fn pairs<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::PairsIterator<'a> { | ||
self.0.range(start, end, cosmwasm_std::Order::Ascending) | ||
} | ||
} | ||
|
||
impl<S> RevIterableStorage for CwStorage<&S> | ||
where | ||
S: cosmwasm_std::Storage + ?Sized, | ||
{ | ||
type RevKeysIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a; | ||
type RevValuesIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a; | ||
type RevPairsIterator<'a> = Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a> where Self: 'a; | ||
|
||
fn rev_keys<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevKeysIterator<'a> { | ||
self.0 | ||
.range_keys(start, end, cosmwasm_std::Order::Descending) | ||
} | ||
|
||
fn rev_values<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevValuesIterator<'a> { | ||
self.0 | ||
.range_values(start, end, cosmwasm_std::Order::Descending) | ||
} | ||
|
||
fn rev_pairs<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevPairsIterator<'a> { | ||
self.0.range(start, end, cosmwasm_std::Order::Descending) | ||
} | ||
} | ||
|
||
impl<S> RevIterableStorage for CwStorage<&mut S> | ||
where | ||
S: cosmwasm_std::Storage + ?Sized, | ||
{ | ||
type RevKeysIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a; | ||
type RevValuesIterator<'a> = Box<dyn Iterator<Item = Vec<u8>> + 'a> where Self: 'a; | ||
type RevPairsIterator<'a> = Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a> where Self: 'a; | ||
|
||
fn rev_keys<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevKeysIterator<'a> { | ||
self.0 | ||
.range_keys(start, end, cosmwasm_std::Order::Descending) | ||
} | ||
|
||
fn rev_values<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevValuesIterator<'a> { | ||
self.0 | ||
.range_values(start, end, cosmwasm_std::Order::Descending) | ||
} | ||
|
||
fn rev_pairs<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevPairsIterator<'a> { | ||
self.0.range(start, end, cosmwasm_std::Order::Descending) | ||
} | ||
} |
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,18 +1,49 @@ | ||
use cw_storey::{containers::Item, CwStorage}; | ||
|
||
use cosmwasm_std::Storage as _; | ||
use storey::containers::{IterableAccessor as _, Map}; | ||
|
||
#[test] | ||
fn smoke_test() { | ||
let mut storage = CwStorage(cosmwasm_std::testing::MockStorage::new()); | ||
let mut raw_storage = cosmwasm_std::testing::MockStorage::new(); | ||
let dyn_storage: &mut dyn cosmwasm_std::Storage = &mut raw_storage; | ||
let mut storage = CwStorage(dyn_storage); | ||
|
||
let item1 = Item::<u64>::new(0); | ||
|
||
item1.access(&mut storage).set(&42).unwrap(); | ||
assert_eq!(item1.access(&storage).get().unwrap(), Some(42)); | ||
assert_eq!(item1.access(&mut storage).get().unwrap(), Some(42)); | ||
|
||
let item2 = Item::<u64>::new(1); | ||
assert_eq!(item2.access(&storage).get().unwrap(), None); | ||
assert_eq!(item2.access(&mut storage).get().unwrap(), None); | ||
|
||
assert_eq!(storage.0.get(&[0]), Some(vec![42])); | ||
} | ||
|
||
#[test] | ||
fn map() { | ||
let mut raw_storage = cosmwasm_std::testing::MockStorage::new(); | ||
let dyn_storage: &mut dyn cosmwasm_std::Storage = &mut raw_storage; | ||
let mut storage = CwStorage(dyn_storage); | ||
|
||
let map = Map::<String, Item<u32>>::new(0); | ||
|
||
map.access(&mut storage).entry_mut("foo").set(&42).unwrap(); | ||
} | ||
|
||
#[test] | ||
fn iteration() { | ||
let mut raw_storage = cosmwasm_std::testing::MockStorage::new(); | ||
let dyn_storage: &mut dyn cosmwasm_std::Storage = &mut raw_storage; | ||
let mut storage = CwStorage(dyn_storage); | ||
|
||
let map = Map::<String, Item<u32>>::new(0); | ||
|
||
map.access(&mut storage).entry_mut("foo").set(&42).unwrap(); | ||
map.access(&mut storage).entry_mut("bar").set(&43).unwrap(); | ||
|
||
let access = map.access(&mut storage); | ||
let mut iter = access.keys(); | ||
assert_eq!(iter.next().unwrap().unwrap().0, "bar"); | ||
assert_eq!(iter.next().unwrap().unwrap().0, "foo"); | ||
assert!(iter.next().is_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