Rollup merge of #94288 - Mark-Simulacrum:ser-opt, r=nnethercote

Cleanup a few Decoder methods

This is just some simple follow up to #93839.

r? `@nnethercote`
This commit is contained in:
Matthias Krüger 2022-02-24 07:48:09 +01:00 committed by GitHub
commit ae27c4ab1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 19 additions and 42 deletions

View file

@ -2418,8 +2418,7 @@ impl<S: Encoder> rustc_serialize::Encodable<S> for AttrId {
} }
impl<D: Decoder> rustc_serialize::Decodable<D> for AttrId { impl<D: Decoder> rustc_serialize::Decodable<D> for AttrId {
fn decode(d: &mut D) -> AttrId { fn decode(_: &mut D) -> AttrId {
d.read_unit();
crate::attr::mk_attr_id() crate::attr::mk_attr_id()
} }
} }

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

@ -63,8 +63,7 @@ impl<S: serialize::Encoder> serialize::Encodable<S> for PredecessorCache {
impl<D: serialize::Decoder> serialize::Decodable<D> for PredecessorCache { impl<D: serialize::Decoder> serialize::Decodable<D> for PredecessorCache {
#[inline] #[inline]
fn decode(d: &mut D) -> Self { fn decode(_: &mut D) -> Self {
let () = d.read_unit();
Self::new() Self::new()
} }
} }

View file

@ -465,8 +465,6 @@ macro_rules! implement_ty_decoder {
impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> { impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
$crate::__impl_decoder_methods! { $crate::__impl_decoder_methods! {
read_unit -> ();
read_u128 -> u128; read_u128 -> u128;
read_u64 -> u64; read_u64 -> u64;
read_u32 -> u32; read_u32 -> u32;
@ -485,12 +483,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,6 +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::borrow::Cow;
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};
@ -549,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 {
@ -563,11 +555,6 @@ macro_rules! read_leb128 {
} }
impl<'a> serialize::Decoder for Decoder<'a> { impl<'a> serialize::Decoder for Decoder<'a> {
#[inline]
fn read_unit(&mut self) -> () {
()
}
#[inline] #[inline]
fn read_u128(&mut self) -> u128 { fn read_u128(&mut self) -> u128 {
read_leb128!(self, read_u128_leb128) read_leb128!(self, read_u128_leb128)
@ -663,7 +650,7 @@ impl<'a> serialize::Decoder for Decoder<'a> {
} }
#[inline] #[inline]
fn read_str(&mut self) -> Cow<'_, 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);
@ -671,14 +658,14 @@ impl<'a> serialize::Decoder for Decoder<'a> {
std::str::from_utf8_unchecked(&self.data[self.position..self.position + len]) std::str::from_utf8_unchecked(&self.data[self.position..self.position + len])
}; };
self.position += len + 1; self.position += len + 1;
Cow::Borrowed(s) s
} }
#[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]
} }
} }
@ -746,10 +733,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

@ -181,7 +181,6 @@ pub trait Encoder {
// concise. // concise.
pub trait Decoder { pub trait Decoder {
// Primitive types: // Primitive types:
fn read_unit(&mut self) -> ();
fn read_usize(&mut self) -> usize; fn read_usize(&mut self) -> usize;
fn read_u128(&mut self) -> u128; fn read_u128(&mut self) -> u128;
fn read_u64(&mut self) -> u64; fn read_u64(&mut self) -> u64;
@ -198,8 +197,8 @@ pub trait Decoder {
fn read_f64(&mut self) -> f64; fn read_f64(&mut self) -> f64;
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) -> Cow<'_, 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
@ -313,7 +312,7 @@ impl<S: Encoder> Encodable<S> for String {
impl<D: Decoder> Decodable<D> for String { impl<D: Decoder> Decodable<D> for String {
fn decode(d: &mut D) -> String { fn decode(d: &mut D) -> String {
d.read_str().into_owned() d.read_str().to_owned()
} }
} }
@ -324,9 +323,7 @@ impl<S: Encoder> Encodable<S> for () {
} }
impl<D: Decoder> Decodable<D> for () { impl<D: Decoder> Decodable<D> for () {
fn decode(d: &mut D) -> () { fn decode(_: &mut D) -> () {}
d.read_unit()
}
} }
impl<S: Encoder, T> Encodable<S> for PhantomData<T> { impl<S: Encoder, T> Encodable<S> for PhantomData<T> {
@ -336,8 +333,7 @@ impl<S: Encoder, T> Encodable<S> for PhantomData<T> {
} }
impl<D: Decoder, T> Decodable<D> for PhantomData<T> { impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
fn decode(d: &mut D) -> PhantomData<T> { fn decode(_: &mut D) -> PhantomData<T> {
d.read_unit();
PhantomData PhantomData
} }
} }