Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate IntKey #547

Merged
merged 16 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, cool.. just started commenting how we need both. Then see how we do.

Nice!

(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)?)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think & used to be required by the compiler.
But now it is smarter?

Any idea when this changed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea. One way to check it could be compiling with different versions, and see in which one this started to work. The match is producing a ref, and that ref is being converted to slice without any help. Not sure this wasn't supported earlier...

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