Merge pull request #46 from apoelstra/copy-nonoverlapping

remove all use of mem::uninitialized and mem::copy_nonoverlapping
This commit is contained in:
Andrew Poelstra 2018-02-16 22:13:39 +00:00 committed by GitHub
commit 7c56f4133b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 31 deletions

View File

@ -82,15 +82,9 @@ macro_rules! impl_array_newtype {
impl<'a> From<&'a [$ty]> for $thing {
fn from(data: &'a [$ty]) -> $thing {
assert_eq!(data.len(), $len);
unsafe {
use std::intrinsics::copy_nonoverlapping;
use std::mem;
let mut ret: $thing = mem::uninitialized();
copy_nonoverlapping(data.as_ptr(),
ret.as_mut_ptr(),
$len);
ret
}
let mut ret = [0; $len];
ret.copy_from_slice(&data[..]);
$thing(ret)
}
}
@ -193,9 +187,7 @@ macro_rules! impl_array_newtype_encodable {
fn visit_seq<V>(&mut self, mut v: V) -> Result<$thing, V::Error>
where V: ::serde::de::SeqVisitor
{
unsafe {
use std::mem;
let mut ret: [$ty; $len] = mem::uninitialized();
let mut ret: [$ty; $len] = [0; $len];
for item in ret.iter_mut() {
*item = match try!(v.visit()) {
Some(c) => c,
@ -206,7 +198,6 @@ macro_rules! impl_array_newtype_encodable {
Ok($thing(ret))
}
}
}
// Begin actual function
d.visit(Visitor { marker: ::std::marker::PhantomData })

View File

@ -68,14 +68,10 @@ impl fmt::Debug for Address {
impl Clone for Address {
fn clone(&self) -> Address {
unsafe {
use std::intrinsics::copy_nonoverlapping;
use std::mem;
let mut ret = mem::uninitialized();
copy_nonoverlapping(self,
&mut ret,
mem::size_of::<Address>());
ret
Address {
services: self.services,
address: self.address,
port: self.port,
}
}
}

View File

@ -255,7 +255,7 @@ impl Sha256dHash {
}
let bytes = s.as_bytes();
let mut ret: [u8; 32] = unsafe { mem::uninitialized() };
let mut ret = [0; 32];
for i in 0..32 {
let hi = match bytes[2*i] {
b @ b'0'...b'9' => (b - b'0') as u8,
@ -314,9 +314,9 @@ impl serde::Serialize for Sha256dHash {
where S: serde::Serializer,
{
unsafe {
use std::{char, mem, str};
use std::{char, str};
let mut string: [u8; 64] = mem::uninitialized();
let mut string = [0; 64];
for i in 0..32 {
string[2 * i] = char::from_digit((self.0[31 - i] / 0x10) as u32, 16).unwrap() as u8;
string[2 * i + 1] = char::from_digit((self.0[31 - i] & 0x0f) as u32, 16).unwrap() as u8;