diff --git a/src/hash.rs b/src/hash.rs index 73ffdd538..f67833072 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -206,7 +206,10 @@ macro_rules! impl_hash { } impl fmt::Display for $from { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - (self as &fmt::Debug).fmt(f) + for i in self.0[0..3].iter() { + try!(write!(f, "{:02x}", i)); + } + write!(f, "…{:02x}", self.0.last().unwrap()) } } @@ -380,7 +383,7 @@ macro_rules! impl_hash { } impl $from { pub fn hex(&self) -> String { - format!("{}", self) + format!("{:?}", self) } pub fn from_bloomed(b: &T) -> Self where T: FixedHash { b.bloom_part($size) } @@ -412,8 +415,18 @@ macro_rules! impl_hash { } } -impl<'a> From<&'a U256> for H256 { - fn from(value: &'a U256) -> H256 { +impl From for H256 { + fn from(value: U256) -> H256 { + unsafe { + let mut ret: H256 = ::std::mem::uninitialized(); + value.to_bytes(&mut ret); + ret + } + } +} + +impl<'_> From<&'_ U256> for H256 { + fn from(value: &'_ U256) -> H256 { unsafe { let mut ret: H256 = ::std::mem::uninitialized(); value.to_bytes(&mut ret); @@ -431,7 +444,17 @@ impl From for Address { } } } - +/* +impl<'_> From<&'_ H256> for Address { + fn from(value: &'_ H256) -> Address { + unsafe { + let mut ret: Address = ::std::mem::uninitialized(); + ::std::ptr::copy(value.as_ptr().offset(12), ret.as_mut_ptr(), 20); + ret + } + } +} +*/ impl From
for H256 { fn from(value: Address) -> H256 { unsafe { @@ -442,6 +465,16 @@ impl From
for H256 { } } +impl<'_> From<&'_ Address> for H256 { + fn from(value: &'_ Address) -> H256 { + unsafe { + let mut ret = H256::new(); + ::std::ptr::copy(value.as_ptr(), ret.as_mut_ptr().offset(12), 20); + ret + } + } +} + pub fn h256_from_hex(s: &str) -> H256 { use std::str::FromStr; H256::from_str(s).unwrap() @@ -487,7 +520,7 @@ mod tests { fn hash() { let h = H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]); assert_eq!(H64::from_str("0123456789abcdef").unwrap(), h); - assert_eq!(format!("{}", h), "0123456789abcdef"); + assert_eq!(format!("{}", h), "012345…ef"); assert_eq!(format!("{:?}", h), "0123456789abcdef"); assert_eq!(h.hex(), "0123456789abcdef"); assert!(h == h); diff --git a/src/json_aid.rs b/src/json_aid.rs index d015bb13c..d065352af 100644 --- a/src/json_aid.rs +++ b/src/json_aid.rs @@ -30,6 +30,10 @@ pub fn h256_from_json(json: &Json) -> H256 { } } +pub fn vec_h256_from_json(json: &Json) -> Vec { + json.as_array().unwrap().iter().map(&h256_from_json).collect() +} + pub fn u256_from_str(s: &str) -> U256 { if s.len() >= 2 && &s[0..2] == "0x" { U256::from_str(&s[2..]).unwrap_or(U256::from(0))