From 815e01a781171d250fcd86f93a411a993bcf8787 Mon Sep 17 00:00:00 2001 From: debris Date: Fri, 27 Nov 2015 23:24:01 +0100 Subject: [PATCH] removed copy from Hash, improved Clone, added BitOr --- src/hash.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/src/hash.rs b/src/hash.rs index fc89b4ab9..98a76ad87 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -1,7 +1,7 @@ use std::str::FromStr; use std::fmt; use std::hash::{Hash, Hasher}; -use std::ops::{Index, IndexMut}; +use std::ops::{Index, IndexMut, BitOr}; use rustc_serialize::hex::*; use error::EthcoreError; use rand::Rng; @@ -80,10 +80,14 @@ macro_rules! impl_hash { impl Clone for $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 { fn eq(&self, other: &Self) -> bool { @@ -115,6 +119,30 @@ macro_rules! impl_hash { &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!(H1024, 128); impl_hash!(H2048, 256); -impl_hash!(H4096, 512); #[test] fn hash() { @@ -138,3 +165,16 @@ fn hash() { assert!(h != H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee])); 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); +}