1
Fork 0

Provide copy-free access to raw Decoder bytes

This commit is contained in:
Mark Rousskov 2022-02-22 18:11:59 -05:00
parent da3b2ca956
commit 2098ea6eba
5 changed files with 12 additions and 21 deletions

View file

@ -153,9 +153,7 @@ impl<E: rustc_serialize::Encoder> Encodable<E> for Fingerprint {
impl<D: rustc_serialize::Decoder> Decodable<D> for Fingerprint { impl<D: rustc_serialize::Decoder> Decodable<D> for Fingerprint {
#[inline] #[inline]
fn decode(d: &mut D) -> Self { fn decode(d: &mut D) -> Self {
let mut bytes = [0u8; 16]; Fingerprint::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap())
d.read_raw_bytes_into(&mut bytes);
Fingerprint::from_le_bytes(bytes)
} }
} }

View file

@ -316,7 +316,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
} }
#[inline] #[inline]
pub fn read_raw_bytes(&mut self, len: usize) -> &'a [u8] { pub fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
self.opaque.read_raw_bytes(len) self.opaque.read_raw_bytes(len)
} }
} }

View file

@ -485,12 +485,12 @@ macro_rules! implement_ty_decoder {
read_f64 -> f64; read_f64 -> f64;
read_f32 -> f32; read_f32 -> f32;
read_char -> char; read_char -> char;
read_str -> Cow<'_, str>; read_str -> &str;
} }
#[inline] #[inline]
fn read_raw_bytes_into(&mut self, bytes: &mut [u8]) { fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
self.opaque.read_raw_bytes_into(bytes) self.opaque.read_raw_bytes(len)
} }
} }
} }

View file

@ -1,5 +1,5 @@
use crate::leb128::{self, max_leb128_len}; use crate::leb128::{self, max_leb128_len};
use crate::serialize::{self, Encoder as _}; use crate::serialize::{self, Decoder as _, Encoder as _};
use std::convert::TryInto; use std::convert::TryInto;
use std::fs::File; use std::fs::File;
use std::io::{self, Write}; use std::io::{self, Write};
@ -548,13 +548,6 @@ impl<'a> Decoder<'a> {
pub fn advance(&mut self, bytes: usize) { pub fn advance(&mut self, bytes: usize) {
self.position += bytes; self.position += bytes;
} }
#[inline]
pub fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
let start = self.position;
self.position += bytes;
&self.data[start..self.position]
}
} }
macro_rules! read_leb128 { macro_rules! read_leb128 {
@ -662,7 +655,7 @@ impl<'a> serialize::Decoder for Decoder<'a> {
} }
#[inline] #[inline]
fn read_str(&mut self) -> &str { fn read_str(&mut self) -> &'a str {
let len = self.read_usize(); let len = self.read_usize();
let sentinel = self.data[self.position + len]; let sentinel = self.data[self.position + len];
assert!(sentinel == STR_SENTINEL); assert!(sentinel == STR_SENTINEL);
@ -674,10 +667,10 @@ impl<'a> serialize::Decoder for Decoder<'a> {
} }
#[inline] #[inline]
fn read_raw_bytes_into(&mut self, s: &mut [u8]) { fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
let start = self.position; let start = self.position;
self.position += s.len(); self.position += bytes;
s.copy_from_slice(&self.data[start..self.position]); &self.data[start..self.position]
} }
} }
@ -745,10 +738,10 @@ impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize {
fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize { fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize {
let _start_pos = decoder.position(); let _start_pos = decoder.position();
let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE); let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE);
let value = u64::from_le_bytes(bytes.try_into().unwrap());
let _end_pos = decoder.position(); let _end_pos = decoder.position();
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE); debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
let value = u64::from_le_bytes(bytes.try_into().unwrap());
IntEncodedWithFixedSize(value) IntEncodedWithFixedSize(value)
} }
} }

View file

@ -199,7 +199,7 @@ pub trait Decoder {
fn read_f32(&mut self) -> f32; fn read_f32(&mut self) -> f32;
fn read_char(&mut self) -> char; fn read_char(&mut self) -> char;
fn read_str(&mut self) -> &str; fn read_str(&mut self) -> &str;
fn read_raw_bytes_into(&mut self, s: &mut [u8]); fn read_raw_bytes(&mut self, len: usize) -> &[u8];
} }
/// Trait for types that can be serialized /// Trait for types that can be serialized