Display stuff.

This commit is contained in:
Gav Wood 2015-11-26 00:02:43 +01:00
parent 168f3220a6
commit 31ee96f01c
2 changed files with 34 additions and 5 deletions

View File

@ -1,14 +1,13 @@
use rustc_serialize::hex::*; use rustc_serialize::hex::*;
use error::EthcoreError; use error::EthcoreError;
use std::str::FromStr; use std::str::FromStr;
use std::fmt;
macro_rules! impl_hash { macro_rules! impl_hash {
($from: ident, $size: expr) => { ($from: ident, $size: expr) => {
#[derive(PartialEq, Debug)] pub struct $from (pub [u8; $size]);
struct $from ([u8; $size]);
impl FromStr for $from { impl FromStr for $from {
// type Output = $from;
type Err = EthcoreError; type Err = EthcoreError;
fn from_str(s: &str) -> Result<$from, EthcoreError> { fn from_str(s: &str) -> Result<$from, EthcoreError> {
@ -21,6 +20,30 @@ macro_rules! impl_hash {
Ok(ret) Ok(ret)
} }
} }
impl fmt::Debug for $from {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in self.0.iter() {
try!(write!(f, "{:02x}", i));
}
Ok(())
}
}
impl fmt::Display for $from {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(self as &fmt::Debug).fmt(f)
}
}
impl PartialEq for $from {
fn eq(&self, other: &Self) -> bool {
for i in 0..$size {
if self.0[i] != other.0[i] {
return false;
}
}
true
}
}
} }
} }
@ -28,7 +51,11 @@ impl_hash!(Hash64, 8);
impl_hash!(Hash128, 16); impl_hash!(Hash128, 16);
impl_hash!(Address, 20); impl_hash!(Address, 20);
impl_hash!(Hash256, 32); impl_hash!(Hash256, 32);
//impl_hash!(Hash512, 64); impl_hash!(Hash512, 64);
impl_hash!(Hash520, 65);
impl_hash!(Hash1024, 128);
impl_hash!(Hash2048, 256);
impl_hash!(Hash4096, 512);
#[test] #[test]
fn it_works() { fn it_works() {

View File

@ -1,5 +1,7 @@
extern crate rustc_serialize; extern crate rustc_serialize;
pub use std::str::FromStr;
pub mod error; pub mod error;
pub mod hash; pub mod hash;
pub mod bytes; pub mod bytes;