From 5fd5c461b3748dae9247de0cbc995c0b3636fc2a Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 13 Jan 2016 23:19:08 +0100 Subject: [PATCH 1/5] u256_from_ hex -> str. --- src/json_aid.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/json_aid.rs b/src/json_aid.rs index a382c61ec..aca998a14 100644 --- a/src/json_aid.rs +++ b/src/json_aid.rs @@ -35,7 +35,7 @@ pub fn h256_from_json(json: &Json) -> H256 { } } -pub fn u256_from_hex(s: &str) -> U256 { +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)) } else { @@ -44,7 +44,7 @@ pub fn u256_from_hex(s: &str) -> U256 { } pub fn u256_from_json(json: &Json) -> U256 { - u256_from_hex(json.as_string().unwrap_or("")) + u256_from_str(json.as_string().unwrap_or("")) } pub fn usize_from_json(json: &Json) -> usize { From 46810e961c95726ef52b52fb0b41c1828a5d09c1 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 14 Jan 2016 01:27:02 +0100 Subject: [PATCH 2/5] 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() From 787a119f120e59277f94d6c16dc03cda79db49db Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 14 Jan 2016 01:28:09 +0100 Subject: [PATCH 3/5] Workaround. --- src/hash.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hash.rs b/src/hash.rs index de63ab061..5d8e8a0fe 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -441,7 +441,7 @@ impl From for Address { } } } - +/* impl<'_> From<&'_ H256> for Address { fn from(value: &'_ H256) -> Address { unsafe { @@ -451,7 +451,7 @@ impl<'_> From<&'_ H256> for Address { } } } - +*/ impl From
for H256 { fn from(value: Address) -> H256 { unsafe { From 1c89b9732da7f3cb1f47f316d7e49f0fb72ba71d Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 14 Jan 2016 12:27:41 +0100 Subject: [PATCH 4/5] Nicer display output for hash types. --- src/hash.rs | 5 ++++- src/json_aid.rs | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/hash.rs b/src/hash.rs index 5d8e8a0fe..0d26d74e6 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()) } } diff --git a/src/json_aid.rs b/src/json_aid.rs index aca998a14..a50c84c5e 100644 --- a/src/json_aid.rs +++ b/src/json_aid.rs @@ -35,6 +35,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)) From d63db8523cd59ca5413046a44fc432a7da34a61b Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 14 Jan 2016 13:10:18 +0100 Subject: [PATCH 5/5] Fix test, revert fn hex() behaviour. --- src/hash.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hash.rs b/src/hash.rs index 0d26d74e6..f67833072 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -383,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) } @@ -520,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);