-
Notifications
You must be signed in to change notification settings - Fork 358
/
Copy pathint_key.rs
131 lines (113 loc) · 3.75 KB
/
int_key.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::mem;
/// Our int keys are simply the big-endian representation bytes for unsigned ints,
/// 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 {
type Buf: AsRef<[u8]> + AsMut<[u8]> + Into<Vec<u8>> + Default;
fn to_cw_bytes(&self) -> Self::Buf;
fn from_cw_bytes(bytes: Self::Buf) -> Self;
}
macro_rules! cw_uint_keys {
(for $($t:ty),+) => {
$(impl CwIntKey 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::from_be_bytes(bytes)
}
})*
}
}
cw_uint_keys!(for u8, u16, u32, u64, u128);
macro_rules! cw_int_keys {
(for $($t:ty, $ut:ty),+) => {
$(impl CwIntKey for $t {
type Buf = [u8; mem::size_of::<$t>()];
#[inline]
fn to_cw_bytes(&self) -> Self::Buf {
(*self as $ut ^ <$t>::MIN as $ut).to_be_bytes()
}
#[inline]
fn from_cw_bytes(bytes: Self::Buf) -> Self {
(Self::from_be_bytes(bytes) as $ut ^ <$t>::MIN as $ut) as _
}
})*
}
}
cw_int_keys!(for i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);
#[cfg(test)]
mod test {
use super::*;
#[test]
fn x8_int_key_works() {
assert_eq!(0x42u8.to_cw_bytes(), [0x42]);
assert_eq!(0x42i8.to_cw_bytes(), [0xc2]);
assert_eq!((-0x3ei8).to_cw_bytes(), [0x42]);
}
#[test]
fn x16_int_key_works() {
assert_eq!(0x4243u16.to_cw_bytes(), [0x42, 0x43]);
assert_eq!(0x4243i16.to_cw_bytes(), [0xc2, 0x43]);
assert_eq!((-0x3dbdi16).to_cw_bytes(), [0x42, 0x43]);
}
#[test]
fn x32_int_key_works() {
assert_eq!(0x424344u32.to_cw_bytes(), [0x00, 0x42, 0x43, 0x44]);
assert_eq!(0x424344i32.to_cw_bytes(), [0x80, 0x42, 0x43, 0x44]);
assert_eq!((-0x7fbdbcbci32).to_cw_bytes(), [0x00, 0x42, 0x43, 0x44]);
}
#[test]
fn x64_int_key_works() {
assert_eq!(
0x42434445u64.to_cw_bytes(),
[0x00, 0x00, 0x00, 0x00, 0x42, 0x43, 0x44, 0x45]
);
assert_eq!(
0x42434445i64.to_cw_bytes(),
[0x80, 0x00, 0x00, 0x00, 0x42, 0x43, 0x44, 0x45]
);
assert_eq!(
(-0x7fffffffbdbcbbbbi64).to_cw_bytes(),
[0x00, 0x00, 0x00, 0x00, 0x42, 0x43, 0x44, 0x45]
);
}
#[test]
fn x128_int_key_works() {
assert_eq!(
0x4243444546u128.to_cw_bytes(),
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x43, 0x44,
0x45, 0x46
]
);
assert_eq!(
0x4243444546i128.to_cw_bytes(),
[
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x43, 0x44,
0x45, 0x46
]
);
assert_eq!(
(-0x7fffffffffffffffffffffbdbcbbbabai128).to_cw_bytes(),
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x43, 0x44,
0x45, 0x46
]
);
}
#[test]
fn unsigned_int_key_order() {
assert!(0u32.to_cw_bytes() < 652u32.to_cw_bytes());
}
#[test]
fn signed_int_key_order() {
assert!((-321i32).to_cw_bytes() < 0i32.to_cw_bytes());
assert!(0i32.to_cw_bytes() < 652i32.to_cw_bytes());
}
}