Provide copy-free access to raw Decoder bytes
This commit is contained in:
parent
da3b2ca956
commit
2098ea6eba
@ -153,9 +153,7 @@ impl<E: rustc_serialize::Encoder> Encodable<E> for Fingerprint {
|
||||
impl<D: rustc_serialize::Decoder> Decodable<D> for Fingerprint {
|
||||
#[inline]
|
||||
fn decode(d: &mut D) -> Self {
|
||||
let mut bytes = [0u8; 16];
|
||||
d.read_raw_bytes_into(&mut bytes);
|
||||
Fingerprint::from_le_bytes(bytes)
|
||||
Fingerprint::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -316,7 +316,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
|
||||
}
|
||||
|
||||
#[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)
|
||||
}
|
||||
}
|
||||
|
@ -485,12 +485,12 @@ macro_rules! implement_ty_decoder {
|
||||
read_f64 -> f64;
|
||||
read_f32 -> f32;
|
||||
read_char -> char;
|
||||
read_str -> Cow<'_, str>;
|
||||
read_str -> &str;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_raw_bytes_into(&mut self, bytes: &mut [u8]) {
|
||||
self.opaque.read_raw_bytes_into(bytes)
|
||||
fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
|
||||
self.opaque.read_raw_bytes(len)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
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::fs::File;
|
||||
use std::io::{self, Write};
|
||||
@ -548,13 +548,6 @@ impl<'a> Decoder<'a> {
|
||||
pub fn advance(&mut self, bytes: usize) {
|
||||
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 {
|
||||
@ -662,7 +655,7 @@ impl<'a> serialize::Decoder for Decoder<'a> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_str(&mut self) -> &str {
|
||||
fn read_str(&mut self) -> &'a str {
|
||||
let len = self.read_usize();
|
||||
let sentinel = self.data[self.position + len];
|
||||
assert!(sentinel == STR_SENTINEL);
|
||||
@ -674,10 +667,10 @@ impl<'a> serialize::Decoder for Decoder<'a> {
|
||||
}
|
||||
|
||||
#[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;
|
||||
self.position += s.len();
|
||||
s.copy_from_slice(&self.data[start..self.position]);
|
||||
self.position += bytes;
|
||||
&self.data[start..self.position]
|
||||
}
|
||||
}
|
||||
|
||||
@ -745,10 +738,10 @@ impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize {
|
||||
fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize {
|
||||
let _start_pos = decoder.position();
|
||||
let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE);
|
||||
let value = u64::from_le_bytes(bytes.try_into().unwrap());
|
||||
let _end_pos = decoder.position();
|
||||
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
|
||||
|
||||
let value = u64::from_le_bytes(bytes.try_into().unwrap());
|
||||
IntEncodedWithFixedSize(value)
|
||||
}
|
||||
}
|
||||
|
@ -199,7 +199,7 @@ pub trait Decoder {
|
||||
fn read_f32(&mut self) -> f32;
|
||||
fn read_char(&mut self) -> char;
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user