1use crate::{
2 decode::Decode,
3 encode::Encode,
4 into_type::IntoType,
5};
6use std::{
7 borrow::Cow,
8 convert::TryInto,
9 mem,
10};
11
12macro_rules! impl_encode_signed {
13 ($ty: ty) => {
14 impl Encode for $ty {
15 fn encode(&self) -> Vec<u8> {
16 let bits = if self.to_be_bytes()[0] & 0x80 == 0x80 {
17 0xff
18 } else {
19 0x00
20 };
21 let mut buf = vec![bits; 32];
22 buf[32 - mem::size_of::<$ty>()..].copy_from_slice(&self.to_be_bytes());
23 buf
24 }
25 }
26 };
27}
28
29macro_rules! impl_encode_unsigned {
30 ($ty: ty) => {
31 impl Encode for $ty {
32 fn encode(&self) -> Vec<u8> {
33 let mut buf = vec![0u8; 32];
34 buf[32 - mem::size_of::<$ty>()..].copy_from_slice(&self.to_be_bytes());
35 buf
36 }
37 }
38 };
39}
40
41impl_encode_signed!(i8);
42impl_encode_unsigned!(u8);
43impl_encode_signed!(i16);
44impl_encode_unsigned!(u16);
45impl_encode_signed!(i32);
46impl_encode_unsigned!(u32);
47impl_encode_signed!(i64);
48impl_encode_unsigned!(u64);
49impl_encode_signed!(i128);
50impl_encode_unsigned!(u128);
51
52impl<'a> Decode<'a> for i8 {
53 fn decode(buf: &'a [u8]) -> Self {
54 buf[31] as i8
55 }
56}
57
58impl<'a> Decode<'a> for u8 {
59 fn decode(buf: &'a [u8]) -> Self {
60 buf[31]
61 }
62}
63
64impl<'a> Decode<'a> for i16 {
65 fn decode(buf: &'a [u8]) -> Self {
66 i16::from_be_bytes(buf[30..32].try_into().unwrap())
67 }
68}
69
70impl<'a> Decode<'a> for u16 {
71 fn decode(buf: &'a [u8]) -> Self {
72 u16::from_be_bytes(buf[30..32].try_into().unwrap())
73 }
74}
75
76impl<'a> Decode<'a> for i32 {
77 fn decode(buf: &'a [u8]) -> Self {
78 i32::from_be_bytes(buf[28..32].try_into().unwrap())
79 }
80}
81
82impl<'a> Decode<'a> for u32 {
83 fn decode(buf: &'a [u8]) -> Self {
84 u32::from_be_bytes(buf[28..32].try_into().unwrap())
85 }
86}
87
88impl<'a> Decode<'a> for i64 {
89 fn decode(buf: &'a [u8]) -> Self {
90 i64::from_be_bytes(buf[24..32].try_into().unwrap())
91 }
92}
93
94impl<'a> Decode<'a> for u64 {
95 fn decode(buf: &'a [u8]) -> Self {
96 u64::from_be_bytes(buf[24..32].try_into().unwrap())
97 }
98}
99
100impl<'a> Decode<'a> for i128 {
101 fn decode(buf: &'a [u8]) -> Self {
102 i128::from_be_bytes(buf[16..32].try_into().unwrap())
103 }
104}
105
106impl<'a> Decode<'a> for u128 {
107 fn decode(buf: &'a [u8]) -> Self {
108 u128::from_be_bytes(buf[16..32].try_into().unwrap())
109 }
110}
111
112pub struct Int24(pub [u8; 3]);
113pub struct Int40(pub [u8; 5]);
114pub struct Int48(pub [u8; 6]);
115pub struct Int56(pub [u8; 7]);
116pub struct Int72(pub [u8; 9]);
117pub struct Int80(pub [u8; 10]);
118pub struct Int88(pub [u8; 11]);
119pub struct Int96(pub [u8; 12]);
120pub struct Int104(pub [u8; 13]);
121pub struct Int112(pub [u8; 14]);
122pub struct Int120(pub [u8; 15]);
123pub struct Int136(pub [u8; 17]);
124pub struct Int144(pub [u8; 18]);
125pub struct Int152(pub [u8; 19]);
126pub struct Int160(pub [u8; 20]);
127pub struct Int168(pub [u8; 21]);
128pub struct Int176(pub [u8; 22]);
129pub struct Int184(pub [u8; 23]);
130pub struct Int192(pub [u8; 24]);
131pub struct Int200(pub [u8; 25]);
132pub struct Int208(pub [u8; 26]);
133pub struct Int216(pub [u8; 27]);
134pub struct Int224(pub [u8; 28]);
135pub struct Int232(pub [u8; 29]);
136pub struct Int240(pub [u8; 30]);
137pub struct Int248(pub [u8; 31]);
138pub struct Int256(pub [u8; 32]);
139
140pub struct Uint24(pub [u8; 3]);
141pub struct Uint40(pub [u8; 5]);
142pub struct Uint48(pub [u8; 6]);
143pub struct Uint56(pub [u8; 7]);
144pub struct Uint72(pub [u8; 9]);
145pub struct Uint80(pub [u8; 10]);
146pub struct Uint88(pub [u8; 11]);
147pub struct Uint96(pub [u8; 12]);
148pub struct Uint104(pub [u8; 13]);
149pub struct Uint112(pub [u8; 14]);
150pub struct Uint120(pub [u8; 15]);
151pub struct Uint136(pub [u8; 17]);
152pub struct Uint144(pub [u8; 18]);
153pub struct Uint152(pub [u8; 19]);
154pub struct Uint160(pub [u8; 20]);
155pub struct Uint168(pub [u8; 21]);
156pub struct Uint176(pub [u8; 22]);
157pub struct Uint184(pub [u8; 23]);
158pub struct Uint192(pub [u8; 24]);
159pub struct Uint200(pub [u8; 25]);
160pub struct Uint208(pub [u8; 26]);
161pub struct Uint216(pub [u8; 27]);
162pub struct Uint224(pub [u8; 28]);
163pub struct Uint232(pub [u8; 29]);
164pub struct Uint240(pub [u8; 30]);
165pub struct Uint248(pub [u8; 31]);
166pub struct Uint256(pub [u8; 32]);
167
168macro_rules! impl_encode_int {
169 ($ty: ident, $expr: expr) => {
170 impl Encode for $ty {
171 fn encode(&self) -> Vec<u8> {
172 let mut value = vec![0u8; 32];
173 value[32 - self.0.len()..32].copy_from_slice(&self.0);
174 value
175 }
176 }
177
178 impl<'a> Decode<'a> for $ty {
179 fn decode(buf: &'a [u8]) -> Self {
180 $ty(buf[0..32].try_into().unwrap())
181 }
182 }
183
184 impl IntoType for $ty {
185 fn into_type() -> Cow<'static, str> {
186 Cow::Borrowed($expr)
187 }
188 }
189 };
190}
191
192impl_encode_int!(Int24, "int24");
193impl_encode_int!(Uint24, "uint24");
194impl_encode_int!(Int40, "int40");
195impl_encode_int!(Uint40, "uint40");
196impl_encode_int!(Int48, "int48");
197impl_encode_int!(Uint48, "uint48");
198impl_encode_int!(Int56, "int56");
199impl_encode_int!(Uint56, "uint56");
200impl_encode_int!(Int72, "int72");
201impl_encode_int!(Uint72, "uint72");
202impl_encode_int!(Int80, "int80");
203impl_encode_int!(Uint80, "uint80");
204impl_encode_int!(Int88, "int88");
205impl_encode_int!(Uint88, "uint88");
206impl_encode_int!(Int96, "int96");
207impl_encode_int!(Uint96, "uint96");
208impl_encode_int!(Int104, "int104");
209impl_encode_int!(Uint104, "uint104");
210impl_encode_int!(Int112, "int112");
211impl_encode_int!(Uint112, "uint112");
212impl_encode_int!(Int120, "int120");
213impl_encode_int!(Uint120, "uint120");
214impl_encode_int!(Int136, "int136");
215impl_encode_int!(Uint136, "uint136");
216impl_encode_int!(Int144, "int144");
217impl_encode_int!(Uint144, "uint144");
218impl_encode_int!(Int152, "int152");
219impl_encode_int!(Uint152, "uint152");
220impl_encode_int!(Int160, "int160");
221impl_encode_int!(Uint160, "uint160");
222impl_encode_int!(Int168, "int168");
223impl_encode_int!(Uint168, "uint168");
224impl_encode_int!(Int176, "int176");
225impl_encode_int!(Uint176, "uint176");
226impl_encode_int!(Int184, "int184");
227impl_encode_int!(Uint184, "uint184");
228impl_encode_int!(Int192, "int192");
229impl_encode_int!(Uint192, "uint192");
230impl_encode_int!(Int200, "int200");
231impl_encode_int!(Uint200, "uint200");
232impl_encode_int!(Int208, "int208");
233impl_encode_int!(Uint208, "uint208");
234impl_encode_int!(Int216, "int216");
235impl_encode_int!(Uint216, "uint216");
236impl_encode_int!(Int224, "int224");
237impl_encode_int!(Uint224, "uint224");
238impl_encode_int!(Int232, "int232");
239impl_encode_int!(Uint232, "uint232");
240impl_encode_int!(Int240, "int240");
241impl_encode_int!(Uint240, "uint240");
242impl_encode_int!(Int248, "int248");
243impl_encode_int!(Uint248, "uint248");
244impl_encode_int!(Int256, "int256");
245impl_encode_int!(Uint256, "uint256");