-
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.
- Loading branch information
0 parents
commit 773b873
Showing
5 changed files
with
171 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/Cargo.lock |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "stork" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
cosmwasm-std = "1.5.0" |
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
impl<B> super::StorageBackend for B | ||
where | ||
B: cosmwasm_std::Storage, | ||
{ | ||
fn get(&self, key: &[u8]) -> Option<Vec<u8>> { | ||
<B as cosmwasm_std::Storage>::get(self, key) | ||
} | ||
|
||
fn set(&mut self, key: &[u8], value: &[u8]) { | ||
<B as cosmwasm_std::Storage>::set(self, key, value) | ||
} | ||
|
||
fn remove(&mut self, key: &[u8]) { | ||
<B as cosmwasm_std::Storage>::remove(self, key) | ||
} | ||
} | ||
|
||
impl<B> super::StorageIterableBackend for B | ||
where | ||
B: cosmwasm_std::Storage, | ||
{ | ||
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 pairs<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::PairsIterator<'a> { | ||
self.range(start, end, cosmwasm_std::Order::Ascending) | ||
} | ||
|
||
fn keys<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::KeysIterator<'a> { | ||
self.range_keys(start, end, cosmwasm_std::Order::Ascending) | ||
} | ||
|
||
fn values<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::ValuesIterator<'a> { | ||
self.range_values(start, end, cosmwasm_std::Order::Ascending) | ||
} | ||
} | ||
|
||
impl<B> super::StorageRevIterableBackend for B | ||
where | ||
B: cosmwasm_std::Storage, | ||
{ | ||
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_pairs<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevPairsIterator<'a> { | ||
self.range(start, end, cosmwasm_std::Order::Descending) | ||
} | ||
|
||
fn rev_keys<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevKeysIterator<'a> { | ||
self.range_keys(start, end, cosmwasm_std::Order::Descending) | ||
} | ||
|
||
fn rev_values<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevValuesIterator<'a> { | ||
self.range_values(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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
mod cosmwasm; | ||
|
||
pub trait StorageBackend { | ||
fn get(&self, key: &[u8]) -> Option<Vec<u8>>; | ||
fn set(&mut self, key: &[u8], value: &[u8]); | ||
fn remove(&mut self, key: &[u8]); | ||
|
||
fn has(&self, key: &[u8]) -> bool { | ||
self.get(key).is_some() | ||
} | ||
} | ||
|
||
pub trait StorageIterableBackend: StorageBackend { | ||
type KeysIterator<'a>: Iterator<Item = Vec<u8>> | ||
where | ||
Self: 'a; | ||
type ValuesIterator<'a>: Iterator<Item = Vec<u8>> | ||
where | ||
Self: 'a; | ||
type PairsIterator<'a>: Iterator<Item = (Vec<u8>, Vec<u8>)> | ||
where | ||
Self: 'a; | ||
|
||
fn keys<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::KeysIterator<'a>; | ||
fn values<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::ValuesIterator<'a>; | ||
fn pairs<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::PairsIterator<'a>; | ||
} | ||
|
||
pub trait StorageRevIterableBackend: StorageBackend { | ||
type RevKeysIterator<'a>: Iterator<Item = Vec<u8>> | ||
where | ||
Self: 'a; | ||
type RevValuesIterator<'a>: Iterator<Item = Vec<u8>> | ||
where | ||
Self: 'a; | ||
type RevPairsIterator<'a>: Iterator<Item = (Vec<u8>, Vec<u8>)> | ||
where | ||
Self: 'a; | ||
|
||
fn rev_keys<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevKeysIterator<'a>; | ||
fn rev_values<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevValuesIterator<'a>; | ||
fn rev_pairs<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevPairsIterator<'a>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
mod backend; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use cosmwasm_std::testing::MockStorage; | ||
|
||
use crate::backend::{ | ||
StorageBackend as _, StorageIterableBackend as _, StorageRevIterableBackend as _, | ||
}; | ||
|
||
#[test] | ||
fn foo() { | ||
let mut storage = MockStorage::new(); | ||
|
||
storage.set(b"foo", b"bar"); | ||
storage.set(b"baz", b"bax"); | ||
assert_eq!(storage.get(b"foo").unwrap(), b"bar"); | ||
|
||
assert!(storage.has(b"foo")); | ||
assert!(!storage.has(b"foobar")); | ||
|
||
assert_eq!( | ||
storage.keys(None, None).collect::<Vec<_>>(), | ||
vec![b"baz", b"foo"], | ||
); | ||
assert_eq!( | ||
storage.rev_keys(None, None).collect::<Vec<_>>(), | ||
vec![b"foo", b"baz"], | ||
); | ||
|
||
storage.remove(b"foo"); | ||
assert!(!storage.has(b"foo")); | ||
assert_eq!(storage.get(b"foo"), None); | ||
} | ||
} |