refactor encode_and_write_metadata

This commit is contained in:
Yoshiki Matsuda 2022-06-01 00:14:42 +09:00
parent 34e44e5774
commit 0928061906
2 changed files with 32 additions and 45 deletions

View File

@ -68,53 +68,45 @@ pub fn encode_and_write_metadata(
.unwrap_or_else(|err| tcx.sess.fatal(&format!("couldn't create a temp dir: {}", err)));
let metadata_tmpdir = MaybeTempDir::new(metadata_tmpdir, tcx.sess.opts.cg.save_temps);
let metadata_filename = metadata_tmpdir.as_ref().join(METADATA_FILENAME);
let need_metadata_file = tcx.sess.opts.output_types.contains_key(&OutputType::Metadata);
let metadata = match metadata_kind {
match metadata_kind {
MetadataKind::None => {
if need_metadata_file {
// Though creating the empty file here seems to be meaningless, cargo expects it.
if let Err(e) = std::fs::File::create(&out_filename) {
tcx.sess.fatal(&format!(
"failed to create the file {}: {}",
out_filename.display(),
e
));
}
if tcx.sess.opts.json_artifact_notifications {
tcx.sess
.parse_sess
.span_diagnostic
.emit_artifact_notification(&out_filename, "metadata");
}
}
EncodedMetadata::empty()
std::fs::File::create(&metadata_filename).unwrap_or_else(|e| {
tcx.sess.fatal(&format!(
"failed to create the file {}: {}",
out_filename.display(),
e
))
});
}
MetadataKind::Uncompressed | MetadataKind::Compressed => {
encode_metadata(tcx, &metadata_filename);
let _prof_timer = tcx.sess.prof.generic_activity("write_crate_metadata");
let (metadata_filename, metadata_tmpdir) = if need_metadata_file {
if let Err(e) = non_durable_rename(&metadata_filename, &out_filename) {
tcx.sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
}
if tcx.sess.opts.json_artifact_notifications {
tcx.sess
.parse_sess
.span_diagnostic
.emit_artifact_notification(&out_filename, "metadata");
}
(out_filename, None)
} else {
(metadata_filename, Some(metadata_tmpdir))
};
EncodedMetadata::from_path(metadata_filename, metadata_tmpdir).unwrap_or_else(|e| {
tcx.sess.fatal(&format!("failed to create encoded metadata from file: {}", e))
})
}
};
let _prof_timer = tcx.sess.prof.generic_activity("write_crate_metadata");
let need_metadata_file = tcx.sess.opts.output_types.contains_key(&OutputType::Metadata);
let (metadata_filename, metadata_tmpdir) = if need_metadata_file {
if let Err(e) = non_durable_rename(&metadata_filename, &out_filename) {
tcx.sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
}
if tcx.sess.opts.json_artifact_notifications {
tcx.sess
.parse_sess
.span_diagnostic
.emit_artifact_notification(&out_filename, "metadata");
}
(out_filename, None)
} else {
(metadata_filename, Some(metadata_tmpdir))
};
let metadata =
EncodedMetadata::from_path(metadata_filename, metadata_tmpdir).unwrap_or_else(|e| {
tcx.sess.fatal(&format!("failed to create encoded metadata from file: {}", e))
});
let need_metadata_module = metadata_kind == MetadataKind::Compressed;
(metadata, need_metadata_module)

View File

@ -2163,11 +2163,6 @@ impl EncodedMetadata {
Ok(Self { mmap, _temp_dir: temp_dir })
}
#[inline]
pub fn empty() -> Self {
Self { mmap: None, _temp_dir: None }
}
#[inline]
pub fn raw_data(&self) -> &[u8] {
self.mmap.as_ref().map(|mmap| mmap.as_ref()).unwrap_or_default()