FixedHash trait

This commit is contained in:
debris 2015-11-27 20:10:31 +01:00
parent b596528377
commit 1eacff7e3d
4 changed files with 36 additions and 16 deletions

7
src/bloom.rs Normal file
View File

@ -0,0 +1,7 @@
use bytes::BytesConvertable;
// use hash::FixedHash;
pub trait Bloomable {
fn shift_bloom<T>(&mut self, bytes: &T) where T: BytesConvertable;
fn contains_bloom<T>(&self, bytes: &T) -> bool where T: BytesConvertable;
}

View File

@ -8,27 +8,22 @@ use rand::Rng;
use rand::os::OsRng;
use bytes::BytesConvertable;
/// types implementing FixedHash must be also BytesConvertable
pub trait FixedHash: BytesConvertable {
fn random() -> Self;
fn randomize(&mut self);
fn mut_bytes(&mut self) -> &mut [u8];
}
macro_rules! impl_hash {
($from: ident, $size: expr) => {
#[derive(Eq)]
pub struct $from (pub [u8; $size]);
impl $from {
pub fn new() -> $from {
$from([0; $size])
}
pub fn random() -> $from {
let mut hash = $from::new();
hash.randomize();
hash
}
pub fn randomize(&mut self) {
let mut rng = OsRng::new().unwrap();
rng.fill_bytes(&mut self.0);
}
pub fn mut_bytes(&mut self) -> &mut [u8; $size] {
&mut self.0
impl $from {
fn new() -> $from {
$from([0; $size])
}
}
@ -38,6 +33,23 @@ macro_rules! impl_hash {
}
}
impl FixedHash for $from {
fn random() -> $from {
let mut hash = $from::new();
hash.randomize();
hash
}
fn randomize(&mut self) {
let mut rng = OsRng::new().unwrap();
rng.fill_bytes(&mut self.0);
}
fn mut_bytes(&mut self) -> &mut [u8] {
&mut self.0
}
}
impl FromStr for $from {
type Err = EthcoreError;

View File

@ -16,6 +16,7 @@ pub mod rlp;
pub mod vector;
pub mod db;
pub mod sha3;
pub mod bloom;
//pub mod network;

View File

@ -1,7 +1,7 @@
use std::mem::uninitialized;
use tiny_keccak::keccak_256;
use bytes::BytesConvertable;
use hash::H256;
use hash::{FixedHash, H256};
trait Hashable {
fn sha3(&self) -> H256;