From d6ec686d379efca7296d6c53abe7968bdfae5723 Mon Sep 17 00:00:00 2001 From: Oliver Middleton Date: Thu, 1 Dec 2016 13:10:49 +0000 Subject: [PATCH] rustdoc: Sort lines in search index and implementors js This means the files are generated deterministically even with rustdoc running in parallel. --- src/librustdoc/html/render.rs | 50 ++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index cbf93662811..57fb0aa3f7a 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -40,7 +40,7 @@ use std::collections::BTreeMap; use std::default::Default; use std::error; -use std::fmt::{self, Display, Formatter}; +use std::fmt::{self, Display, Formatter, Write as FmtWrite}; use std::fs::{self, File, OpenOptions}; use std::io::prelude::*; use std::io::{self, BufWriter, BufReader}; @@ -718,10 +718,13 @@ fn collect(path: &Path, krate: &str, // Update the search index let dst = cx.dst.join("search-index.js"); - let all_indexes = try_err!(collect(&dst, &krate.name, "searchIndex"), &dst); + let mut all_indexes = try_err!(collect(&dst, &krate.name, "searchIndex"), &dst); + all_indexes.push(search_index); + // Sort the indexes by crate so the file will be generated identically even + // with rustdoc running in parallel. + all_indexes.sort(); let mut w = try_err!(File::create(&dst), &dst); try_err!(writeln!(&mut w, "var searchIndex = {{}};"), &dst); - try_err!(writeln!(&mut w, "{}", search_index), &dst); for index in &all_indexes { try_err!(writeln!(&mut w, "{}", *index), &dst); } @@ -729,7 +732,6 @@ fn collect(path: &Path, krate: &str, // Update the list of all implementors for traits let dst = cx.dst.join("implementors"); - try_err!(mkdir(&dst), &dst); for (&did, imps) in &cache.implementors { // Private modules can leak through to this phase of rustdoc, which // could contain implementations for otherwise private types. In some @@ -746,37 +748,37 @@ fn collect(path: &Path, krate: &str, } }; + let mut implementors = format!(r#"implementors["{}"] = ["#, krate.name); + for imp in imps { + // If the trait and implementation are in the same crate, then + // there's no need to emit information about it (there's inlining + // going on). If they're in different crates then the crate defining + // the trait will be interested in our implementation. + if imp.def_id.krate == did.krate { continue } + write!(implementors, r#""{}","#, imp.impl_).unwrap(); + } + implementors.push_str("];"); + let mut mydst = dst.clone(); for part in &remote_path[..remote_path.len() - 1] { mydst.push(part); - try_err!(mkdir(&mydst), &mydst); } + try_err!(fs::create_dir_all(&mydst), &mydst); mydst.push(&format!("{}.{}.js", remote_item_type.css_class(), remote_path[remote_path.len() - 1])); - let all_implementors = try_err!(collect(&mydst, &krate.name, - "implementors"), - &mydst); - try_err!(mkdir(mydst.parent().unwrap()), - &mydst.parent().unwrap().to_path_buf()); - let mut f = BufWriter::new(try_err!(File::create(&mydst), &mydst)); - try_err!(writeln!(&mut f, "(function() {{var implementors = {{}};"), &mydst); + let mut all_implementors = try_err!(collect(&mydst, &krate.name, "implementors"), &mydst); + all_implementors.push(implementors); + // Sort the implementors by crate so the file will be generated + // identically even with rustdoc running in parallel. + all_implementors.sort(); + let mut f = try_err!(File::create(&mydst), &mydst); + try_err!(writeln!(&mut f, "(function() {{var implementors = {{}};"), &mydst); for implementor in &all_implementors { - try_err!(write!(&mut f, "{}", *implementor), &mydst); - } - - try_err!(write!(&mut f, r#"implementors["{}"] = ["#, krate.name), &mydst); - for imp in imps { - // If the trait and implementation are in the same crate, then - // there's no need to emit information about it (there's inlining - // going on). If they're in different crates then the crate defining - // the trait will be interested in our implementation. - if imp.def_id.krate == did.krate { continue } - try_err!(write!(&mut f, r#""{}","#, imp.impl_), &mydst); + try_err!(writeln!(&mut f, "{}", *implementor), &mydst); } - try_err!(writeln!(&mut f, r"];"), &mydst); try_err!(writeln!(&mut f, "{}", r" if (window.register_implementors) { window.register_implementors(implementors); -- 2.44.0