Skip to content

Commit

Permalink
* Update aliases data struct from HashMap to BTreeMap to have more de…
Browse files Browse the repository at this point in the history
…terministic results

  * Update Javascript to take this change into account
* Update CrateData::aliases field to take a reference instead (it allowed to remove a conversion loop)
  • Loading branch information
GuillaumeGomez committed May 14, 2020
1 parent c4d9318 commit e17ac66
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
16 changes: 7 additions & 9 deletions src/librustdoc/html/render/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ crate struct Cache {

/// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
/// we need the alias element to have an array of items.
pub(super) aliases: FxHashMap<String, Vec<usize>>,
pub(super) aliases: BTreeMap<String, Vec<usize>>,
}

impl Cache {
Expand Down Expand Up @@ -331,7 +331,7 @@ impl DocFolder for Cache {
for alias in item.attrs.get_doc_aliases() {
self.aliases
.entry(alias.to_lowercase())
.or_insert(Vec::with_capacity(1))
.or_insert(Vec::new())
.push(self.search_index.len() - 1);
}
}
Expand Down Expand Up @@ -553,10 +553,10 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
parent_idx: None,
search_type: get_index_search_type(&item),
});
for alias in item.attrs.get_doc_aliases().into_iter() {
for alias in item.attrs.get_doc_aliases() {
aliases
.entry(alias.to_lowercase())
.or_insert(Vec::with_capacity(1))
.or_insert(Vec::new())
.push(search_index.len() - 1);
}
}
Expand Down Expand Up @@ -600,9 +600,6 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
.map(|module| shorten(plain_summary_line(module.doc_value())))
.unwrap_or(String::new());

let crate_aliases =
aliases.iter().map(|(k, values)| (k.clone(), values.clone())).collect::<Vec<_>>();

#[derive(Serialize)]
struct CrateData<'a> {
doc: String,
Expand All @@ -614,7 +611,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
//
// To be noted: the `usize` elements are indexes to `items`.
#[serde(rename = "a")]
aliases: Option<Vec<(String, Vec<usize>)>>,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
aliases: &'a BTreeMap<String, Vec<usize>>,
}

// Collect the index into a string
Expand All @@ -625,7 +623,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
doc: crate_doc,
items: crate_items,
paths: crate_paths,
aliases: if crate_aliases.is_empty() { None } else { Some(crate_aliases) },
aliases,
})
.expect("failed serde conversion")
// All these `replace` calls are because we have to go through JS string for JSON content.
Expand Down
7 changes: 4 additions & 3 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1781,12 +1781,13 @@ function getSearchElement() {
if (aliases) {
ALIASES[crate] = {};
var j, local_aliases;
for (i = 0; i < aliases.length; ++i) {
var alias_name = aliases[i][0];
for (var alias_name in aliases) {
if (!aliases.hasOwnProperty(alias_name)) { continue; }

if (!ALIASES[crate].hasOwnProperty(alias_name)) {
ALIASES[crate][alias_name] = [];
}
local_aliases = aliases[i][1];
local_aliases = aliases[alias_name];
for (j = 0; j < local_aliases.length; ++j) {
ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
}
Expand Down

0 comments on commit e17ac66

Please sign in to comment.