Skip to content

Commit 0753215

Browse files
committed
make write_section_heading return impl fmt::Display
1 parent 932c880 commit 0753215

File tree

2 files changed

+214
-106
lines changed

2 files changed

+214
-106
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,29 +1208,29 @@ impl<'a> AssocItemLink<'a> {
12081208
}
12091209
}
12101210

1211-
pub fn write_section_heading(
1212-
w: &mut impl fmt::Write,
1213-
title: &str,
1214-
id: &str,
1215-
extra_class: Option<&str>,
1216-
extra: impl fmt::Display,
1217-
) {
1218-
let (extra_class, whitespace) = match extra_class {
1219-
Some(extra) => (extra, " "),
1220-
None => ("", ""),
1221-
};
1222-
write!(
1223-
w,
1224-
"<h2 id=\"{id}\" class=\"{extra_class}{whitespace}section-header\">\
1211+
pub fn write_section_heading<'a>(
1212+
title: &'a str,
1213+
id: &'a str,
1214+
extra_class: Option<&'a str>,
1215+
extra: impl fmt::Display + 'a,
1216+
) -> impl fmt::Display + 'a {
1217+
fmt::from_fn(move |w| {
1218+
let (extra_class, whitespace) = match extra_class {
1219+
Some(extra) => (extra, " "),
1220+
None => ("", ""),
1221+
};
1222+
write!(
1223+
w,
1224+
"<h2 id=\"{id}\" class=\"{extra_class}{whitespace}section-header\">\
12251225
{title}\
12261226
<a href=\"#{id}\" class=\"anchor\">§</a>\
12271227
</h2>{extra}",
1228-
)
1229-
.unwrap();
1228+
)
1229+
})
12301230
}
12311231

1232-
fn write_impl_section_heading(w: &mut impl fmt::Write, title: &str, id: &str) {
1233-
write_section_heading(w, title, id, None, "")
1232+
fn write_impl_section_heading<'a>(title: &'a str, id: &'a str) -> impl fmt::Display + 'a {
1233+
write_section_heading(title, id, None, "")
12341234
}
12351235

12361236
pub(crate) fn render_all_impls(
@@ -1247,24 +1247,32 @@ pub(crate) fn render_all_impls(
12471247
buf
12481248
};
12491249
if !impls.is_empty() {
1250-
write_impl_section_heading(&mut w, "Trait Implementations", "trait-implementations");
1251-
write!(w, "<div id=\"trait-implementations-list\">{impls}</div>").unwrap();
1250+
write!(
1251+
w,
1252+
"{}<div id=\"trait-implementations-list\">{impls}</div>",
1253+
write_impl_section_heading("Trait Implementations", "trait-implementations")
1254+
)
1255+
.unwrap();
12521256
}
12531257

12541258
if !synthetic.is_empty() {
1255-
write_impl_section_heading(
1256-
&mut w,
1257-
"Auto Trait Implementations",
1258-
"synthetic-implementations",
1259-
);
1260-
w.write_str("<div id=\"synthetic-implementations-list\">").unwrap();
1259+
write!(
1260+
w,
1261+
"{}<div id=\"synthetic-implementations-list\">",
1262+
write_impl_section_heading("Auto Trait Implementations", "synthetic-implementations",)
1263+
)
1264+
.unwrap();
12611265
render_impls(cx, &mut w, synthetic, containing_item, false);
12621266
w.write_str("</div>").unwrap();
12631267
}
12641268

12651269
if !blanket_impl.is_empty() {
1266-
write_impl_section_heading(&mut w, "Blanket Implementations", "blanket-implementations");
1267-
w.write_str("<div id=\"blanket-implementations-list\">").unwrap();
1270+
write!(
1271+
w,
1272+
"{}<div id=\"blanket-implementations-list\">",
1273+
write_impl_section_heading("Blanket Implementations", "blanket-implementations")
1274+
)
1275+
.unwrap();
12681276
render_impls(cx, &mut w, blanket_impl, containing_item, false);
12691277
w.write_str("</div>").unwrap();
12701278
}
@@ -1301,25 +1309,34 @@ fn render_assoc_items_inner(
13011309
let mut tmp_buf = String::new();
13021310
let (render_mode, id, class_html) = match what {
13031311
AssocItemRender::All => {
1304-
write_impl_section_heading(&mut tmp_buf, "Implementations", "implementations");
1312+
write_str(
1313+
&mut tmp_buf,
1314+
format_args!(
1315+
"{}",
1316+
write_impl_section_heading("Implementations", "implementations")
1317+
),
1318+
);
13051319
(RenderMode::Normal, "implementations-list".to_owned(), "")
13061320
}
13071321
AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => {
13081322
let id =
13091323
cx.derive_id(small_url_encode(format!("deref-methods-{:#}", type_.print(cx))));
13101324
let derived_id = cx.derive_id(&id);
1311-
tmp_buf.push_str("<details class=\"toggle big-toggle\" open><summary>");
13121325
close_tags.push("</details>");
1313-
write_impl_section_heading(
1326+
write_str(
13141327
&mut tmp_buf,
1315-
&format!(
1316-
"<span>Methods from {trait_}&lt;Target = {type_}&gt;</span>",
1317-
trait_ = trait_.print(cx),
1318-
type_ = type_.print(cx),
1328+
format_args!(
1329+
"<details class=\"toggle big-toggle\" open><summary>{}</summary>",
1330+
write_impl_section_heading(
1331+
&format!(
1332+
"<span>Methods from {trait_}&lt;Target = {type_}&gt;</span>",
1333+
trait_ = trait_.print(cx),
1334+
type_ = type_.print(cx),
1335+
),
1336+
&id,
1337+
)
13191338
),
1320-
&id,
13211339
);
1322-
tmp_buf.push_str("</summary>");
13231340
if let Some(def_id) = type_.def_id(cx.cache()) {
13241341
cx.deref_id_map.borrow_mut().insert(def_id, id);
13251342
}

0 commit comments

Comments
 (0)