Merge pull request #206 from ethcore/ark

U256<->H256 Conversion
This commit is contained in:
Gav Wood 2016-01-26 12:09:01 +01:00
commit 537cc61cd6

View File

@ -469,6 +469,18 @@ impl<'_> From<&'_ U256> for H256 {
} }
} }
impl From<H256> for U256 {
fn from(value: H256) -> U256 {
U256::from(value.bytes())
}
}
impl<'_> From<&'_ H256> for U256 {
fn from(value: &'_ H256) -> U256 {
U256::from(value.bytes())
}
}
impl From<H256> for Address { impl From<H256> for Address {
fn from(value: H256) -> Address { fn from(value: H256) -> Address {
unsafe { unsafe {
@ -562,6 +574,7 @@ pub static ZERO_H256: H256 = H256([0x00; 32]);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use hash::*; use hash::*;
use uint::*;
use std::str::FromStr; use std::str::FromStr;
#[test] #[test]
@ -635,5 +648,18 @@ mod tests {
// too short. // too short.
assert_eq!(H64::from(0), H64::from("0x34567890abcdef")); assert_eq!(H64::from(0), H64::from("0x34567890abcdef"));
} }
#[test]
fn from_and_to_u256() {
let u: U256 = x!(0x123456789abcdef0u64);
let h = H256::from(u);
assert_eq!(H256::from(u), H256::from("000000000000000000000000000000000000000000000000123456789abcdef0"));
let h_ref = H256::from(&u);
assert_eq!(h, h_ref);
let r_ref: U256 = From::from(&h);
assert_eq!(r_ref, u);
let r: U256 = From::from(h);
assert_eq!(r, u);
}
} }