Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c57d778

Browse files
committed
define MmapMut and use it in Decodable impl
1 parent 47c3689 commit c57d778

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

compiler/rustc_data_structures/src/memmap.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fs::File;
22
use std::io;
3-
use std::ops::Deref;
3+
use std::ops::{Deref, DerefMut};
44

55
use crate::owning_ref::StableAddress;
66

@@ -45,3 +45,64 @@ impl Deref for Mmap {
4545
// export any function that can cause the `Vec` to be re-allocated. As such the address of the
4646
// bytes inside this `Vec` is stable.
4747
unsafe impl StableAddress for Mmap {}
48+
49+
#[cfg(not(target_arch = "wasm32"))]
50+
pub struct MmapMut(memmap2::MmapMut);
51+
52+
#[cfg(target_arch = "wasm32")]
53+
pub struct MmapMut(Vec<u8>);
54+
55+
#[cfg(not(target_arch = "wasm32"))]
56+
impl MmapMut {
57+
#[inline]
58+
pub fn map_anon(len: usize) -> io::Result<Self> {
59+
let mmap = memmap2::MmapMut::map_anon(len)?;
60+
Ok(MmapMut(mmap))
61+
}
62+
63+
#[inline]
64+
pub fn flush(&mut self) -> io::Result<()> {
65+
self.0.flush()
66+
}
67+
68+
#[inline]
69+
pub fn make_read_only(self) -> std::io::Result<Mmap> {
70+
let mmap = self.0.make_read_only()?;
71+
Ok(Mmap(mmap))
72+
}
73+
}
74+
75+
#[cfg(target_arch = "wasm32")]
76+
impl MmapMut {
77+
#[inline]
78+
pub fn map_anon(len: usize) -> io::Result<Self> {
79+
let data = Vec::with_capacity(len);
80+
Ok(MmapMut(data))
81+
}
82+
83+
#[inline]
84+
pub fn flush(&mut self) -> io::Result<()> {
85+
Ok(())
86+
}
87+
88+
#[inline]
89+
pub fn make_read_only(self) -> std::io::Result<Mmap> {
90+
Ok(Mmap(self.0))
91+
}
92+
}
93+
94+
impl Deref for MmapMut {
95+
type Target = [u8];
96+
97+
#[inline]
98+
fn deref(&self) -> &[u8] {
99+
&*self.0
100+
}
101+
}
102+
103+
impl DerefMut for MmapMut {
104+
#[inline]
105+
fn deref_mut(&mut self) -> &mut [u8] {
106+
&mut *self.0
107+
}
108+
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::rmeta::*;
44

55
use rustc_data_structures::fingerprint::Fingerprint;
66
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
7-
use rustc_data_structures::memmap::Mmap;
7+
use rustc_data_structures::memmap::{Mmap, MmapMut};
88
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
99
use rustc_data_structures::sync::{join, par_iter, Lrc, ParallelIterator};
1010
use rustc_data_structures::temp_dir::MaybeTempDir;
@@ -44,7 +44,6 @@ use std::io::{Read, Seek, Write};
4444
use std::iter;
4545
use std::num::NonZeroUsize;
4646
use std::path::{Path, PathBuf};
47-
use tempfile::Builder as TempFileBuilder;
4847
use tracing::{debug, trace};
4948

5049
pub(super) struct EncodeContext<'a, 'tcx> {
@@ -2179,19 +2178,19 @@ impl<S: Encoder> Encodable<S> for EncodedMetadata {
21792178

21802179
impl<D: Decoder> Decodable<D> for EncodedMetadata {
21812180
fn decode(d: &mut D) -> Self {
2182-
let temp_dir = TempFileBuilder::new().prefix("decoded").tempdir().unwrap();
2183-
let temp_dir = MaybeTempDir::new(temp_dir, false);
2184-
let filename = temp_dir.as_ref().join("decoded");
2185-
let file = std::fs::File::create(&filename).unwrap();
2186-
let mut file = std::io::BufWriter::new(file);
2187-
21882181
let len = d.read_usize();
2189-
for _ in 0..len {
2190-
file.write(&[d.read_u8()]).unwrap();
2191-
}
2192-
file.flush().unwrap();
2182+
let mmap = if len > 0 {
2183+
let mut mmap = MmapMut::map_anon(len).unwrap();
2184+
for _ in 0..len {
2185+
(&mut mmap[..]).write(&[d.read_u8()]).unwrap();
2186+
}
2187+
mmap.flush().unwrap();
2188+
Some(mmap.make_read_only().unwrap())
2189+
} else {
2190+
None
2191+
};
21932192

2194-
Self::from_path(filename, Some(temp_dir)).unwrap()
2193+
Self { mmap, _temp_dir: None }
21952194
}
21962195
}
21972196

0 commit comments

Comments
 (0)