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 rand::os::OsRng;
|
||||||
use bytes::BytesConvertable;
|
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 {
|
macro_rules! impl_hash {
|
||||||
($from: ident, $size: expr) => {
|
($from: ident, $size: expr) => {
|
||||||
#[derive(Eq)]
|
#[derive(Eq)]
|
||||||
pub struct $from (pub [u8; $size]);
|
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] {
|
impl $from {
|
||||||
&mut self.0
|
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 {
|
impl FromStr for $from {
|
||||||
type Err = EthcoreError;
|
type Err = EthcoreError;
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ pub mod rlp;
|
|||||||
pub mod vector;
|
pub mod vector;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
pub mod sha3;
|
pub mod sha3;
|
||||||
|
pub mod bloom;
|
||||||
|
|
||||||
//pub mod network;
|
//pub mod network;
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::mem::uninitialized;
|
use std::mem::uninitialized;
|
||||||
use tiny_keccak::keccak_256;
|
use tiny_keccak::keccak_256;
|
||||||
use bytes::BytesConvertable;
|
use bytes::BytesConvertable;
|
||||||
use hash::H256;
|
use hash::{FixedHash, H256};
|
||||||
|
|
||||||
trait Hashable {
|
trait Hashable {
|
||||||
fn sha3(&self) -> H256;
|
fn sha3(&self) -> H256;
|
||||||
|
Loading…
Reference in New Issue
Block a user