Skip to content

Commit 53f1bed

Browse files
committed
Use UrlPartsBuilder and remove join_with_slash
1 parent 6b19cf9 commit 53f1bed

File tree

2 files changed

+16
-36
lines changed

2 files changed

+16
-36
lines changed

src/librustdoc/html/format.rs

-22
Original file line numberDiff line numberDiff line change
@@ -503,28 +503,6 @@ crate enum HrefError {
503503
NotInExternalCache,
504504
}
505505

506-
// This mostly works with sequences of symbols, but sometimes the first item
507-
// comes from a string, and in that case we want to trim any trailing `/`.
508-
// `syms` can be empty.
509-
crate fn join_with_slash(first: Option<&str>, syms: &[Symbol]) -> String {
510-
// 64 bytes covers 99.9%+ of cases.
511-
let mut s = String::with_capacity(64);
512-
if let Some(first) = first {
513-
s.push_str(first.trim_end_matches('/'));
514-
if !syms.is_empty() {
515-
s.push('/');
516-
}
517-
}
518-
if !syms.is_empty() {
519-
s.push_str(&syms[0].as_str());
520-
for sym in &syms[1..] {
521-
s.push('/');
522-
s.push_str(&sym.as_str());
523-
}
524-
}
525-
s
526-
}
527-
528506
// Panics if `syms` is empty.
529507
crate fn join_with_double_colon(syms: &[Symbol]) -> String {
530508
// 64 bytes covers 99.9%+ of cases.

src/librustdoc/html/render/print_item.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ use crate::formats::item_type::ItemType;
2626
use crate::formats::{AssocItemRender, Impl, RenderMode};
2727
use crate::html::escape::Escape;
2828
use crate::html::format::{
29-
join_with_double_colon, join_with_slash, print_abi_with_space, print_constness_with_space,
30-
print_where_clause, Buffer, PrintWithSpace,
29+
join_with_double_colon, print_abi_with_space, print_constness_with_space, print_where_clause,
30+
Buffer, PrintWithSpace,
3131
};
3232
use crate::html::highlight;
3333
use crate::html::layout::Page;
3434
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
35+
use crate::html::url_parts_builder::UrlPartsBuilder;
3536

3637
use askama::Template;
3738

@@ -854,20 +855,21 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
854855
}
855856
}
856857

858+
let mut js_src_path: UrlPartsBuilder = std::iter::repeat("..")
859+
.take(cx.current.len())
860+
.chain(std::iter::once("implementors"))
861+
.collect();
862+
if it.def_id.is_local() {
863+
js_src_path.extend(cx.current.iter().copied());
864+
} else {
865+
let (ref path, _) = cache.external_paths[&it.def_id.expect_def_id()];
866+
js_src_path.extend(path[..path.len() - 1].iter().copied());
867+
}
868+
js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), it.name.unwrap()));
857869
write!(
858870
w,
859-
"<script type=\"text/javascript\" \
860-
src=\"{root_path}/implementors/{path}/{ty}.{name}.js\" async>\
861-
</script>",
862-
root_path = vec![".."; cx.current.len()].join("/"),
863-
path = if it.def_id.is_local() {
864-
join_with_slash(None, &cx.current)
865-
} else {
866-
let (ref path, _) = cache.external_paths[&it.def_id.expect_def_id()];
867-
join_with_slash(None, &path[..path.len() - 1])
868-
},
869-
ty = it.type_(),
870-
name = it.name.unwrap()
871+
"<script type=\"text/javascript\" src=\"{src}\" async></script>",
872+
src = js_src_path.finish(),
871873
);
872874
}
873875

0 commit comments

Comments
 (0)