Skip to content

Commit 34f8889

Browse files
committed
seek and write the root position to the metadata file
1 parent 336af60 commit 34f8889

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc_span::{
4040
use rustc_target::abi::VariantIdx;
4141
use std::borrow::Borrow;
4242
use std::hash::Hash;
43-
use std::io::Write;
43+
use std::io::{Seek, Write};
4444
use std::iter;
4545
use std::num::NonZeroUsize;
4646
use std::path::{Path, PathBuf};
@@ -2165,7 +2165,7 @@ impl EncodedMetadata {
21652165
}
21662166

21672167
impl<S: Encoder> Encodable<S> for EncodedMetadata {
2168-
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
2168+
fn encode(&self, s: &mut S) {
21692169
let slice = self.raw_data();
21702170
slice.encode(s)
21712171
}
@@ -2248,20 +2248,25 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: impl AsRef<Path>) {
22482248
let root = ecx.encode_crate_root();
22492249

22502250
ecx.opaque.flush();
2251-
let mut result = std::fs::read(path.as_ref()).unwrap();
2251+
let mut file = std::fs::OpenOptions::new()
2252+
.write(true)
2253+
.open(path.as_ref())
2254+
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to open the file: {}", err)));
22522255

22532256
// Encode the root position.
22542257
let header = METADATA_HEADER.len();
2258+
file.seek(std::io::SeekFrom::Start(header as u64))
2259+
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to seek the file: {}", err)));
22552260
let pos = root.position.get();
2256-
result[header + 0] = (pos >> 24) as u8;
2257-
result[header + 1] = (pos >> 16) as u8;
2258-
result[header + 2] = (pos >> 8) as u8;
2259-
result[header + 3] = (pos >> 0) as u8;
2260-
2261-
std::fs::write(path, &result).unwrap();
2261+
file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8])
2262+
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to write to the file: {}", err)));
22622263

22632264
// Record metadata size for self-profiling
2264-
tcx.prof.artifact_size("crate_metadata", "crate_metadata", result.len() as u64);
2265+
tcx.prof.artifact_size(
2266+
"crate_metadata",
2267+
"crate_metadata",
2268+
file.metadata().unwrap().len() as u64,
2269+
);
22652270
}
22662271

22672272
pub fn provide(providers: &mut Providers) {

0 commit comments

Comments
 (0)