-
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 #20 from CosmWasm/cw-storey
Introduce `cw-storey`
- Loading branch information
Showing
8 changed files
with
82 additions
and
16 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
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
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,25 @@ | ||
use storey::{StorageBackend, StorageBackendMut}; | ||
|
||
pub struct CwStorage<S>(pub S); | ||
|
||
impl<S> StorageBackend for CwStorage<S> | ||
where | ||
S: cosmwasm_std::Storage, | ||
{ | ||
fn get(&self, key: &[u8]) -> Option<Vec<u8>> { | ||
cosmwasm_std::Storage::get(&self.0, key) | ||
} | ||
} | ||
|
||
impl<S> StorageBackendMut for CwStorage<S> | ||
where | ||
S: cosmwasm_std::Storage, | ||
{ | ||
fn set(&mut self, key: &[u8], value: &[u8]) { | ||
cosmwasm_std::Storage::set(&mut self.0, key, value) | ||
} | ||
|
||
fn remove(&mut self, key: &[u8]) { | ||
cosmwasm_std::Storage::remove(&mut self.0, key) | ||
} | ||
} |
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,4 @@ | ||
pub type Item<T> = storey::containers::Item<T, crate::encoding::CwEncoding>; | ||
pub type Column<T> = storey::containers::Column<T, crate::encoding::CwEncoding>; | ||
|
||
pub use storey::containers::Map; |
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,26 @@ | ||
use storey::encoding::{Cover, DecodableWithImpl, EncodableWithImpl, Encoding}; | ||
|
||
pub struct CwEncoding; | ||
|
||
impl Encoding for CwEncoding { | ||
type DecodeError = (); | ||
type EncodeError = (); | ||
} | ||
|
||
impl<T> EncodableWithImpl<CwEncoding> for Cover<&T> | ||
where | ||
T: serde::Serialize, | ||
{ | ||
fn encode_impl(self) -> Result<Vec<u8>, ()> { | ||
rmp_serde::to_vec(self.0).map_err(|_| ()) | ||
} | ||
} | ||
|
||
impl<T> DecodableWithImpl<CwEncoding> for Cover<T> | ||
where | ||
T: serde::de::DeserializeOwned, | ||
{ | ||
fn decode_impl(data: &[u8]) -> Result<Self, ()> { | ||
rmp_serde::from_slice(data).map(Cover).map_err(|_| ()) | ||
} | ||
} |
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,14 +1,5 @@ | ||
pub fn add(left: usize, right: usize) -> usize { | ||
left + right | ||
} | ||
mod backend; | ||
pub mod containers; | ||
mod encoding; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
} | ||
} | ||
pub use backend::CwStorage; |
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,18 @@ | ||
use cw_storey::{containers::Item, CwStorage}; | ||
|
||
use cosmwasm_std::Storage as _; | ||
|
||
#[test] | ||
fn smoke_test() { | ||
let mut storage = CwStorage(cosmwasm_std::testing::MockStorage::new()); | ||
|
||
let item1 = Item::<u64>::new(&[0]); | ||
|
||
item1.access(&mut storage).set(&42).unwrap(); | ||
assert_eq!(item1.access(&storage).get().unwrap(), Some(42)); | ||
|
||
let item2 = Item::<u64>::new(&[1]); | ||
assert_eq!(item2.access(&storage).get().unwrap(), None); | ||
|
||
assert_eq!(storage.0.get(&[0]), Some(vec![42])); | ||
} |
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