Skip to content

Commit ae27c4a

Browse files
authored
Rollup merge of rust-lang#94288 - Mark-Simulacrum:ser-opt, r=nnethercote
Cleanup a few Decoder methods This is just some simple follow up to rust-lang#93839. r? `@nnethercote`
2 parents f3433d1 + f1bcb0f commit ae27c4a

File tree

7 files changed

+19
-42
lines changed

7 files changed

+19
-42
lines changed

compiler/rustc_ast/src/ast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2418,8 +2418,7 @@ impl<S: Encoder> rustc_serialize::Encodable<S> for AttrId {
24182418
}
24192419

24202420
impl<D: Decoder> rustc_serialize::Decodable<D> for AttrId {
2421-
fn decode(d: &mut D) -> AttrId {
2422-
d.read_unit();
2421+
fn decode(_: &mut D) -> AttrId {
24232422
crate::attr::mk_attr_id()
24242423
}
24252424
}

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/mir/predecessors.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ impl<S: serialize::Encoder> serialize::Encodable<S> for PredecessorCache {
6363

6464
impl<D: serialize::Decoder> serialize::Decodable<D> for PredecessorCache {
6565
#[inline]
66-
fn decode(d: &mut D) -> Self {
67-
let () = d.read_unit();
66+
fn decode(_: &mut D) -> Self {
6867
Self::new()
6968
}
7069
}

compiler/rustc_middle/src/ty/codec.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,6 @@ macro_rules! implement_ty_decoder {
465465

466466
impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
467467
$crate::__impl_decoder_methods! {
468-
read_unit -> ();
469-
470468
read_u128 -> u128;
471469
read_u64 -> u64;
472470
read_u32 -> u32;
@@ -485,12 +483,12 @@ macro_rules! implement_ty_decoder {
485483
read_f64 -> f64;
486484
read_f32 -> f32;
487485
read_char -> char;
488-
read_str -> Cow<'_, str>;
486+
read_str -> &str;
489487
}
490488

491489
#[inline]
492-
fn read_raw_bytes_into(&mut self, bytes: &mut [u8]) {
493-
self.opaque.read_raw_bytes_into(bytes)
490+
fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
491+
self.opaque.read_raw_bytes(len)
494492
}
495493
}
496494
}

compiler/rustc_serialize/src/opaque.rs

+7-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::leb128::{self, max_leb128_len};
2-
use crate::serialize::{self, Encoder as _};
3-
use std::borrow::Cow;
2+
use crate::serialize::{self, Decoder as _, Encoder as _};
43
use std::convert::TryInto;
54
use std::fs::File;
65
use std::io::{self, Write};
@@ -549,25 +548,13 @@ impl<'a> Decoder<'a> {
549548
pub fn advance(&mut self, bytes: usize) {
550549
self.position += bytes;
551550
}
552-
553-
#[inline]
554-
pub fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
555-
let start = self.position;
556-
self.position += bytes;
557-
&self.data[start..self.position]
558-
}
559551
}
560552

561553
macro_rules! read_leb128 {
562554
($dec:expr, $fun:ident) => {{ leb128::$fun($dec.data, &mut $dec.position) }};
563555
}
564556

565557
impl<'a> serialize::Decoder for Decoder<'a> {
566-
#[inline]
567-
fn read_unit(&mut self) -> () {
568-
()
569-
}
570-
571558
#[inline]
572559
fn read_u128(&mut self) -> u128 {
573560
read_leb128!(self, read_u128_leb128)
@@ -663,22 +650,22 @@ impl<'a> serialize::Decoder for Decoder<'a> {
663650
}
664651

665652
#[inline]
666-
fn read_str(&mut self) -> Cow<'_, str> {
653+
fn read_str(&mut self) -> &'a str {
667654
let len = self.read_usize();
668655
let sentinel = self.data[self.position + len];
669656
assert!(sentinel == STR_SENTINEL);
670657
let s = unsafe {
671658
std::str::from_utf8_unchecked(&self.data[self.position..self.position + len])
672659
};
673660
self.position += len + 1;
674-
Cow::Borrowed(s)
661+
s
675662
}
676663

677664
#[inline]
678-
fn read_raw_bytes_into(&mut self, s: &mut [u8]) {
665+
fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
679666
let start = self.position;
680-
self.position += s.len();
681-
s.copy_from_slice(&self.data[start..self.position]);
667+
self.position += bytes;
668+
&self.data[start..self.position]
682669
}
683670
}
684671

@@ -746,10 +733,10 @@ impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize {
746733
fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize {
747734
let _start_pos = decoder.position();
748735
let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE);
736+
let value = u64::from_le_bytes(bytes.try_into().unwrap());
749737
let _end_pos = decoder.position();
750738
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
751739

752-
let value = u64::from_le_bytes(bytes.try_into().unwrap());
753740
IntEncodedWithFixedSize(value)
754741
}
755742
}

compiler/rustc_serialize/src/serialize.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ pub trait Encoder {
181181
// concise.
182182
pub trait Decoder {
183183
// Primitive types:
184-
fn read_unit(&mut self) -> ();
185184
fn read_usize(&mut self) -> usize;
186185
fn read_u128(&mut self) -> u128;
187186
fn read_u64(&mut self) -> u64;
@@ -198,8 +197,8 @@ pub trait Decoder {
198197
fn read_f64(&mut self) -> f64;
199198
fn read_f32(&mut self) -> f32;
200199
fn read_char(&mut self) -> char;
201-
fn read_str(&mut self) -> Cow<'_, str>;
202-
fn read_raw_bytes_into(&mut self, s: &mut [u8]);
200+
fn read_str(&mut self) -> &str;
201+
fn read_raw_bytes(&mut self, len: usize) -> &[u8];
203202
}
204203

205204
/// Trait for types that can be serialized
@@ -313,7 +312,7 @@ impl<S: Encoder> Encodable<S> for String {
313312

314313
impl<D: Decoder> Decodable<D> for String {
315314
fn decode(d: &mut D) -> String {
316-
d.read_str().into_owned()
315+
d.read_str().to_owned()
317316
}
318317
}
319318

@@ -324,9 +323,7 @@ impl<S: Encoder> Encodable<S> for () {
324323
}
325324

326325
impl<D: Decoder> Decodable<D> for () {
327-
fn decode(d: &mut D) -> () {
328-
d.read_unit()
329-
}
326+
fn decode(_: &mut D) -> () {}
330327
}
331328

332329
impl<S: Encoder, T> Encodable<S> for PhantomData<T> {
@@ -336,8 +333,7 @@ impl<S: Encoder, T> Encodable<S> for PhantomData<T> {
336333
}
337334

338335
impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
339-
fn decode(d: &mut D) -> PhantomData<T> {
340-
d.read_unit();
336+
fn decode(_: &mut D) -> PhantomData<T> {
341337
PhantomData
342338
}
343339
}

0 commit comments

Comments
 (0)