diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-08-17 18:18:19 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-17 18:18:19 +0200 |
| commit | 1a95a5f2dbffc3f48e0515fd9bdfa77f2921872b (patch) | |
| tree | bfc55d0c66c66b7110b7e88d09cb27fac3c5a7fe | |
| parent | ddbbda47ebf2a69008f48b9c5e83b615e0e00545 (diff) | |
| parent | 321d40f060bccd79ea6878abc67bda5d10b65e5f (diff) | |
| download | rust-1a95a5f2dbffc3f48e0515fd9bdfa77f2921872b.tar.gz rust-1a95a5f2dbffc3f48e0515fd9bdfa77f2921872b.zip | |
Rollup merge of #129191 - aDotInTheVoid:rdj-serial-cleanup, r=GuillaumeGomez
rustdoc-json: Clean up serialization and printing. Somewhat a followup to #128963, but makes sense regardless. - Renames `out_path` to `out_dir` because it's not the path to the JSON, but the directory - Also adds a comment explaining `None` - Renames `write` to `serialize_and_write` because it does both. - Also renames the self-profile activity name to be clear this measures both IO cost and serialization CPU cost - Expands the timer to cover flushing - Renames `output` to `output_crate`, to emphasize it's the contents, not the `--output` flag. r? `@GuillaumeGomez`
| -rw-r--r-- | src/librustdoc/json/mod.rs | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index a424faaf999..e2860292aa3 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -39,8 +39,10 @@ pub(crate) struct JsonRenderer<'tcx> { /// A mapping of IDs that contains all local items for this crate which gets output as a top /// level field of the JSON blob. index: Rc<RefCell<FxHashMap<types::Id, types::Item>>>, - /// The directory where the blob will be written to. - out_path: Option<PathBuf>, + /// The directory where the JSON blob should be written to. + /// + /// If this is `None`, the blob will be printed to `stdout` instead. + out_dir: Option<PathBuf>, cache: Rc<Cache>, imported_items: DefIdSet, } @@ -101,18 +103,20 @@ impl<'tcx> JsonRenderer<'tcx> { .unwrap_or_default() } - fn write<T: Write>( + fn serialize_and_write<T: Write>( &self, - output: types::Crate, + output_crate: types::Crate, mut writer: BufWriter<T>, path: &str, ) -> Result<(), Error> { - self.tcx - .sess - .time("rustdoc_json_serialization", || serde_json::ser::to_writer(&mut writer, &output)) - .unwrap(); - try_err!(writer.flush(), path); - Ok(()) + self.sess().time("rustdoc_json_serialize_and_write", || { + try_err!( + serde_json::ser::to_writer(&mut writer, &output_crate).map_err(|e| e.to_string()), + path + ); + try_err!(writer.flush(), path); + Ok(()) + }) } } @@ -137,7 +141,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { JsonRenderer { tcx, index: Rc::new(RefCell::new(FxHashMap::default())), - out_path: if options.output_to_stdout { None } else { Some(options.output) }, + out_dir: if options.output_to_stdout { None } else { Some(options.output) }, cache: Rc::new(cache), imported_items, }, @@ -237,7 +241,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { let index = (*self.index).clone().into_inner(); debug!("Constructing Output"); - let output = types::Crate { + let output_crate = types::Crate { root: types::Id(format!("0:0:{}", e.name(self.tcx).as_u32())), crate_version: self.cache.crate_version.clone(), includes_private: self.cache.document_private, @@ -278,20 +282,20 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { .collect(), format_version: types::FORMAT_VERSION, }; - if let Some(ref out_path) = self.out_path { - let out_dir = out_path.clone(); + if let Some(ref out_dir) = self.out_dir { try_err!(create_dir_all(&out_dir), out_dir); - let mut p = out_dir; - p.push(output.index.get(&output.root).unwrap().name.clone().unwrap()); + let mut p = out_dir.clone(); + p.push(output_crate.index.get(&output_crate.root).unwrap().name.clone().unwrap()); p.set_extension("json"); - self.write( - output, + + self.serialize_and_write( + output_crate, BufWriter::new(try_err!(File::create(&p), p)), &p.display().to_string(), ) } else { - self.write(output, BufWriter::new(stdout()), "<stdout>") + self.serialize_and_write(output_crate, BufWriter::new(stdout().lock()), "<stdout>") } } |
