Skip to content

Commit 38f6c07

Browse files
Improve code readability for sidebar links
1 parent 51a993f commit 38f6c07

File tree

1 file changed

+48
-16
lines changed
  • src/librustdoc/html/render

1 file changed

+48
-16
lines changed

src/librustdoc/html/render/mod.rs

+48-16
Original file line numberDiff line numberDiff line change
@@ -1811,23 +1811,53 @@ fn get_next_url(used_links: &mut FxHashSet<String>, url: String) -> String {
18111811
format!("{}-{}", url, add)
18121812
}
18131813

1814+
struct SidebarLink {
1815+
name: Symbol,
1816+
url: String,
1817+
}
1818+
1819+
impl fmt::Display for SidebarLink {
1820+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1821+
write!(f, "<a href=\"#{}\">{}</a>", self.url, self.name)
1822+
}
1823+
}
1824+
1825+
impl PartialEq for SidebarLink {
1826+
fn eq(&self, other: &Self) -> bool {
1827+
self.url == other.url
1828+
}
1829+
}
1830+
1831+
impl Eq for SidebarLink {}
1832+
1833+
impl PartialOrd for SidebarLink {
1834+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
1835+
Some(self.cmp(other))
1836+
}
1837+
}
1838+
1839+
impl Ord for SidebarLink {
1840+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
1841+
self.url.cmp(&other.url)
1842+
}
1843+
}
1844+
18141845
fn get_methods(
18151846
i: &clean::Impl,
18161847
for_deref: bool,
18171848
used_links: &mut FxHashSet<String>,
18181849
deref_mut: bool,
18191850
tcx: TyCtxt<'_>,
1820-
) -> Vec<String> {
1851+
) -> Vec<SidebarLink> {
18211852
i.items
18221853
.iter()
18231854
.filter_map(|item| match item.name {
1824-
Some(ref name) if !name.is_empty() && item.is_method() => {
1855+
Some(name) if !name.is_empty() && item.is_method() => {
18251856
if !for_deref || should_render_item(item, deref_mut, tcx) {
1826-
Some(format!(
1827-
"<a href=\"#{}\">{}</a>",
1828-
get_next_url(used_links, format!("method.{}", name)),
1829-
name
1830-
))
1857+
Some(SidebarLink {
1858+
name,
1859+
url: get_next_url(used_links, format!("method.{}", name)),
1860+
})
18311861
} else {
18321862
None
18331863
}
@@ -1837,15 +1867,17 @@ fn get_methods(
18371867
.collect::<Vec<_>>()
18381868
}
18391869

1840-
fn get_associated_constants(i: &clean::Impl, used_links: &mut FxHashSet<String>) -> Vec<String> {
1870+
fn get_associated_constants(
1871+
i: &clean::Impl,
1872+
used_links: &mut FxHashSet<String>,
1873+
) -> Vec<SidebarLink> {
18411874
i.items
18421875
.iter()
18431876
.filter_map(|item| match item.name {
1844-
Some(ref name) if !name.is_empty() && item.is_associated_const() => Some(format!(
1845-
"<a href=\"#{}\">{}</a>",
1846-
get_next_url(used_links, format!("associatedconstant.{}", name)),
1847-
name
1848-
)),
1877+
Some(name) if !name.is_empty() && item.is_associated_const() => Some(SidebarLink {
1878+
name,
1879+
url: get_next_url(used_links, format!("associatedconstant.{}", name)),
1880+
}),
18491881
_ => None,
18501882
})
18511883
.collect::<Vec<_>>()
@@ -1910,7 +1942,7 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
19101942
<div class=\"sidebar-links\">",
19111943
);
19121944
for line in assoc_consts {
1913-
out.push_str(&line);
1945+
write!(out, "{}", line);
19141946
}
19151947
out.push_str("</div>");
19161948
}
@@ -1928,7 +1960,7 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
19281960
<div class=\"sidebar-links\">",
19291961
);
19301962
for line in methods {
1931-
out.push_str(&line);
1963+
write!(out, "{}", line);
19321964
}
19331965
out.push_str("</div>");
19341966
}
@@ -2063,7 +2095,7 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
20632095
ret.sort();
20642096
out.push_str("<div class=\"sidebar-links\">");
20652097
for link in ret {
2066-
out.push_str(&link);
2098+
write!(out, "{}", link);
20672099
}
20682100
out.push_str("</div>");
20692101
}

0 commit comments

Comments
 (0)