Skip to content

Commit

Permalink
Use UrlPartsBuilder and remove join_with_slash
Browse files Browse the repository at this point in the history
  • Loading branch information
camelid committed Jan 14, 2022
1 parent 6b19cf9 commit 53f1bed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 36 deletions.
22 changes: 0 additions & 22 deletions src/librustdoc/html/format.rs
Expand Up @@ -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.
Expand Down
30 changes: 16 additions & 14 deletions src/librustdoc/html/render/print_item.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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(),
);
}

Expand Down

0 comments on commit 53f1bed

Please sign in to comment.