From dd99ebd85914f61ad16b8504ad854ec96cbe516b Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 13 Jan 2016 18:41:33 +0100 Subject: [PATCH 1/3] From::from(u64) for hashes. --- src/hash.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/hash.rs b/src/hash.rs index 5eb76a05e..67d502633 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -377,6 +377,19 @@ macro_rules! impl_hash { pub fn from_bloomed(b: &T) -> Self where T: FixedHash { b.bloom_part($size) } } + + impl From for $from { + fn from(mut value: u64) -> $from { + let mut ret = $from::new(); + for i in 0..8 { + if i < $size { + ret.0[$size - i - 1] = (value & 0xff) as u8; + value >>= 8; + } + } + ret + } + } } } @@ -387,7 +400,7 @@ impl<'a> From<&'a U256> for H256 { value.to_bytes(&mut ret); ret } - } + } } impl From for Address { From 5cc5e7780ce15be0ca315a1f5f1ca1274668e9c2 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 13 Jan 2016 18:58:57 +0100 Subject: [PATCH 2/3] Tests and an additional From::from for hex string -> hash that defaults to 0s for bad strings. --- src/hash.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/hash.rs b/src/hash.rs index 67d502633..544e82fd2 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -35,6 +35,14 @@ pub trait FixedHash: Sized + BytesConvertable + Populatable { fn is_zero(&self) -> bool; } +fn clean_0x(s: &str) -> &str { + if s.len() >= 2 && &s[0..2] == "0x" { + &s[2..] + } else { + s + } +} + macro_rules! impl_hash { ($from: ident, $size: expr) => { #[derive(Eq)] @@ -390,6 +398,17 @@ macro_rules! impl_hash { ret } } + + impl<'_> From<&'_ str> for $from { + fn from(s: &'_ str) -> $from { + use std::str::FromStr; + if s.len() % 2 == 1 { + $from::from_str(&("0".to_string() + &(clean_0x(s).to_string()))[..]).unwrap_or($from::new()) + } else { + $from::from_str(clean_0x(s)).unwrap_or($from::new()) + } + } + } } } @@ -518,5 +537,19 @@ mod tests { let a = Address::from(h); assert_eq!(address, a); } + + #[test] + fn from_u64() { + assert_eq!(H128::from(0x1234567890abcdef), H128::from_str("00000000000000001234567890abcdef").unwrap()); + assert_eq!(H64::from(0x1234567890abcdef), H64::from_str("1234567890abcdef").unwrap()); + assert_eq!(H32::from(0x1234567890abcdef), H32::from_str("90abcdef").unwrap()); + } + + #[test] + fn from_str() { + assert_eq!(H64::from(0x1234567890abcdef), H64::from("0x1234567890abcdef")); + assert_eq!(H64::from(0x1234567890abcdef), H64::from("1234567890abcdef")); + assert_eq!(H64::from(0x234567890abcdef), H64::from("0x234567890abcdef")); + } } From 3a1d829b1dbff866ba460c1d4e1e740d368f26c1 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 13 Jan 2016 19:00:19 +0100 Subject: [PATCH 3/3] Additional test. --- src/hash.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/hash.rs b/src/hash.rs index 544e82fd2..73ffdd538 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -550,6 +550,8 @@ mod tests { assert_eq!(H64::from(0x1234567890abcdef), H64::from("0x1234567890abcdef")); assert_eq!(H64::from(0x1234567890abcdef), H64::from("1234567890abcdef")); assert_eq!(H64::from(0x234567890abcdef), H64::from("0x234567890abcdef")); + // too short. + assert_eq!(H64::from(0), H64::from("0x34567890abcdef")); } }