FixedHash trait
This commit is contained in:
parent
b596528377
commit
1eacff7e3d
7
src/bloom.rs
Normal file
7
src/bloom.rs
Normal 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;
|
||||
}
|
42
src/hash.rs
42
src/hash.rs
@ -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;
|
||||
|
||||
|
@ -16,6 +16,7 @@ pub mod rlp;
|
||||
pub mod vector;
|
||||
pub mod db;
|
||||
pub mod sha3;
|
||||
pub mod bloom;
|
||||
|
||||
//pub mod network;
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user