Skip to content

Commit 2098ea6

Browse files
Provide copy-free access to raw Decoder bytes
1 parent da3b2ca commit 2098ea6

File tree

5 files changed

+12
-21
lines changed

5 files changed

+12
-21
lines changed

compiler/rustc_data_structures/src/fingerprint.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,7 @@ impl<E: rustc_serialize::Encoder> Encodable<E> for Fingerprint {
153153
impl<D: rustc_serialize::Decoder> Decodable<D> for Fingerprint {
154154
#[inline]
155155
fn decode(d: &mut D) -> Self {
156-
let mut bytes = [0u8; 16];
157-
d.read_raw_bytes_into(&mut bytes);
158-
Fingerprint::from_le_bytes(bytes)
156+
Fingerprint::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap())
159157
}
160158
}
161159

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
316316
}
317317

318318
#[inline]
319-
pub fn read_raw_bytes(&mut self, len: usize) -> &'a [u8] {
319+
pub fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
320320
self.opaque.read_raw_bytes(len)
321321
}
322322
}

compiler/rustc_middle/src/ty/codec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,12 @@ macro_rules! implement_ty_decoder {
485485
read_f64 -> f64;
486486
read_f32 -> f32;
487487
read_char -> char;
488-
read_str -> Cow<'_, str>;
488+
read_str -> &str;
489489
}
490490

491491
#[inline]
492-
fn read_raw_bytes_into(&mut self, bytes: &mut [u8]) {
493-
self.opaque.read_raw_bytes_into(bytes)
492+
fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
493+
self.opaque.read_raw_bytes(len)
494494
}
495495
}
496496
}

compiler/rustc_serialize/src/opaque.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::leb128::{self, max_leb128_len};
2-
use crate::serialize::{self, Encoder as _};
2+
use crate::serialize::{self, Decoder as _, Encoder as _};
33
use std::convert::TryInto;
44
use std::fs::File;
55
use std::io::{self, Write};
@@ -548,13 +548,6 @@ impl<'a> Decoder<'a> {
548548
pub fn advance(&mut self, bytes: usize) {
549549
self.position += bytes;
550550
}
551-
552-
#[inline]
553-
pub fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
554-
let start = self.position;
555-
self.position += bytes;
556-
&self.data[start..self.position]
557-
}
558551
}
559552

560553
macro_rules! read_leb128 {
@@ -662,7 +655,7 @@ impl<'a> serialize::Decoder for Decoder<'a> {
662655
}
663656

664657
#[inline]
665-
fn read_str(&mut self) -> &str {
658+
fn read_str(&mut self) -> &'a str {
666659
let len = self.read_usize();
667660
let sentinel = self.data[self.position + len];
668661
assert!(sentinel == STR_SENTINEL);
@@ -674,10 +667,10 @@ impl<'a> serialize::Decoder for Decoder<'a> {
674667
}
675668

676669
#[inline]
677-
fn read_raw_bytes_into(&mut self, s: &mut [u8]) {
670+
fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
678671
let start = self.position;
679-
self.position += s.len();
680-
s.copy_from_slice(&self.data[start..self.position]);
672+
self.position += bytes;
673+
&self.data[start..self.position]
681674
}
682675
}
683676

@@ -745,10 +738,10 @@ impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize {
745738
fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize {
746739
let _start_pos = decoder.position();
747740
let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE);
741+
let value = u64::from_le_bytes(bytes.try_into().unwrap());
748742
let _end_pos = decoder.position();
749743
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
750744

751-
let value = u64::from_le_bytes(bytes.try_into().unwrap());
752745
IntEncodedWithFixedSize(value)
753746
}
754747
}

compiler/rustc_serialize/src/serialize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub trait Decoder {
199199
fn read_f32(&mut self) -> f32;
200200
fn read_char(&mut self) -> char;
201201
fn read_str(&mut self) -> &str;
202-
fn read_raw_bytes_into(&mut self, s: &mut [u8]);
202+
fn read_raw_bytes(&mut self, len: usize) -> &[u8];
203203
}
204204

205205
/// Trait for types that can be serialized

0 commit comments

Comments
 (0)