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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
use {U64, U128, U256, U512}; #[cfg(feature="serialize")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; #[cfg(feature="serialize")] use s_types_serialize; macro_rules! impl_serde { ($name: ident, $len: expr) => { #[cfg(feature="serialize")] impl Serialize for $name { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer { let mut slice = [0u8; 2 + 2 * $len]; s_types_serialize::serialize(&mut slice, &self.0, serializer) } } #[cfg(feature="serialize")] impl<'de> Deserialize<'de> for $name { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> { let mut bytes = [0u8; $len]; s_types_serialize::deserialize_check_len(deserializer, s_types_serialize::ExpectedLen::Exact(&mut bytes))?; Ok($name(bytes)) } } } } macro_rules! impl_uint_conversions { ($hash: ident, $uint: ident) => { impl From<$uint> for $hash { fn from(value: $uint) -> Self { let mut ret = $hash::new(); value.to_big_endian(&mut ret); ret } } impl<'a> From<&'a $uint> for $hash { fn from(value: &'a $uint) -> Self { let mut ret = $hash::new(); value.to_big_endian(&mut ret); ret } } impl From<$hash> for $uint { fn from(value: $hash) -> Self { Self::from(&value) } } impl<'a> From<&'a $hash> for $uint { fn from(value: &'a $hash) -> Self { Self::from(value.as_ref() as &[u8]) } } } } impl_serde!(H32, 4); impl_serde!(H64, 8); impl_serde!(H128, 16); impl_serde!(H160, 20); impl_serde!(H256, 32); impl_serde!(H264, 33); impl_serde!(H512, 64); impl_serde!(H520, 65); construct_hash!(H32, 4); construct_hash!(H64, 8); construct_hash!(H128, 16); construct_hash!(H160, 20); construct_hash!(H256, 32); construct_hash!(H264, 33); construct_hash!(H512, 64); construct_hash!(H520, 65); impl_uint_conversions!(H64, U64); impl_uint_conversions!(H128, U128); impl_uint_conversions!(H256, U256); impl_uint_conversions!(H512, U512); #[deprecated] impl From<H256> for H160 { fn from(value: H256) -> H160 { let mut ret = H160::new(); ret.0.copy_from_slice(&value[12..32]); ret } } #[deprecated] impl From<H256> for H64 { fn from(value: H256) -> H64 { let mut ret = H64::new(); ret.0.copy_from_slice(&value[20..28]); ret } } impl From<H160> for H256 { fn from(value: H160) -> H256 { let mut ret = H256::new(); ret.0[12..32].copy_from_slice(&value); ret } } impl<'a> From<&'a H160> for H256 { fn from(value: &'a H160) -> H256 { let mut ret = H256::new(); ret.0[12..32].copy_from_slice(value); ret } } #[cfg(test)] mod tests { use super::{H160, H256}; use serde_json as ser; #[test] fn test_serialize_h160() { let tests = vec![ (H160::from(0), "0x0000000000000000000000000000000000000000"), (H160::from(2), "0x0000000000000000000000000000000000000002"), (H160::from(15), "0x000000000000000000000000000000000000000f"), (H160::from(16), "0x0000000000000000000000000000000000000010"), (H160::from(1_000), "0x00000000000000000000000000000000000003e8"), (H160::from(100_000), "0x00000000000000000000000000000000000186a0"), (H160::from(u64::max_value()), "0x000000000000000000000000ffffffffffffffff"), ]; for (number, expected) in tests { assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap()); assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap()); } } #[test] fn test_serialize_h256() { let tests = vec![ (H256::from(0), "0x0000000000000000000000000000000000000000000000000000000000000000"), (H256::from(2), "0x0000000000000000000000000000000000000000000000000000000000000002"), (H256::from(15), "0x000000000000000000000000000000000000000000000000000000000000000f"), (H256::from(16), "0x0000000000000000000000000000000000000000000000000000000000000010"), (H256::from(1_000), "0x00000000000000000000000000000000000000000000000000000000000003e8"), (H256::from(100_000), "0x00000000000000000000000000000000000000000000000000000000000186a0"), (H256::from(u64::max_value()), "0x000000000000000000000000000000000000000000000000ffffffffffffffff"), ]; for (number, expected) in tests { assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap()); assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap()); } } #[test] fn test_serialize_invalid() { assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data()); assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000g\"").unwrap_err().is_data()); assert!(ser::from_str::<H256>("\"0x00000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data()); assert!(ser::from_str::<H256>("\"\"").unwrap_err().is_data()); assert!(ser::from_str::<H256>("\"0\"").unwrap_err().is_data()); assert!(ser::from_str::<H256>("\"10\"").unwrap_err().is_data()); } }