removed copy from Hash, improved Clone, added BitOr

This commit is contained in:
debris 2015-11-27 23:24:01 +01:00
parent f8bd7da111
commit 815e01a781

View File

@ -1,7 +1,7 @@
use std::str::FromStr; use std::str::FromStr;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut, BitOr};
use rustc_serialize::hex::*; use rustc_serialize::hex::*;
use error::EthcoreError; use error::EthcoreError;
use rand::Rng; use rand::Rng;
@ -80,10 +80,14 @@ macro_rules! impl_hash {
impl Clone for $from { impl Clone for $from {
fn clone(&self) -> $from { fn clone(&self) -> $from {
*self unsafe {
use std::{mem, ptr};
let mut ret: $from = mem::uninitialized();
ptr::copy(self.0.as_ptr(), ret.0.as_mut_ptr(), mem::size_of::<$from>());
ret
}
} }
} }
impl Copy for $from {}
impl PartialEq for $from { impl PartialEq for $from {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
@ -115,6 +119,30 @@ macro_rules! impl_hash {
&mut self.0[index] &mut self.0[index]
} }
} }
impl<'a> BitOr for &'a $from {
type Output = $from;
fn bitor(self, rhs: Self) -> Self::Output {
unsafe {
use std::mem;
let mut ret: $from = mem::uninitialized();
for i in 0..$size {
ret.0[i] = self.0[i] | rhs.0[i];
}
ret
}
}
}
impl BitOr for $from {
type Output = $from;
fn bitor(self, rhs: Self) -> Self::Output {
&self | &rhs
}
}
} }
} }
@ -126,7 +154,6 @@ impl_hash!(H512, 64);
impl_hash!(H520, 65); impl_hash!(H520, 65);
impl_hash!(H1024, 128); impl_hash!(H1024, 128);
impl_hash!(H2048, 256); impl_hash!(H2048, 256);
impl_hash!(H4096, 512);
#[test] #[test]
fn hash() { fn hash() {
@ -138,3 +165,16 @@ fn hash() {
assert!(h != H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee])); assert!(h != H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee]));
assert!(h != H64([0; 8])); assert!(h != H64([0; 8]));
} }
#[test]
fn hash_bitor() {
let a = H64([1; 8]);
let b = H64([2; 8]);
let c = H64([3; 8]);
// borrow
assert_eq!(&a | &b, c);
// move
assert_eq!(a | b, c);
}