Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6c585fc

Browse files
Clean up render_assoc_items_inner a bit
1 parent 7a42ca9 commit 6c585fc

File tree

1 file changed

+52
-54
lines changed
  • src/librustdoc/html/render

1 file changed

+52
-54
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ fn short_item_info(
613613

614614
// Render the list of items inside one of the sections "Trait Implementations",
615615
// "Auto Trait Implementations," "Blanket Trait Implementations" (on struct/enum pages).
616-
fn render_impls(
616+
pub(crate) fn render_impls(
617617
cx: &mut Context<'_>,
618618
w: &mut Buffer,
619619
impls: &[&&Impl],
@@ -1025,6 +1025,47 @@ impl<'a> AssocItemLink<'a> {
10251025
}
10261026
}
10271027

1028+
fn write_impl_section_heading(w: &mut Buffer, title: &str, id: &str) {
1029+
write!(
1030+
w,
1031+
"<h2 id=\"{id}\" class=\"small-section-header\">\
1032+
{title}\
1033+
<a href=\"#{id}\" class=\"anchor\"></a>\
1034+
</h2>"
1035+
);
1036+
}
1037+
1038+
pub(crate) fn render_all_impls(
1039+
w: &mut Buffer,
1040+
cx: &mut Context<'_>,
1041+
containing_item: &clean::Item,
1042+
concrete: &[&&Impl],
1043+
synthetic: &[&&Impl],
1044+
blanket_impl: &[&&Impl],
1045+
) {
1046+
let mut impls = Buffer::empty_from(w);
1047+
render_impls(cx, &mut impls, concrete, containing_item, true);
1048+
let impls = impls.into_inner();
1049+
if !impls.is_empty() {
1050+
write_impl_section_heading(w, "Trait Implementations", "trait-implementations");
1051+
write!(w, "<div id=\"trait-implementations-list\">{}</div>", impls);
1052+
}
1053+
1054+
if !synthetic.is_empty() {
1055+
write_impl_section_heading(w, "Auto Trait Implementations", "synthetic-implementations");
1056+
w.write_str("<div id=\"synthetic-implementations-list\">");
1057+
render_impls(cx, w, synthetic, containing_item, false);
1058+
w.write_str("</div>");
1059+
}
1060+
1061+
if !blanket_impl.is_empty() {
1062+
write_impl_section_heading(w, "Blanket Implementations", "blanket-implementations");
1063+
w.write_str("<div id=\"blanket-implementations-list\">");
1064+
render_impls(cx, w, blanket_impl, containing_item, false);
1065+
w.write_str("</div>");
1066+
}
1067+
}
1068+
10281069
fn render_assoc_items(
10291070
w: &mut Buffer,
10301071
cx: &mut Context<'_>,
@@ -1054,12 +1095,7 @@ fn render_assoc_items_inner(
10541095
let mut tmp_buf = Buffer::empty_from(w);
10551096
let (render_mode, id) = match what {
10561097
AssocItemRender::All => {
1057-
tmp_buf.write_str(
1058-
"<h2 id=\"implementations\" class=\"small-section-header\">\
1059-
Implementations\
1060-
<a href=\"#implementations\" class=\"anchor\"></a>\
1061-
</h2>",
1062-
);
1098+
write_impl_section_heading(&mut tmp_buf, "Implementations", "implementations");
10631099
(RenderMode::Normal, "implementations-list".to_owned())
10641100
}
10651101
AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => {
@@ -1068,15 +1104,14 @@ fn render_assoc_items_inner(
10681104
if let Some(def_id) = type_.def_id(cx.cache()) {
10691105
cx.deref_id_map.insert(def_id, id.clone());
10701106
}
1071-
write!(
1072-
tmp_buf,
1073-
"<h2 id=\"{id}\" class=\"small-section-header\">\
1074-
<span>Methods from {trait_}&lt;Target = {type_}&gt;</span>\
1075-
<a href=\"#{id}\" class=\"anchor\"></a>\
1076-
</h2>",
1077-
id = id,
1078-
trait_ = trait_.print(cx),
1079-
type_ = type_.print(cx),
1107+
write_impl_section_heading(
1108+
&mut tmp_buf,
1109+
&format!(
1110+
"<span>Methods from {trait_}&lt;Target = {type_}&gt;</span>",
1111+
trait_ = trait_.print(cx),
1112+
type_ = type_.print(cx),
1113+
),
1114+
&id,
10801115
);
10811116
(RenderMode::ForDeref { mut_: deref_mut_ }, cx.derive_id(id))
10821117
}
@@ -1128,44 +1163,7 @@ fn render_assoc_items_inner(
11281163
let (blanket_impl, concrete): (Vec<&&Impl>, _) =
11291164
concrete.into_iter().partition(|t| t.inner_impl().kind.is_blanket());
11301165

1131-
let mut impls = Buffer::empty_from(w);
1132-
render_impls(cx, &mut impls, &concrete, containing_item, true);
1133-
let impls = impls.into_inner();
1134-
if !impls.is_empty() {
1135-
write!(
1136-
w,
1137-
"<h2 id=\"trait-implementations\" class=\"small-section-header\">\
1138-
Trait Implementations\
1139-
<a href=\"#trait-implementations\" class=\"anchor\"></a>\
1140-
</h2>\
1141-
<div id=\"trait-implementations-list\">{}</div>",
1142-
impls
1143-
);
1144-
}
1145-
1146-
if !synthetic.is_empty() {
1147-
w.write_str(
1148-
"<h2 id=\"synthetic-implementations\" class=\"small-section-header\">\
1149-
Auto Trait Implementations\
1150-
<a href=\"#synthetic-implementations\" class=\"anchor\"></a>\
1151-
</h2>\
1152-
<div id=\"synthetic-implementations-list\">",
1153-
);
1154-
render_impls(cx, w, &synthetic, containing_item, false);
1155-
w.write_str("</div>");
1156-
}
1157-
1158-
if !blanket_impl.is_empty() {
1159-
w.write_str(
1160-
"<h2 id=\"blanket-implementations\" class=\"small-section-header\">\
1161-
Blanket Implementations\
1162-
<a href=\"#blanket-implementations\" class=\"anchor\"></a>\
1163-
</h2>\
1164-
<div id=\"blanket-implementations-list\">",
1165-
);
1166-
render_impls(cx, w, &blanket_impl, containing_item, false);
1167-
w.write_str("</div>");
1168-
}
1166+
render_all_impls(w, cx, containing_item, &concrete, &synthetic, &blanket_impl);
11691167
}
11701168
}
11711169

0 commit comments

Comments
 (0)