]> git.lizzy.rs Git - rust.git/commitdiff
* Update aliases data struct from HashMap to BTreeMap to have more deterministic...
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 13 May 2020 15:03:37 +0000 (17:03 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 14 May 2020 09:36:02 +0000 (11:36 +0200)
  * Update Javascript to take this change into account
* Update CrateData::aliases field to take a reference instead (it allowed to remove a conversion loop)

src/librustdoc/html/render/cache.rs
src/librustdoc/html/static/main.js

index b8d97c2ac503bae863fbd436954e09b2a8455f61..57d385de32096d88e11c5682a94ffe6213b05716 100644 (file)
@@ -120,7 +120,7 @@ pub enum ExternalLocation {
 
     /// 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 {
@@ -331,7 +331,7 @@ fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
                         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);
                         }
                     }
@@ -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);
             }
         }
@@ -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,
@@ -614,7 +611,8 @@ struct CrateData<'a> {
         //
         // 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
@@ -625,7 +623,7 @@ struct CrateData<'a> {
             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.
index 7592331dd656d5197a4221aa88f213601820b455..9b498d66249e2fde35175429162a10f83da2690f 100644 (file)
@@ -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);
                         }