remove unsafety from util/hash.rs

This commit is contained in:
Robert Habermeier 2016-06-07 16:04:26 +02:00
parent e408b7ac99
commit 9ae93d6962

View File

@ -150,10 +150,8 @@ macro_rules! impl_hash {
}
fn copy_to(&self, dest: &mut[u8]) {
unsafe {
let min = ::std::cmp::min($size, dest.len());
::std::ptr::copy(self.0.as_ptr(), dest.as_mut_ptr(), min);
}
dest[..min].copy_from_slice(&self.0[..min]);
}
fn shift_bloomed<'a, T>(&'a mut self, b: &T) -> &'a mut Self where T: FixedHash {
@ -163,10 +161,7 @@ macro_rules! impl_hash {
// impl |= instead
// TODO: that's done now!
unsafe {
use std::{mem, ptr};
ptr::copy(new_self.0.as_ptr(), self.0.as_mut_ptr(), mem::size_of::<Self>());
}
self.0 = new_self.0;
self
}
@ -316,14 +311,11 @@ macro_rules! impl_hash {
#[cfg_attr(feature="dev", allow(expl_impl_clone_on_copy))]
impl Clone for $from {
fn clone(&self) -> $from {
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>());
let mut ret = $from::new();
ret.0.copy_from_slice(&self.0);
ret
}
}
}
impl PartialEq for $from {
fn eq(&self, other: &Self) -> bool {
@ -404,16 +396,13 @@ macro_rules! impl_hash {
type Output = $from;
fn bitor(self, rhs: Self) -> Self::Output {
unsafe {
use std::mem;
let mut ret: $from = mem::uninitialized();
let mut ret: $from = $from::default();
for i in 0..$size {
ret.0[i] = self.0[i] | rhs.0[i];
}
ret
}
}
}
/// Moving `BitOr`
impl BitOr for $from {
@ -429,16 +418,13 @@ macro_rules! impl_hash {
type Output = $from;
fn bitand(self, rhs: Self) -> Self::Output {
unsafe {
use std::mem;
let mut ret: $from = mem::uninitialized();
let mut ret: $from = $from::default();
for i in 0..$size {
ret.0[i] = self.0[i] & rhs.0[i];
}
ret
}
}
}
/// Moving `BitAnd`
impl BitAnd for $from {
@ -454,16 +440,13 @@ macro_rules! impl_hash {
type Output = $from;
fn bitxor(self, rhs: Self) -> Self::Output {
unsafe {
use std::mem;
let mut ret: $from = mem::uninitialized();
let mut ret: $from = $from::default();
for i in 0..$size {
ret.0[i] = self.0[i] ^ rhs.0[i];
}
ret
}
}
}
/// Moving `BitXor`
impl BitXor for $from {
@ -516,23 +499,19 @@ macro_rules! impl_hash {
impl From<U256> for H256 {
fn from(value: U256) -> H256 {
unsafe {
let mut ret: H256 = ::std::mem::uninitialized();
let mut ret = H256::new();
value.to_raw_bytes(&mut ret);
ret
}
}
}
impl<'a> From<&'a U256> for H256 {
fn from(value: &'a U256) -> H256 {
unsafe {
let mut ret: H256 = ::std::mem::uninitialized();
let mut ret: H256 = H256::new();
value.to_raw_bytes(&mut ret);
ret
}
}
}
impl From<H256> for U256 {
fn from(value: H256) -> U256 {
@ -548,53 +527,46 @@ impl<'a> From<&'a H256> for U256 {
impl From<H256> for Address {
fn from(value: H256) -> Address {
unsafe {
let mut ret: Address = ::std::mem::uninitialized();
::std::ptr::copy(value.as_ptr().offset(12), ret.as_mut_ptr(), 20);
let mut ret = Address::new();
ret.0.copy_from_slice(&value[12..32]);
ret
}
}
}
impl From<H256> for H64 {
fn from(value: H256) -> H64 {
unsafe {
let mut ret: H64 = ::std::mem::uninitialized();
::std::ptr::copy(value.as_ptr().offset(20), ret.as_mut_ptr(), 8);
let mut ret = H64::new();
ret.0.copy_from_slice(&value[20..28]);
ret
}
}
}
/*
impl<'a> From<&'a H256> for Address {
fn from(value: &'a H256) -> Address {
unsafe {
let mut ret: Address = ::std::mem::uninitialized();
::std::ptr::copy(value.as_ptr().offset(12), ret.as_mut_ptr(), 20);
let mut ret = Address::new();
ret.0.copy_from_slice(&value[12..32]);
ret
}
}
}
*/
impl From<Address> for H256 {
fn from(value: Address) -> H256 {
unsafe {
let mut ret = H256::new();
::std::ptr::copy(value.as_ptr(), ret.as_mut_ptr().offset(12), 20);
ret.0[12..32].copy_from_slice(&value);
ret
}
}
}
impl<'a> From<&'a Address> for H256 {
fn from(value: &'a Address) -> H256 {
unsafe {
let mut ret = H256::new();
::std::ptr::copy(value.as_ptr(), ret.as_mut_ptr().offset(12), 20);
ret.0[12..32].copy_from_slice(&value);
ret
}
}
}
/// Convert string `s` to an `H256`. Will panic if `s` is not 64 characters long or if any of
/// those characters are not 0-9, a-z or A-Z.