diff --git a/packages/storage-plus/src/de.rs b/packages/storage-plus/src/de.rs index 60f6fcb57..c1b13889f 100644 --- a/packages/storage-plus/src/de.rs +++ b/packages/storage-plus/src/de.rs @@ -1,13 +1,9 @@ -// TODO: Remove along with IntKey -#![allow(deprecated)] - use std::array::TryFromSliceError; use std::convert::TryInto; use cosmwasm_std::{Addr, StdError, StdResult}; use crate::int_key::CwIntKey; -use crate::keys::{IntKey, TimestampKey}; pub trait KeyDeserialize { type Output: Sized; @@ -116,31 +112,6 @@ macro_rules! integer_de { integer_de!(for i8, u8, i16, u16, i32, u32, i64, u64, i128, u128); -macro_rules! intkey_de { - (for $($t:ty),+) => { - $(impl KeyDeserialize for IntKey<$t> { - type Output = $t; - - #[inline(always)] - fn from_vec(value: Vec) -> StdResult { - Ok(<$t>::from_cw_bytes(value.as_slice().try_into() - .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?)) - } - })* - } -} - -intkey_de!(for i8, u8, i16, u16, i32, u32, i64, u64, i128, u128); - -impl KeyDeserialize for TimestampKey { - type Output = u64; - - #[inline(always)] - fn from_vec(value: Vec) -> StdResult { - >::from_vec(value) - } -} - fn parse_length(value: &[u8]) -> StdResult { Ok(u16::from_be_bytes( value @@ -183,7 +154,7 @@ impl KeyDeserialize for #[cfg(test)] mod test { use super::*; - use crate::{PrimaryKey, U32Key}; + use crate::PrimaryKey; const BYTES: &[u8] = b"Hello"; const STRING: &str = "Hello"; @@ -278,96 +249,6 @@ mod test { ); } - #[test] - fn deserialize_integer_works() { - assert_eq!(>::from_slice(&[1]).unwrap(), 1u8); - assert_eq!(>::from_slice(&[127]).unwrap(), -1i8); - assert_eq!(>::from_slice(&[128]).unwrap(), 0i8); - - assert_eq!(>::from_slice(&[1, 0]).unwrap(), 256u16); - assert_eq!(>::from_slice(&[128, 0]).unwrap(), 0i16); - assert_eq!(>::from_slice(&[127, 255]).unwrap(), -1i16); - - assert_eq!( - >::from_slice(&[1, 0, 0, 0]).unwrap(), - 16777216u32 - ); - assert_eq!(>::from_slice(&[128, 0, 0, 0]).unwrap(), 0i32); - assert_eq!( - >::from_slice(&[127, 255, 255, 255]).unwrap(), - -1i32 - ); - - 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(), - 0i64 - ); - assert_eq!( - >::from_slice(&[127, 255, 255, 255, 255, 255, 255, 255]).unwrap(), - -1i64 - ); - - 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(), - 0i128 - ); - assert_eq!( - >::from_slice(&[ - 127, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 - ]) - .unwrap(), - -1i128 - ); - assert_eq!( - >::from_slice(&[ - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 - ]) - .unwrap(), - 170141183460469231731687303715884105727i128, - ); - } - - #[test] - fn deserialize_broken_integer_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 { .. }) - )); - } - - #[test] - fn deserialize_timestamp_works() { - assert_eq!( - ::from_slice(&[1, 0, 0, 0, 0, 0, 0, 0]).unwrap(), - 72057594037927936 - ); - } - - #[test] - fn deserialize_broken_timestamp_errs() { - // More bytes fails - assert!(matches!( - ::from_slice(&[1, 2, 3, 4, 5, 6, 7, 8, 9]).err(), - Some(StdError::GenericErr { .. }) - )); - } - #[test] fn deserialize_tuple_works() { assert_eq!( @@ -379,10 +260,8 @@ mod test { #[test] fn deserialize_triple_works() { assert_eq!( - <(&[u8], U32Key, &str)>::from_slice( - (BYTES, U32Key::new(1234), STRING).joined_key().as_slice() - ) - .unwrap(), + <(&[u8], u32, &str)>::from_slice((BYTES, 1234u32, STRING).joined_key().as_slice()) + .unwrap(), (BYTES.to_vec(), 1234, STRING.to_string()) ); } diff --git a/packages/storage-plus/src/keys.rs b/packages/storage-plus/src/keys.rs index 63bedba2e..e4240abb1 100644 --- a/packages/storage-plus/src/keys.rs +++ b/packages/storage-plus/src/keys.rs @@ -1,13 +1,8 @@ -// TODO: Remove along with IntKey -#![allow(deprecated)] - -use cosmwasm_std::{Addr, Timestamp}; -use std::marker::PhantomData; +use cosmwasm_std::Addr; use crate::de::KeyDeserialize; use crate::helpers::namespaces_with_key; use crate::int_key::CwIntKey; -use crate::Endian; #[derive(Debug)] pub enum Key<'a> { @@ -305,153 +300,10 @@ macro_rules! integer_prefix { integer_prefix!(for i8, Val8, u8, Val8, i16, Val16, u16, Val16, i32, Val32, u32, Val32, i64, Val64, u64, Val64); -// this auto-implements PrimaryKey for all the IntKey types -impl<'a, T: Endian + Clone> PrimaryKey<'a> for IntKey -where - IntKey: 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 IntKey { - fn prefix(&self) -> Vec { - self.wrapped.prefix() - } -} - -#[deprecated(note = "It is suggested to use `u8` as key type instead of the `U8Key` wrapper")] -pub type U8Key = IntKey; -#[deprecated(note = "It is suggested to use `u16` as key type instead of the `U16Key` wrapper")] -pub type U16Key = IntKey; -#[deprecated(note = "It is suggested to use `u32` as key type instead of the `U32Key` wrapper")] -pub type U32Key = IntKey; -#[deprecated(note = "It is suggested to use `u64` as key type instead of the `U64Key` wrapper")] -pub type U64Key = IntKey; -#[deprecated(note = "Consider using 64-bit keys instead of the `U128Key` wrapper")] -pub type U128Key = IntKey; - -#[deprecated(note = "It is suggested to use `i8` as key type instead of the `I8Key` wrapper")] -pub type I8Key = IntKey; -#[deprecated(note = "It is suggested to use `i16` as key type instead of the `I16Key` wrapper")] -pub type I16Key = IntKey; -#[deprecated(note = "It is suggested to use `i32` as key type instead of the `I32Key` wrapper")] -pub type I32Key = IntKey; -#[deprecated(note = "It is suggested to use `i64` as key type instead of the `I64Key` wrapper")] -pub type I64Key = IntKey; -#[deprecated(note = "Consider using 64-bit keys instead of the `I128Key` wrapper")] -pub type I128Key = IntKey; - -/// It will cast one-particular int type into a Key via Vec, ensuring you don't mix up u32 and u64 -/// You can use new or the from/into pair to build a key from an int: -/// -/// let k = U64Key::new(12345); -/// let k = U32Key::from(12345); -/// let k: U16Key = 12345.into(); -#[deprecated(note = "It is suggested to use naked int types instead of IntKey wrapper")] -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct IntKey { - pub wrapped: Vec, - pub data: PhantomData, -} - -impl IntKey { - pub fn new(val: T) -> Self { - IntKey { - wrapped: val.to_cw_bytes().into(), - data: PhantomData, - } - } -} - -impl From for IntKey { - fn from(val: T) -> Self { - IntKey::new(val) - } -} - -impl From> for IntKey { - fn from(wrap: Vec) -> Self { - // TODO: Consider properly handling case, when `wrap` has length not conforming for the - // wrapped integer type. - IntKey { - wrapped: wrap, - data: PhantomData, - } - } -} - -impl From> for Vec { - fn from(k: IntKey) -> Vec { - k.wrapped - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct TimestampKey(U64Key); - -impl TimestampKey { - pub fn new(ts: Timestamp) -> Self { - Self(ts.nanos().into()) - } -} - -impl<'a> PrimaryKey<'a> for TimestampKey { - type Prefix = (); - type SubPrefix = (); - type Suffix = Self; - type SuperSuffix = Self; - - fn key(&self) -> Vec { - self.0.key() - } -} - -impl<'a> Prefixer<'a> for TimestampKey { - fn prefix(&self) -> Vec { - self.0.prefix() - } -} - -impl From> for TimestampKey { - fn from(val: Vec) -> Self { - Self(val.into()) - } -} - -impl From for TimestampKey { - fn from(val: Timestamp) -> Self { - Self::new(val) - } -} - #[cfg(test)] mod test { use super::*; - #[test] - fn u64key_works() { - let k: U64Key = 134u64.into(); - let path = k.key(); - assert_eq!(1, path.len()); - assert_eq!(134u64.to_cw_bytes(), path[0].as_ref()); - } - - #[test] - fn u32key_works() { - let k: U32Key = 4242u32.into(); - let path = k.key(); - assert_eq!(1, path.len()); - assert_eq!(4242u32.to_cw_bytes(), path[0].as_ref()); - } - #[test] fn naked_8key_works() { let k: u8 = 42u8; @@ -549,22 +401,9 @@ mod test { assert_eq!(path, vec!["foo".as_bytes(), b"bar"],); } - #[test] - fn composite_int_key() { - // Note we don't spec the int types (u32, u64) on the right, - // just the keys they convert into - let k: (U32Key, U64Key) = (123.into(), 87654.into()); - let path = k.key(); - assert_eq!(2, path.len()); - assert_eq!(4, path[0].as_ref().len()); - assert_eq!(8, path[1].as_ref().len()); - assert_eq!(path[0].as_ref(), 123u32.to_cw_bytes()); - assert_eq!(path[1].as_ref(), 87654u64.to_cw_bytes()); - } - #[test] fn naked_composite_int_key() { - let k: (u32, U64Key) = (123, 87654.into()); + let k: (u32, u64) = (123, 87654); let path = k.key(); assert_eq!(2, path.len()); assert_eq!(4, path[0].as_ref().len()); @@ -589,35 +428,6 @@ mod test { assert_eq!(dir, vec![first, b"bar"]); } - #[test] - fn proper_prefixes() { - let simple: &str = "hello"; - assert_eq!(simple.prefix().len(), 1); - assert_eq!(simple.prefix()[0].as_ref(), b"hello"); - - let pair: (U32Key, &[u8]) = (12345.into(), b"random"); - let one: Vec = vec![0, 0, 48, 57]; - let two: Vec = b"random".to_vec(); - assert_eq!(pair.prefix(), vec![one.as_slice(), two.as_slice()]); - - let triple: (&str, U32Key, &[u8]) = ("begin", 12345.into(), b"end"); - let one: Vec = b"begin".to_vec(); - let two: Vec = vec![0, 0, 48, 57]; - let three: Vec = b"end".to_vec(); - assert_eq!( - triple.prefix(), - vec![one.as_slice(), two.as_slice(), three.as_slice()] - ); - - // same works with owned variants (&str -> String, &[u8] -> Vec) - let owned_triple: (String, U32Key, Vec) = - ("begin".to_string(), 12345.into(), b"end".to_vec()); - assert_eq!( - owned_triple.prefix(), - vec![one.as_slice(), two.as_slice(), three.as_slice()] - ); - } - #[test] fn naked_8bit_prefixes() { let pair: (u8, &[u8]) = (123, b"random"); diff --git a/packages/storage-plus/src/lib.rs b/packages/storage-plus/src/lib.rs index 0bcbafce0..ff8e508b9 100644 --- a/packages/storage-plus/src/lib.rs +++ b/packages/storage-plus/src/lib.rs @@ -26,14 +26,9 @@ pub use indexes::MultiIndex; pub use indexes::UniqueIndex; #[cfg(feature = "iterator")] pub use indexes::{index_string, index_string_tuple, index_triple, index_tuple, Index}; -pub use item::Item; -// TODO: Remove along with `IntKey` -#[allow(deprecated)] -pub use keys::{I128Key, I16Key, I32Key, I64Key, I8Key}; -// TODO: Remove along with `IntKey` pub use int_key::CwIntKey; -#[allow(deprecated)] -pub use keys::{Key, Prefixer, PrimaryKey, U128Key, U16Key, U32Key, U64Key, U8Key}; +pub use item::Item; +pub use keys::{Key, Prefixer, PrimaryKey}; pub use keys_old::IntKeyOld; pub use map::Map; pub use path::Path;