Skip to content

Commit

Permalink
Provide copy-free access to raw Decoder bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed Feb 22, 2022
1 parent da3b2ca commit 2098ea6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 21 deletions.
4 changes: 1 addition & 3 deletions compiler/rustc_data_structures/src/fingerprint.rs
Expand Up @@ -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())
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/decoder.rs
Expand Up @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/ty/codec.rs
Expand Up @@ -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)
}
}
}
Expand Down
19 changes: 6 additions & 13 deletions compiler/rustc_serialize/src/opaque.rs
@@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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]
}
}

Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_serialize/src/serialize.rs
Expand Up @@ -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
Expand Down

0 comments on commit 2098ea6

Please sign in to comment.