Skip to content

Commit 7c6274d

Browse files
committed
rustc_serialize: have read_raw_bytes take MaybeUninit<u8> slice
1 parent a4daa63 commit 7c6274d

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

compiler/rustc_data_structures/src/fingerprint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_serialize::{
44
Decodable, Encodable,
55
};
66
use std::hash::{Hash, Hasher};
7-
use std::mem;
7+
use std::mem::{self, MaybeUninit};
88

99
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy)]
1010
pub struct Fingerprint(u64, u64);
@@ -61,7 +61,7 @@ impl Fingerprint {
6161
}
6262

6363
pub fn decode_opaque(decoder: &mut opaque::Decoder<'_>) -> Result<Fingerprint, String> {
64-
let mut bytes = [0; 16];
64+
let mut bytes: [MaybeUninit<u8>; 16] = MaybeUninit::uninit_array();
6565

6666
decoder.read_raw_bytes(&mut bytes)?;
6767

compiler/rustc_serialize/src/opaque.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::leb128::{self, read_signed_leb128, write_signed_leb128};
22
use crate::serialize;
33
use std::borrow::Cow;
4+
use std::mem::MaybeUninit;
5+
use std::ptr;
46

57
// -----------------------------------------------------------------------------
68
// Encoder
@@ -179,11 +181,19 @@ impl<'a> Decoder<'a> {
179181
}
180182

181183
#[inline]
182-
pub fn read_raw_bytes(&mut self, s: &mut [u8]) -> Result<(), String> {
184+
pub fn read_raw_bytes(&mut self, s: &mut [MaybeUninit<u8>]) -> Result<(), String> {
183185
let start = self.position;
184186
let end = start + s.len();
185-
186-
s.copy_from_slice(&self.data[start..end]);
187+
assert!(end <= self.data.len());
188+
189+
// SAFETY: Both `src` and `dst` point to at least `s.len()` elements:
190+
// `src` points to at least `s.len()` elements by above assert, and
191+
// `dst` points to `s.len()` elements by derivation from `s`.
192+
unsafe {
193+
let src = self.data.as_ptr().add(start);
194+
let dst = s.as_mut_ptr() as *mut u8;
195+
ptr::copy_nonoverlapping(src, dst, s.len());
196+
}
187197

188198
self.position = end;
189199

0 commit comments

Comments
 (0)