about summary refs log tree commit diff
path: root/compiler/rustc_serialize/src
diff options
context:
space:
mode:
authorBen Kimock <kimockb@gmail.com>2024-05-03 21:17:57 -0400
committerBen Kimock <kimockb@gmail.com>2024-05-21 20:12:29 -0400
commit95150d72465db491f4b04b73545e106462bd003b (patch)
tree99ebc1f9f9f08d0ff0065ac0a05fd60bbfb74eb2 /compiler/rustc_serialize/src
parent39e02f1bd1e53d009da382654139f7c0639172a8 (diff)
downloadrust-95150d72465db491f4b04b73545e106462bd003b.tar.gz
rust-95150d72465db491f4b04b73545e106462bd003b.zip
Add a footer in FileEncoder and check for it in MemDecoder
Diffstat (limited to 'compiler/rustc_serialize/src')
-rw-r--r--compiler/rustc_serialize/src/opaque.rs16
1 files changed, 11 insertions, 5 deletions
diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs
index eec83c02d35..1b4b1d2436f 100644
--- a/compiler/rustc_serialize/src/opaque.rs
+++ b/compiler/rustc_serialize/src/opaque.rs
@@ -17,6 +17,8 @@ use crate::int_overflow::DebugStrictAdd;
 
 pub type FileEncodeResult = Result<usize, (PathBuf, io::Error)>;
 
+const FOOTER: &[u8] = b"rust-end-file";
+
 /// The size of the buffer in `FileEncoder`.
 const BUF_SIZE: usize = 8192;
 
@@ -181,6 +183,7 @@ impl FileEncoder {
     }
 
     pub fn finish(&mut self) -> FileEncodeResult {
+        self.write_all(FOOTER);
         self.flush();
         #[cfg(debug_assertions)]
         {
@@ -261,15 +264,18 @@ pub struct MemDecoder<'a> {
 
 impl<'a> MemDecoder<'a> {
     #[inline]
-    pub fn new(data: &'a [u8], position: usize) -> MemDecoder<'a> {
+    pub fn new(data: &'a [u8], position: usize) -> Option<MemDecoder<'a>> {
+        let data = data.strip_suffix(FOOTER)?;
         let Range { start, end } = data.as_ptr_range();
-        MemDecoder { start, current: data[position..].as_ptr(), end, _marker: PhantomData }
+        Some(MemDecoder { start, current: data[position..].as_ptr(), end, _marker: PhantomData })
     }
 
     #[inline]
-    pub fn data(&self) -> &'a [u8] {
-        // SAFETY: This recovers the original slice, only using members we never modify.
-        unsafe { std::slice::from_raw_parts(self.start, self.len()) }
+    pub fn split_at(&self, position: usize) -> MemDecoder<'a> {
+        assert!(position <= self.len());
+        // SAFETY: We checked above that this offset is within the original slice
+        let current = unsafe { self.start.add(position) };
+        MemDecoder { start: self.start, current, end: self.end, _marker: PhantomData }
     }
 
     #[inline]