diff --git a/packages/storage-plus/benches/main.rs b/packages/storage-plus/benches/main.rs index 1b5e557b1..a0623257f 100644 --- a/packages/storage-plus/benches/main.rs +++ b/packages/storage-plus/benches/main.rs @@ -4,7 +4,7 @@ use rand::Rng; use std::mem; use std::time::Duration; -use cw_storage_plus::CwIntKey; +use cw_storage_plus::IntKey; fn bench_signed_int_key(c: &mut Criterion) { let mut group = c.benchmark_group("Signed int keys"); diff --git a/packages/storage-plus/src/de.rs b/packages/storage-plus/src/de.rs index c1b13889f..046d8b9cb 100644 --- a/packages/storage-plus/src/de.rs +++ b/packages/storage-plus/src/de.rs @@ -3,7 +3,7 @@ use std::convert::TryInto; use cosmwasm_std::{Addr, StdError, StdResult}; -use crate::int_key::CwIntKey; +use crate::int_key::IntKey; pub trait KeyDeserialize { type Output: Sized; diff --git a/packages/storage-plus/src/de_old.rs b/packages/storage-plus/src/de_old.rs deleted file mode 100644 index e8c5f529d..000000000 --- a/packages/storage-plus/src/de_old.rs +++ /dev/null @@ -1,106 +0,0 @@ -use std::array::TryFromSliceError; -use std::convert::TryInto; - -use cosmwasm_std::{StdError, StdResult}; - -use crate::de::KeyDeserialize; -use crate::keys_old::IntKeyOld; - -macro_rules! intkey_old_de { - (for $($t:ty),+) => { - $(impl KeyDeserialize for IntKeyOld<$t> { - type Output = $t; - - #[inline(always)] - fn from_vec(value: Vec) -> StdResult { - Ok(<$t>::from_be_bytes(value.as_slice().try_into() - .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?)) - } - })* - } -} - -intkey_old_de!(for i8, u8, i16, u16, i32, u32, i64, u64, i128, u128); - -#[cfg(test)] -mod test { - use super::*; - use crate::keys_old::IntKeyOld; - - #[test] - fn deserialize_integer_old_works() { - assert_eq!(>::from_slice(&[1]).unwrap(), 1u8); - assert_eq!(>::from_slice(&[127]).unwrap(), 127i8); - assert_eq!(>::from_slice(&[128]).unwrap(), -128i8); - - assert_eq!(>::from_slice(&[1, 0]).unwrap(), 256u16); - assert_eq!(>::from_slice(&[128, 0]).unwrap(), -32768i16); - assert_eq!(>::from_slice(&[127, 255]).unwrap(), 32767i16); - - assert_eq!( - >::from_slice(&[1, 0, 0, 0]).unwrap(), - 16777216u32 - ); - assert_eq!( - >::from_slice(&[128, 0, 0, 0]).unwrap(), - -2147483648i32 - ); - assert_eq!( - >::from_slice(&[127, 255, 255, 255]).unwrap(), - 2147483647i32 - ); - - assert_eq!( - >::from_slice(&[1, 0, 0, 0, 0, 0, 0, 0]).unwrap(), - 72057594037927936u64 - ); - assert_eq!( - >::from_slice(&[128, 0, 0, 0, 0, 0, 0, 0]).unwrap(), - -9223372036854775808i64 - ); - assert_eq!( - >::from_slice(&[127, 255, 255, 255, 255, 255, 255, 255]).unwrap(), - 9223372036854775807i64 - ); - - assert_eq!( - >::from_slice(&[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) - .unwrap(), - 1329227995784915872903807060280344576u128 - ); - assert_eq!( - >::from_slice(&[128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) - .unwrap(), - -170141183460469231731687303715884105728i128 - ); - assert_eq!( - >::from_slice(&[ - 127, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 - ]) - .unwrap(), - 170141183460469231731687303715884105727i128 - ); - assert_eq!( - >::from_slice(&[ - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 - ]) - .unwrap(), - -1i128, - ); - } - - #[test] - fn deserialize_broken_integer_old_errs() { - // One byte less fails - assert!(matches!( - >::from_slice(&[1]).err(), - Some(StdError::GenericErr { .. }) - )); - - // More bytes fails too - assert!(matches!( - >::from_slice(&[1, 2]).err(), - Some(StdError::GenericErr { .. }) - )); - } -} diff --git a/packages/storage-plus/src/int_key.rs b/packages/storage-plus/src/int_key.rs index 7f7713369..0292674a2 100644 --- a/packages/storage-plus/src/int_key.rs +++ b/packages/storage-plus/src/int_key.rs @@ -4,8 +4,7 @@ use std::mem; /// but "sign-flipped" (xored msb) big-endian bytes for signed ints. /// /// So that the representation of signed integers is in the right lexicographical order. -// TODO: Rename to `IntKey` after deprecating current `IntKey` (https://github.com/CosmWasm/cw-plus/issues/570) -pub trait CwIntKey: Sized + Copy { +pub trait IntKey: Sized + Copy { type Buf: AsRef<[u8]> + AsMut<[u8]> + Into> + Default; fn to_cw_bytes(&self) -> Self::Buf; @@ -14,7 +13,7 @@ pub trait CwIntKey: Sized + Copy { macro_rules! cw_uint_keys { (for $($t:ty),+) => { - $(impl CwIntKey for $t { + $(impl IntKey for $t { type Buf = [u8; mem::size_of::<$t>()]; #[inline] @@ -34,7 +33,7 @@ cw_uint_keys!(for u8, u16, u32, u64, u128); macro_rules! cw_int_keys { (for $($t:ty, $ut:ty),+) => { - $(impl CwIntKey for $t { + $(impl IntKey for $t { type Buf = [u8; mem::size_of::<$t>()]; #[inline] diff --git a/packages/storage-plus/src/keys.rs b/packages/storage-plus/src/keys.rs index 9e55bfd5b..cf7eff867 100644 --- a/packages/storage-plus/src/keys.rs +++ b/packages/storage-plus/src/keys.rs @@ -2,7 +2,7 @@ use cosmwasm_std::Addr; use crate::de::KeyDeserialize; use crate::helpers::namespaces_with_key; -use crate::int_key::CwIntKey; +use crate::int_key::IntKey; #[derive(Debug)] pub enum Key<'a> { diff --git a/packages/storage-plus/src/keys_old.rs b/packages/storage-plus/src/keys_old.rs deleted file mode 100644 index 97752ae03..000000000 --- a/packages/storage-plus/src/keys_old.rs +++ /dev/null @@ -1,105 +0,0 @@ -use crate::de::KeyDeserialize; -use crate::keys::Key; -#[cfg(feature = "iterator")] -use crate::{Bound, Bounder}; -use crate::{Endian, Prefixer, PrimaryKey}; -use std::marker::PhantomData; - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct IntKeyOld { - pub wrapped: Vec, - pub data: PhantomData, -} - -impl IntKeyOld { - pub fn new(val: T) -> Self { - IntKeyOld { - wrapped: val.to_be_bytes().into(), - data: PhantomData, - } - } -} - -impl From for IntKeyOld { - fn from(val: T) -> Self { - IntKeyOld::new(val) - } -} - -impl From> for IntKeyOld { - fn from(wrap: Vec) -> Self { - IntKeyOld { - wrapped: wrap, - data: PhantomData, - } - } -} - -impl From> for Vec { - fn from(k: IntKeyOld) -> Vec { - k.wrapped - } -} - -// this auto-implements PrimaryKey for all the IntKeyOld types -impl<'a, T: Endian + Clone> PrimaryKey<'a> for IntKeyOld -where - IntKeyOld: KeyDeserialize, -{ - type Prefix = (); - type SubPrefix = (); - type Suffix = Self; - type SuperSuffix = Self; - - fn key(&self) -> Vec { - self.wrapped.key() - } -} - -// this auto-implements Prefixer for all the IntKey types -impl<'a, T: Endian> Prefixer<'a> for IntKeyOld { - fn prefix(&self) -> Vec { - self.wrapped.prefix() - } -} - -// this auto-implements Bounder for all the IntKey types -#[cfg(feature = "iterator")] -impl<'a, T: Endian> Bounder<'a> for IntKeyOld -where - IntKeyOld: KeyDeserialize, -{ - fn inclusive_bound(self) -> Option> { - Some(Bound::inclusive(self)) - } - - fn exclusive_bound(self) -> Option> { - Some(Bound::exclusive(self)) - } -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn u64key_old_works() { - let k: IntKeyOld = 134u64.into(); - let path = k.key(); - assert_eq!(1, path.len()); - assert_eq!(134u64.to_be_bytes(), path[0].as_ref()); - } - - #[test] - fn i32key_old_works() { - let k: IntKeyOld = 4242i32.into(); - let path = k.key(); - assert_eq!(1, path.len()); - assert_eq!(4242i32.to_be_bytes(), path[0].as_ref()); - - let k: IntKeyOld = IntKeyOld::::from(-4242i32); - let path = k.key(); - assert_eq!(1, path.len()); - assert_eq!((-4242i32).to_be_bytes(), path[0].as_ref()); - } -} diff --git a/packages/storage-plus/src/lib.rs b/packages/storage-plus/src/lib.rs index d6f93efb3..03b2b52d1 100644 --- a/packages/storage-plus/src/lib.rs +++ b/packages/storage-plus/src/lib.rs @@ -1,6 +1,5 @@ mod bound; mod de; -mod de_old; mod endian; mod helpers; mod indexed_map; @@ -10,7 +9,6 @@ mod int_key; mod item; mod iter_helpers; mod keys; -mod keys_old; mod map; mod path; mod prefix; @@ -30,10 +28,9 @@ pub use indexes::Index; pub use indexes::MultiIndex; #[cfg(feature = "iterator")] pub use indexes::UniqueIndex; -pub use int_key::CwIntKey; +pub use int_key::IntKey; pub use item::Item; pub use keys::{Key, Prefixer, PrimaryKey}; -pub use keys_old::IntKeyOld; pub use map::Map; pub use path::Path; #[cfg(feature = "iterator")] diff --git a/packages/storage-plus/src/map.rs b/packages/storage-plus/src/map.rs index 75ad389be..67d243af0 100644 --- a/packages/storage-plus/src/map.rs +++ b/packages/storage-plus/src/map.rs @@ -275,9 +275,7 @@ mod test { #[cfg(feature = "iterator")] use crate::bound::Bounder; - use crate::int_key::CwIntKey; - #[cfg(feature = "iterator")] - use crate::IntKeyOld; + use crate::int_key::IntKey; #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] struct Data { @@ -293,8 +291,6 @@ mod test { #[cfg(feature = "iterator")] const PEOPLE_ID: Map = Map::new("people_id"); #[cfg(feature = "iterator")] - const SIGNED_ID_OLD: Map, Data> = Map::new("signed_id"); - #[cfg(feature = "iterator")] const SIGNED_ID: Map = Map::new("signed_id"); const ALLOWANCE: Map<(&[u8], &[u8]), u64> = Map::new("allow"); @@ -860,76 +856,6 @@ mod test { assert_eq!(all, vec![(50, data3)]); } - #[test] - #[cfg(feature = "iterator")] - fn range_signed_integer_key_migration() { - let mut store = MockStorage::new(); - - // save and load three keys with the old format - let data = Data { - name: "John".to_string(), - age: 32, - }; - SIGNED_ID_OLD - .save(&mut store, IntKeyOld::::from(-1234), &data) - .unwrap(); - - let data2 = Data { - name: "Jim".to_string(), - age: 44, - }; - SIGNED_ID_OLD - .save(&mut store, IntKeyOld::::from(-56), &data2) - .unwrap(); - - let data3 = Data { - name: "Jules".to_string(), - age: 55, - }; - SIGNED_ID_OLD - .save(&mut store, IntKeyOld::::from(50), &data3) - .unwrap(); - - // obtain all current keys - let current = SIGNED_ID_OLD - .range(&store, None, None, Order::Ascending) - .collect::>>() - .unwrap(); - // confirm wrong current order - assert_eq!( - current, - vec![ - (50, data3.clone()), - (-1234, data.clone()), - (-56, data2.clone()) - ] - ); - - // remove old entries - for (k, _) in current.iter() { - SIGNED_ID_OLD.remove(&mut store, (*k).into()); - } - - // confirm map is empty - assert!(SIGNED_ID_OLD - .range(&store, None, None, Order::Ascending) - .next() - .is_none()); - - // save in new format - for (k, v) in current.into_iter() { - SIGNED_ID.save(&mut store, k, &v).unwrap(); - } - - // obtain new keys - let new = SIGNED_ID - .range(&store, None, None, Order::Ascending) - .collect::>>() - .unwrap(); - // confirm new order is right - assert_eq!(new, vec![(-1234, data), (-56, data2), (50, data3)]); - } - #[test] #[cfg(feature = "iterator")] fn range_raw_composite_key() {