From 88debb21901a8ce401e4cdf8ea6ec73c3ccfcc15 Mon Sep 17 00:00:00 2001 From: debris Date: Thu, 24 Dec 2015 00:31:43 +0100 Subject: [PATCH] address to and from h256 --- src/hash.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/hash.rs b/src/hash.rs index e86f8e834..ea041f2ac 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -320,6 +320,26 @@ impl<'a> From<&'a U256> for H256 { } } +impl From 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 { + let mut ret = H256::new(); + ::std::ptr::copy(value.as_ptr(), ret.as_mut_ptr().offset(12), 20); + ret + } + } +} + impl_hash!(H32, 4); impl_hash!(H64, 8); impl_hash!(H128, 16); @@ -382,5 +402,13 @@ mod tests { assert!(my_bloom.contains_bloom(&address.sha3())); assert!(my_bloom.contains_bloom(&topic.sha3())); } + + #[test] + fn from_and_to_address() { + let address = Address::from_str("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap(); + let h = H256::from(address.clone()); + let a = Address::from(h); + assert_eq!(address, a); + } }