From 46810e961c95726ef52b52fb0b41c1828a5d09c1 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 14 Jan 2016 01:27:02 +0100 Subject: [PATCH] Broken casting. --- src/hash.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/hash.rs b/src/hash.rs index 73ffdd538..de63ab061 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -412,8 +412,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); @@ -432,6 +442,16 @@ 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 +462,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()