Skip to content

Commit

Permalink
Merge pull request #547 from CosmWasm/472-deprecate-int-key-3
Browse files Browse the repository at this point in the history
Deprecate IntKey
  • Loading branch information
ueco-jb authored Nov 15, 2021
2 parents 0a79a5e + 61dbbd1 commit 5b61f15
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 69 deletions.
18 changes: 17 additions & 1 deletion packages/storage-plus/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl KeyDeserialize for &Addr {

macro_rules! integer_de {
(for $($t:ty),+) => {
$(impl KeyDeserialize for IntKey<$t> {
$(impl KeyDeserialize for $t {
type Output = $t;

#[inline(always)]
Expand All @@ -112,6 +112,22 @@ 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<u8>) -> StdResult<Self::Output> {
Ok(<$t>::from_be_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;

Expand Down
18 changes: 10 additions & 8 deletions packages/storage-plus/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use serde::de::DeserializeOwned;
use std::any::type_name;

use crate::keys::Key;

use cosmwasm_std::{
from_slice, to_vec, Addr, Binary, ContractResult, Empty, QuerierWrapper, QueryRequest,
StdError, StdResult, SystemResult, WasmQuery,
Expand All @@ -20,15 +22,15 @@ pub(crate) fn may_deserialize<T: DeserializeOwned>(
value: &Option<Vec<u8>>,
) -> StdResult<Option<T>> {
match value {
Some(vec) => Ok(Some(from_slice(&vec)?)),
Some(vec) => Ok(Some(from_slice(vec)?)),
None => Ok(None),
}
}

/// must_deserialize parses json bytes from storage (Option), returning NotFound error if no data present
pub(crate) fn must_deserialize<T: DeserializeOwned>(value: &Option<Vec<u8>>) -> StdResult<T> {
match value {
Some(vec) => from_slice(&vec),
Some(vec) => from_slice(vec),
None => Err(StdError::not_found(type_name::<T>())),
}
}
Expand All @@ -54,25 +56,25 @@ pub(crate) fn namespaces_with_key(namespaces: &[&[u8]], key: &[u8]) -> Vec<u8> {
/// there are multiple sets we do not want to combine just to call this
pub(crate) fn nested_namespaces_with_key(
top_names: &[&[u8]],
sub_names: &[&[u8]],
sub_names: &[Key],
key: &[u8],
) -> Vec<u8> {
let mut size = key.len();
for &namespace in top_names {
size += namespace.len() + 2;
}
for &namespace in sub_names {
size += namespace.len() + 2;
for namespace in sub_names {
size += namespace.as_ref().len() + 2;
}

let mut out = Vec::with_capacity(size);
for &namespace in top_names {
out.extend_from_slice(&encode_length(namespace));
out.extend_from_slice(namespace);
}
for &namespace in sub_names {
out.extend_from_slice(&encode_length(namespace));
out.extend_from_slice(namespace);
for namespace in sub_names {
out.extend_from_slice(&encode_length(namespace.as_ref()));
out.extend_from_slice(namespace.as_ref());
}
out.extend_from_slice(key);
out
Expand Down
Loading

0 comments on commit 5b61f15

Please sign in to comment.