Merge branch 'master' of github.com:gavofyork/ethcore-util into io

This commit is contained in:
arkpar 2016-01-14 16:52:18 +01:00
commit d971abf1da
3 changed files with 45 additions and 8 deletions

View File

@ -206,7 +206,10 @@ macro_rules! impl_hash {
} }
impl fmt::Display for $from { impl fmt::Display for $from {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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 { impl $from {
pub fn hex(&self) -> String { pub fn hex(&self) -> String {
format!("{}", self) format!("{:?}", self)
} }
pub fn from_bloomed<T>(b: &T) -> Self where T: FixedHash { b.bloom_part($size) } pub fn from_bloomed<T>(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 { impl From<U256> for H256 {
fn from(value: &'a U256) -> 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 { unsafe {
let mut ret: H256 = ::std::mem::uninitialized(); let mut ret: H256 = ::std::mem::uninitialized();
value.to_bytes(&mut ret); value.to_bytes(&mut ret);
@ -431,7 +444,17 @@ impl From<H256> 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<Address> for H256 { impl From<Address> for H256 {
fn from(value: Address) -> H256 { fn from(value: Address) -> H256 {
unsafe { unsafe {
@ -442,6 +465,16 @@ impl From<Address> 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 { pub fn h256_from_hex(s: &str) -> H256 {
use std::str::FromStr; use std::str::FromStr;
H256::from_str(s).unwrap() H256::from_str(s).unwrap()
@ -487,7 +520,7 @@ mod tests {
fn hash() { fn hash() {
let h = H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]); let h = H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]);
assert_eq!(H64::from_str("0123456789abcdef").unwrap(), h); assert_eq!(H64::from_str("0123456789abcdef").unwrap(), h);
assert_eq!(format!("{}", h), "0123456789abcdef"); assert_eq!(format!("{}", h), "012345ef");
assert_eq!(format!("{:?}", h), "0123456789abcdef"); assert_eq!(format!("{:?}", h), "0123456789abcdef");
assert_eq!(h.hex(), "0123456789abcdef"); assert_eq!(h.hex(), "0123456789abcdef");
assert!(h == h); assert!(h == h);

View File

@ -30,6 +30,10 @@ pub fn h256_from_json(json: &Json) -> H256 {
} }
} }
pub fn vec_h256_from_json(json: &Json) -> Vec<H256> {
json.as_array().unwrap().iter().map(&h256_from_json).collect()
}
pub fn u256_from_str(s: &str) -> U256 { pub fn u256_from_str(s: &str) -> U256 {
if s.len() >= 2 && &s[0..2] == "0x" { if s.len() >= 2 && &s[0..2] == "0x" {
U256::from_str(&s[2..]).unwrap_or(U256::from(0)) U256::from_str(&s[2..]).unwrap_or(U256::from(0))

View File

@ -71,7 +71,7 @@ macro_rules! construct_uint {
pub fn as_u32(&self) -> u32 { pub fn as_u32(&self) -> u32 {
let &$name(ref arr) = self; let &$name(ref arr) = self;
if (arr[0] & (0xffffffffu64 << 32)) != 0 { if (arr[0] & (0xffffffffu64 << 32)) != 0 {
panic!("Intger overflow when casting U256") panic!("Integer overflow when casting U256")
} }
self.as_u64() as u32 self.as_u64() as u32
} }
@ -82,7 +82,7 @@ macro_rules! construct_uint {
let &$name(ref arr) = self; let &$name(ref arr) = self;
for i in 1..$n_words { for i in 1..$n_words {
if arr[i] != 0 { if arr[i] != 0 {
panic!("Intger overflow when casting U256") panic!("Integer overflow when casting U256")
} }
} }
arr[0] arr[0]