rustc_serialize: have read_raw_bytes take MaybeUninit<u8> slice

This commit is contained in:
Tyson Nottingham 2020-12-16 19:46:19 -08:00
parent a4daa63a90
commit 7c6274d464
2 changed files with 14 additions and 4 deletions

View file

@ -4,7 +4,7 @@ use rustc_serialize::{
Decodable, Encodable,
};
use std::hash::{Hash, Hasher};
use std::mem;
use std::mem::{self, MaybeUninit};
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy)]
pub struct Fingerprint(u64, u64);
@ -61,7 +61,7 @@ impl Fingerprint {
}
pub fn decode_opaque(decoder: &mut opaque::Decoder<'_>) -> Result<Fingerprint, String> {
let mut bytes = [0; 16];
let mut bytes: [MaybeUninit<u8>; 16] = MaybeUninit::uninit_array();
decoder.read_raw_bytes(&mut bytes)?;

View file

@ -1,6 +1,8 @@
use crate::leb128::{self, read_signed_leb128, write_signed_leb128};
use crate::serialize;
use std::borrow::Cow;
use std::mem::MaybeUninit;
use std::ptr;
// -----------------------------------------------------------------------------
// Encoder
@ -179,11 +181,19 @@ impl<'a> Decoder<'a> {
}
#[inline]
pub fn read_raw_bytes(&mut self, s: &mut [u8]) -> Result<(), String> {
pub fn read_raw_bytes(&mut self, s: &mut [MaybeUninit<u8>]) -> Result<(), String> {
let start = self.position;
let end = start + s.len();
assert!(end <= self.data.len());
s.copy_from_slice(&self.data[start..end]);
// SAFETY: Both `src` and `dst` point to at least `s.len()` elements:
// `src` points to at least `s.len()` elements by above assert, and
// `dst` points to `s.len()` elements by derivation from `s`.
unsafe {
let src = self.data.as_ptr().add(start);
let dst = s.as_mut_ptr() as *mut u8;
ptr::copy_nonoverlapping(src, dst, s.len());
}
self.position = end;