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 { impl<'a> From<&'a [$ty]> for $thing {
fn from(data: &'a [$ty]) -> $thing { fn from(data: &'a [$ty]) -> $thing {
assert_eq!(data.len(), $len); assert_eq!(data.len(), $len);
unsafe { let mut ret = [0; $len];
use std::intrinsics::copy_nonoverlapping; ret.copy_from_slice(&data[..]);
use std::mem; $thing(ret)
let mut ret: $thing = mem::uninitialized();
copy_nonoverlapping(data.as_ptr(),
ret.as_mut_ptr(),
$len);
ret
}
} }
} }
@ -193,18 +187,15 @@ macro_rules! impl_array_newtype_encodable {
fn visit_seq<V>(&mut self, mut v: V) -> Result<$thing, V::Error> fn visit_seq<V>(&mut self, mut v: V) -> Result<$thing, V::Error>
where V: ::serde::de::SeqVisitor where V: ::serde::de::SeqVisitor
{ {
unsafe { let mut ret: [$ty; $len] = [0; $len];
use std::mem; for item in ret.iter_mut() {
let mut ret: [$ty; $len] = mem::uninitialized(); *item = match try!(v.visit()) {
for item in ret.iter_mut() { Some(c) => c,
*item = match try!(v.visit()) { None => return Err(::serde::de::Error::end_of_stream())
Some(c) => c, };
None => return Err(::serde::de::Error::end_of_stream())
};
}
try!(v.end());
Ok($thing(ret))
} }
try!(v.end());
Ok($thing(ret))
} }
} }

View File

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

View File

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