]> git.lizzy.rs Git - rust.git/commitdiff
Provide copy-free access to raw Decoder bytes
authorMark Rousskov <mark.simulacrum@gmail.com>
Tue, 22 Feb 2022 23:11:59 +0000 (18:11 -0500)
committerMark Rousskov <mark.simulacrum@gmail.com>
Tue, 22 Feb 2022 23:11:59 +0000 (18:11 -0500)
compiler/rustc_data_structures/src/fingerprint.rs
compiler/rustc_metadata/src/rmeta/decoder.rs
compiler/rustc_middle/src/ty/codec.rs
compiler/rustc_serialize/src/opaque.rs
compiler/rustc_serialize/src/serialize.rs

index e931379dd3a70d5d7416e034d1877d01f0605dd8..c88f3e73cff37255d14bbfffaf947d6189f76a09 100644 (file)
@@ -153,9 +153,7 @@ fn encode(&self, s: &mut E) -> Result<(), E::Error> {
 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())
     }
 }
 
index 66968c9ba54ab77d9036b3efd031fc3cea8ba2db..b715f6c3f1fc97284277bb64c245f3a63151bcae 100644 (file)
@@ -316,7 +316,7 @@ fn read_lazy_with_meta<T: ?Sized + LazyMeta>(&mut self, meta: T::Meta) -> Lazy<T
     }
 
     #[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)
     }
 }
index ecd30ba441ff4c6afd892603087f4039c8ffb2f2..7a6cbea00d866ce7cf8d923e2384a3ee8e7e24b2 100644 (file)
@@ -485,12 +485,12 @@ impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> {
                     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)
                 }
             }
         }
index 8e2c866cd386c58dab53d7b1f6d511155c140856..1a71ee9038bcbee688930cb791539797e69cae3a 100644 (file)
@@ -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 @@ pub fn set_position(&mut self, pos: usize) {
     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 @@ fn read_char(&mut self) -> char {
     }
 
     #[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 @@ fn read_str(&mut self) -> &str {
     }
 
     #[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)
     }
 }
index fbbd13657ba54de1b2ec4af4995a5ef812059d7f..7b6dd8b60f800fbfa1af7a32bcff5b20c9224629 100644 (file)
@@ -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