Skip to content

Commit bae1021

Browse files
committed
make assoc_type return impl fmt::Display
1 parent f614260 commit bae1021

File tree

1 file changed

+107
-82
lines changed
  • src/librustdoc/html/render

1 file changed

+107
-82
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 107 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -919,35 +919,34 @@ fn assoc_const<'a, 'tcx>(
919919
})
920920
}
921921

922-
fn assoc_type(
923-
w: &mut String,
924-
it: &clean::Item,
925-
generics: &clean::Generics,
926-
bounds: &[clean::GenericBound],
927-
default: Option<&clean::Type>,
928-
link: AssocItemLink<'_>,
922+
fn assoc_type<'a, 'tcx>(
923+
it: &'a clean::Item,
924+
generics: &'a clean::Generics,
925+
bounds: &'a [clean::GenericBound],
926+
default: Option<&'a clean::Type>,
927+
link: AssocItemLink<'a>,
929928
indent: usize,
930-
cx: &Context<'_>,
931-
) {
932-
write_str(
933-
w,
934-
format_args!(
929+
cx: &'a Context<'tcx>,
930+
) -> impl fmt::Display + 'a + Captures<'tcx> {
931+
fmt::from_fn(move |w| {
932+
write!(
933+
w,
935934
"{indent}{vis}type <a{href} class=\"associatedtype\">{name}</a>{generics}",
936935
indent = " ".repeat(indent),
937936
vis = visibility_print_with_space(it, cx),
938937
href = assoc_href_attr(it, link, cx).maybe_display(),
939938
name = it.name.as_ref().unwrap(),
940939
generics = generics.print(cx),
941-
),
942-
);
943-
if !bounds.is_empty() {
944-
write_str(w, format_args!(": {}", print_generic_bounds(bounds, cx)));
945-
}
946-
// Render the default before the where-clause which aligns with the new recommended style. See #89122.
947-
if let Some(default) = default {
948-
write_str(w, format_args!(" = {}", default.print(cx)));
949-
}
950-
write_str(w, format_args!("{}", print_where_clause(generics, cx, indent, Ending::NoNewline)));
940+
)?;
941+
if !bounds.is_empty() {
942+
write!(w, ": {}", print_generic_bounds(bounds, cx))?;
943+
}
944+
// Render the default before the where-clause which aligns with the new recommended style. See #89122.
945+
if let Some(default) = default {
946+
write!(w, " = {}", default.print(cx))?;
947+
}
948+
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline))
949+
})
951950
}
952951

953952
fn assoc_method<'a, 'tcx>(
@@ -1182,26 +1181,40 @@ fn render_assoc_item(
11821181
),
11831182
);
11841183
}
1185-
clean::RequiredAssocTypeItem(ref generics, ref bounds) => assoc_type(
1186-
w,
1187-
item,
1188-
generics,
1189-
bounds,
1190-
None,
1191-
link,
1192-
if parent == ItemType::Trait { 4 } else { 0 },
1193-
cx,
1194-
),
1195-
clean::AssocTypeItem(ref ty, ref bounds) => assoc_type(
1196-
w,
1197-
item,
1198-
&ty.generics,
1199-
bounds,
1200-
Some(ty.item_type.as_ref().unwrap_or(&ty.type_)),
1201-
link,
1202-
if parent == ItemType::Trait { 4 } else { 0 },
1203-
cx,
1204-
),
1184+
clean::RequiredAssocTypeItem(ref generics, ref bounds) => {
1185+
write_str(
1186+
w,
1187+
format_args!(
1188+
"{}",
1189+
assoc_type(
1190+
item,
1191+
generics,
1192+
bounds,
1193+
None,
1194+
link,
1195+
if parent == ItemType::Trait { 4 } else { 0 },
1196+
cx,
1197+
)
1198+
),
1199+
);
1200+
}
1201+
clean::AssocTypeItem(ref ty, ref bounds) => {
1202+
write_str(
1203+
w,
1204+
format_args!(
1205+
"{}",
1206+
assoc_type(
1207+
item,
1208+
&ty.generics,
1209+
bounds,
1210+
Some(ty.item_type.as_ref().unwrap_or(&ty.type_)),
1211+
link,
1212+
if parent == ItemType::Trait { 4 } else { 0 },
1213+
cx,
1214+
)
1215+
),
1216+
);
1217+
}
12051218
_ => panic!("render_assoc_item called on non-associated-item"),
12061219
}
12071220
}
@@ -1578,20 +1591,23 @@ fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {
15781591
);
15791592
for it in &impl_.items {
15801593
if let clean::AssocTypeItem(ref tydef, ref _bounds) = it.kind {
1581-
out.push_str("<div class=\"where\"> ");
15821594
let empty_set = FxIndexSet::default();
15831595
let src_link = AssocItemLink::GotoSource(trait_did.into(), &empty_set);
1584-
assoc_type(
1596+
write_str(
15851597
&mut out,
1586-
it,
1587-
&tydef.generics,
1588-
&[], // intentionally leaving out bounds
1589-
Some(&tydef.type_),
1590-
src_link,
1591-
0,
1592-
cx,
1598+
format_args!(
1599+
"<div class=\"where\"> {};</div>",
1600+
assoc_type(
1601+
it,
1602+
&tydef.generics,
1603+
&[], // intentionally leaving out bounds
1604+
Some(&tydef.type_),
1605+
src_link,
1606+
0,
1607+
cx,
1608+
)
1609+
),
15931610
);
1594-
out.push_str(";</div>");
15951611
}
15961612
}
15971613
}
@@ -1860,18 +1876,21 @@ fn render_impl(
18601876
// Anchors are only used on trait impls.
18611877
write_str(w, format_args!("<a href=\"#{id}\" class=\"anchor\">§</a>"));
18621878
}
1863-
w.push_str("<h4 class=\"code-header\">");
1864-
assoc_type(
1879+
write_str(
18651880
w,
1866-
item,
1867-
generics,
1868-
bounds,
1869-
None,
1870-
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1871-
0,
1872-
cx,
1881+
format_args!(
1882+
"<h4 class=\"code-header\">{}</h4></section>",
1883+
assoc_type(
1884+
item,
1885+
generics,
1886+
bounds,
1887+
None,
1888+
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1889+
0,
1890+
cx,
1891+
)
1892+
),
18731893
);
1874-
w.push_str("</h4></section>");
18751894
}
18761895
clean::AssocTypeItem(tydef, _bounds) => {
18771896
let source_id = format!("{item_type}.{name}");
@@ -1885,18 +1904,21 @@ fn render_impl(
18851904
// Anchors are only used on trait impls.
18861905
write_str(w, format_args!("<a href=\"#{id}\" class=\"anchor\">§</a>"));
18871906
}
1888-
w.push_str("<h4 class=\"code-header\">");
1889-
assoc_type(
1907+
write_str(
18901908
w,
1891-
item,
1892-
&tydef.generics,
1893-
&[], // intentionally leaving out bounds
1894-
Some(tydef.item_type.as_ref().unwrap_or(&tydef.type_)),
1895-
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1896-
0,
1897-
cx,
1909+
format_args!(
1910+
"<h4 class=\"code-header\">{}</h4></section>",
1911+
assoc_type(
1912+
item,
1913+
&tydef.generics,
1914+
&[], // intentionally leaving out bounds
1915+
Some(tydef.item_type.as_ref().unwrap_or(&tydef.type_)),
1916+
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1917+
0,
1918+
cx,
1919+
)
1920+
),
18981921
);
1899-
w.push_str("</h4></section>");
19001922
}
19011923
clean::StrippedItem(..) => return,
19021924
_ => panic!("can't make docs for trait item with name {:?}", item.name),
@@ -2192,18 +2214,21 @@ pub(crate) fn render_impl_summary(
21922214
if show_def_docs {
21932215
for it in &inner_impl.items {
21942216
if let clean::AssocTypeItem(ref tydef, ref _bounds) = it.kind {
2195-
w.push_str("<div class=\"where\"> ");
2196-
assoc_type(
2217+
write_str(
21972218
w,
2198-
it,
2199-
&tydef.generics,
2200-
&[], // intentionally leaving out bounds
2201-
Some(&tydef.type_),
2202-
AssocItemLink::Anchor(None),
2203-
0,
2204-
cx,
2219+
format_args!(
2220+
"<div class=\"where\"> {};</div>",
2221+
assoc_type(
2222+
it,
2223+
&tydef.generics,
2224+
&[], // intentionally leaving out bounds
2225+
Some(&tydef.type_),
2226+
AssocItemLink::Anchor(None),
2227+
0,
2228+
cx,
2229+
)
2230+
),
22052231
);
2206-
w.push_str(";</div>");
22072232
}
22082233
}
22092234
}

0 commit comments

Comments
 (0)