common changes in hash

This commit is contained in:
debris 2015-11-28 01:38:00 +01:00
parent 815e01a781
commit 47f8695c3b
4 changed files with 27 additions and 17 deletions

View File

@ -1,7 +0,0 @@
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

@ -9,10 +9,13 @@ use rand::os::OsRng;
use bytes::BytesConvertable;
/// types implementing FixedHash must be also BytesConvertable
pub trait FixedHash: BytesConvertable {
pub trait FixedHash: Sized + BytesConvertable {
fn new() -> Self;
fn random() -> Self;
fn randomize(&mut self);
fn mut_bytes(&mut self) -> &mut [u8];
fn shift_bloom<'a, T>(&'a mut self, b: &T) -> &'a mut Self where T: FixedHash;
fn bloom_part<T>(&self) -> T where T: FixedHash;
}
macro_rules! impl_hash {
@ -20,13 +23,6 @@ macro_rules! impl_hash {
#[derive(Eq)]
pub struct $from (pub [u8; $size]);
impl $from {
fn new() -> $from {
$from([0; $size])
}
}
impl BytesConvertable for $from {
fn bytes(&self) -> &[u8] {
&self.0
@ -34,6 +30,10 @@ macro_rules! impl_hash {
}
impl FixedHash for $from {
fn new() -> $from {
$from([0; $size])
}
fn random() -> $from {
let mut hash = $from::new();
hash.randomize();
@ -48,6 +48,24 @@ macro_rules! impl_hash {
fn mut_bytes(&mut self) -> &mut [u8] {
&mut self.0
}
fn shift_bloom<'a, T>(&'a mut self, b: &T) -> &'a mut Self where T: FixedHash {
let bp: Self = b.bloom_part();
let new_self = &bp | self;
// impl |= instead
unsafe {
use std::{mem, ptr};
ptr::copy(new_self.0.as_ptr(), self.0.as_mut_ptr(), mem::size_of::<Self>());
}
self
}
fn bloom_part<T>(&self) -> T where T: FixedHash {
panic!()
}
}
impl FromStr for $from {

View File

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

View File

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