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

Make Uint64, Uint128, Int64, Int128 usable as keys #86

Merged
merged 2 commits into from
Sep 24, 2024
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
4 changes: 2 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::array::TryFromSliceError;
use std::convert::TryInto;

use cosmwasm_std::{Addr, StdError, StdResult};
use cosmwasm_std::{Addr, Int128, Int64, StdError, StdResult, Uint128, Uint64};

use crate::int_key::IntKey;

Expand Down Expand Up @@ -156,7 +156,7 @@ macro_rules! integer_de {
}
}

integer_de!(for i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);
integer_de!(for i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, Uint64, Uint128, Int64, Int128);

fn parse_length(value: &[u8]) -> StdResult<usize> {
Ok(u16::from_be_bytes(
Expand Down
44 changes: 44 additions & 0 deletions src/int_key.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::mem;

use cosmwasm_std::{Int128, Int64, Uint128, Uint64};

/// Our int keys are simply the big-endian representation bytes for unsigned ints,
/// but "sign-flipped" (xored msb) big-endian bytes for signed ints.
///
Expand Down Expand Up @@ -51,6 +53,48 @@ macro_rules! cw_int_keys {

cw_int_keys!(for i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);

macro_rules! cw_uint_std_keys {
(for $($t:ty),+) => {
$(impl IntKey for $t {
type Buf = [u8; mem::size_of::<$t>()];

#[inline]
fn to_cw_bytes(&self) -> Self::Buf {
self.to_be_bytes()
}

#[inline]
fn from_cw_bytes(bytes: Self::Buf) -> Self {
Self::new(IntKey::from_cw_bytes(bytes))
}
})*
}
}

cw_uint_std_keys!(for Uint64, Uint128);

macro_rules! cw_int_std_keys {
(for $($t:ty),+) => {
$(impl IntKey for $t {
type Buf = [u8; mem::size_of::<$t>()];

#[inline]
fn to_cw_bytes(&self) -> Self::Buf {
let mut bytes = self.to_be_bytes();
bytes[0] ^= 0x80;
bytes
}

#[inline]
fn from_cw_bytes(bytes: Self::Buf) -> Self {
Self::new(IntKey::from_cw_bytes(bytes))
webmaster128 marked this conversation as resolved.
Show resolved Hide resolved
}
})*
}
}

cw_int_std_keys!(for Int64, Int128);

#[cfg(test)]
mod test {
use super::*;
Expand Down
38 changes: 35 additions & 3 deletions src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmwasm_std::{storage_keys::namespace_with_key, Addr};
use cosmwasm_std::{Int128, Int64, Uint128, Uint64};

use crate::de::KeyDeserialize;
use crate::int_key::IntKey;
Expand Down Expand Up @@ -304,8 +305,7 @@ macro_rules! integer_key {
}
}

integer_key!(for i8, Val8, u8, Val8, i16, Val16, u16, Val16, i32, Val32, u32, Val32, i64, Val64, u64, Val64, i128, Val128, u128, Val128);

integer_key!(for i8, Val8, u8, Val8, i16, Val16, u16, Val16, i32, Val32, u32, Val32, i64, Val64, u64, Val64, i128, Val128, u128, Val128, Uint64, Val64, Uint128, Val128, Int64, Val64, Int128, Val128);
macro_rules! integer_prefix {
(for $($t:ty, $v:tt),+) => {
$(impl<'a> Prefixer<'a> for $t {
Expand All @@ -316,7 +316,7 @@ macro_rules! integer_prefix {
}
}

integer_prefix!(for i8, Val8, u8, Val8, i16, Val16, u16, Val16, i32, Val32, u32, Val32, i64, Val64, u64, Val64, i128, Val128, u128, Val128);
integer_prefix!(for i8, Val8, u8, Val8, i16, Val16, u16, Val16, i32, Val32, u32, Val32, i64, Val64, u64, Val64, i128, Val128, u128, Val128, Uint64, Val64, Uint128, Val128, Int64, Val64, Int128, Val128);

#[cfg(test)]
mod test {
Expand Down Expand Up @@ -389,6 +389,38 @@ mod test {
assert_eq!(4242i128.to_cw_bytes(), path[0].as_ref());
}

#[test]
fn std_uint64_key_works() {
let k: Uint64 = Uint64::from(4242u64);
let path = k.key();
assert_eq!(1, path.len());
assert_eq!(4242u64.to_cw_bytes(), path[0].as_ref());
}

#[test]
fn std_uint128_key_works() {
let k: Uint128 = Uint128::from(4242u128);
let path = k.key();
assert_eq!(1, path.len());
assert_eq!(4242u128.to_cw_bytes(), path[0].as_ref());
}

#[test]
fn std_int64_key_works() {
let k: Int64 = Int64::from(-4242i64);
let path = k.key();
assert_eq!(1, path.len());
assert_eq!((-4242i64).to_cw_bytes(), path[0].as_ref());
}

#[test]
fn std_int128_key_works() {
let k: Int128 = Int128::from(-4242i128);
let path = k.key();
assert_eq!(1, path.len());
assert_eq!((-4242i128).to_cw_bytes(), path[0].as_ref());
}

#[test]
fn str_key_works() {
type K<'a> = &'a str;
Expand Down