remove all use of mem::uninitialized and mem::copy_nonoverlapping

This commit is contained in:
Andrew Poelstra 2018-02-14 16:44:02 +00:00
parent deda031975
commit 9f092a6f31
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,9 +187,7 @@ 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;
let mut ret: [$ty; $len] = mem::uninitialized();
for item in ret.iter_mut() { for item in ret.iter_mut() {
*item = match try!(v.visit()) { *item = match try!(v.visit()) {
Some(c) => c, Some(c) => c,
@ -206,7 +198,6 @@ macro_rules! impl_array_newtype_encodable {
Ok($thing(ret)) Ok($thing(ret))
} }
} }
}
// Begin actual function // Begin actual function
d.visit(Visitor { marker: ::std::marker::PhantomData }) d.visit(Visitor { marker: ::std::marker::PhantomData })

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;