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