about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNoah Lev <camelidcamel@gmail.com>2022-01-10 12:18:26 -0800
committerNoah Lev <camelidcamel@gmail.com>2022-01-14 12:05:35 -0800
commit53f1bed83ab3124ddc26c8e1e28666ae8a0f1842 (patch)
treeda8c85cee098349aa687a0b2a852096c40a7e012
parent6b19cf9f7438c1ef715e5fb9a4dff508f8e74dae (diff)
downloadrust-53f1bed83ab3124ddc26c8e1e28666ae8a0f1842.tar.gz
rust-53f1bed83ab3124ddc26c8e1e28666ae8a0f1842.zip
Use UrlPartsBuilder and remove `join_with_slash`
-rw-r--r--src/librustdoc/html/format.rs22
-rw-r--r--src/librustdoc/html/render/print_item.rs30
2 files changed, 16 insertions, 36 deletions
diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs
index b6cb0fbc76a..342c15855d2 100644
--- a/src/librustdoc/html/format.rs
+++ b/src/librustdoc/html/format.rs
@@ -503,28 +503,6 @@ crate enum HrefError {
     NotInExternalCache,
 }
 
-// This mostly works with sequences of symbols, but sometimes the first item
-// comes from a string, and in that case we want to trim any trailing `/`.
-// `syms` can be empty.
-crate fn join_with_slash(first: Option<&str>, syms: &[Symbol]) -> String {
-    // 64 bytes covers 99.9%+ of cases.
-    let mut s = String::with_capacity(64);
-    if let Some(first) = first {
-        s.push_str(first.trim_end_matches('/'));
-        if !syms.is_empty() {
-            s.push('/');
-        }
-    }
-    if !syms.is_empty() {
-        s.push_str(&syms[0].as_str());
-        for sym in &syms[1..] {
-            s.push('/');
-            s.push_str(&sym.as_str());
-        }
-    }
-    s
-}
-
 // Panics if `syms` is empty.
 crate fn join_with_double_colon(syms: &[Symbol]) -> String {
     // 64 bytes covers 99.9%+ of cases.
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index dacaeac78f2..eda637acfc5 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -26,12 +26,13 @@ use crate::formats::item_type::ItemType;
 use crate::formats::{AssocItemRender, Impl, RenderMode};
 use crate::html::escape::Escape;
 use crate::html::format::{
-    join_with_double_colon, join_with_slash, print_abi_with_space, print_constness_with_space,
-    print_where_clause, Buffer, PrintWithSpace,
+    join_with_double_colon, print_abi_with_space, print_constness_with_space, print_where_clause,
+    Buffer, PrintWithSpace,
 };
 use crate::html::highlight;
 use crate::html::layout::Page;
 use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
+use crate::html::url_parts_builder::UrlPartsBuilder;
 
 use askama::Template;
 
@@ -854,20 +855,21 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
         }
     }
 
+    let mut js_src_path: UrlPartsBuilder = std::iter::repeat("..")
+        .take(cx.current.len())
+        .chain(std::iter::once("implementors"))
+        .collect();
+    if it.def_id.is_local() {
+        js_src_path.extend(cx.current.iter().copied());
+    } else {
+        let (ref path, _) = cache.external_paths[&it.def_id.expect_def_id()];
+        js_src_path.extend(path[..path.len() - 1].iter().copied());
+    }
+    js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), it.name.unwrap()));
     write!(
         w,
-        "<script type=\"text/javascript\" \
-                 src=\"{root_path}/implementors/{path}/{ty}.{name}.js\" async>\
-         </script>",
-        root_path = vec![".."; cx.current.len()].join("/"),
-        path = if it.def_id.is_local() {
-            join_with_slash(None, &cx.current)
-        } else {
-            let (ref path, _) = cache.external_paths[&it.def_id.expect_def_id()];
-            join_with_slash(None, &path[..path.len() - 1])
-        },
-        ty = it.type_(),
-        name = it.name.unwrap()
+        "<script type=\"text/javascript\" src=\"{src}\" async></script>",
+        src = js_src_path.finish(),
     );
 }