Skip to content

Commit f614260

Browse files
committed
make assoc_const return impl fmt::Display
1 parent 0ded3c1 commit f614260

File tree

1 file changed

+110
-83
lines changed
  • src/librustdoc/html/render

1 file changed

+110
-83
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 110 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -879,45 +879,44 @@ enum AssocConstValue<'a> {
879879
None,
880880
}
881881

882-
fn assoc_const(
883-
w: &mut String,
884-
it: &clean::Item,
885-
generics: &clean::Generics,
886-
ty: &clean::Type,
887-
value: AssocConstValue<'_>,
888-
link: AssocItemLink<'_>,
882+
fn assoc_const<'a, 'tcx>(
883+
it: &'a clean::Item,
884+
generics: &'a clean::Generics,
885+
ty: &'a clean::Type,
886+
value: AssocConstValue<'a>,
887+
link: AssocItemLink<'a>,
889888
indent: usize,
890-
cx: &Context<'_>,
891-
) {
889+
cx: &'a Context<'tcx>,
890+
) -> impl fmt::Display + 'a + Captures<'tcx> {
892891
let tcx = cx.tcx();
893-
write_str(
894-
w,
895-
format_args!(
892+
fmt::from_fn(move |w| {
893+
write!(
894+
w,
896895
"{indent}{vis}const <a{href} class=\"constant\">{name}</a>{generics}: {ty}",
897896
indent = " ".repeat(indent),
898897
vis = visibility_print_with_space(it, cx),
899898
href = assoc_href_attr(it, link, cx).maybe_display(),
900899
name = it.name.as_ref().unwrap(),
901900
generics = generics.print(cx),
902901
ty = ty.print(cx),
903-
),
904-
);
905-
if let AssocConstValue::TraitDefault(konst) | AssocConstValue::Impl(konst) = value {
906-
// FIXME: `.value()` uses `clean::utils::format_integer_with_underscore_sep` under the
907-
// hood which adds noisy underscores and a type suffix to number literals.
908-
// This hurts readability in this context especially when more complex expressions
909-
// are involved and it doesn't add much of value.
910-
// Find a way to print constants here without all that jazz.
911-
let repr = konst.value(tcx).unwrap_or_else(|| konst.expr(tcx));
912-
if match value {
913-
AssocConstValue::TraitDefault(_) => true, // always show
914-
AssocConstValue::Impl(_) => repr != "_", // show if there is a meaningful value to show
915-
AssocConstValue::None => unreachable!(),
916-
} {
917-
write_str(w, format_args!(" = {}", Escape(&repr)));
902+
)?;
903+
if let AssocConstValue::TraitDefault(konst) | AssocConstValue::Impl(konst) = value {
904+
// FIXME: `.value()` uses `clean::utils::format_integer_with_underscore_sep` under the
905+
// hood which adds noisy underscores and a type suffix to number literals.
906+
// This hurts readability in this context especially when more complex expressions
907+
// are involved and it doesn't add much of value.
908+
// Find a way to print constants here without all that jazz.
909+
let repr = konst.value(tcx).unwrap_or_else(|| konst.expr(tcx));
910+
if match value {
911+
AssocConstValue::TraitDefault(_) => true, // always show
912+
AssocConstValue::Impl(_) => repr != "_", // show if there is a meaningful value to show
913+
AssocConstValue::None => unreachable!(),
914+
} {
915+
write!(w, " = {}", Escape(&repr))?;
916+
}
918917
}
919-
}
920-
write_str(w, format_args!("{}", print_where_clause(generics, cx, indent, Ending::NoNewline)));
918+
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline))
919+
})
921920
}
922921

923922
fn assoc_type(
@@ -1132,36 +1131,57 @@ fn render_assoc_item(
11321131
),
11331132
);
11341133
}
1135-
clean::RequiredAssocConstItem(generics, ty) => assoc_const(
1136-
w,
1137-
item,
1138-
generics,
1139-
ty,
1140-
AssocConstValue::None,
1141-
link,
1142-
if parent == ItemType::Trait { 4 } else { 0 },
1143-
cx,
1144-
),
1145-
clean::ProvidedAssocConstItem(ci) => assoc_const(
1146-
w,
1147-
item,
1148-
&ci.generics,
1149-
&ci.type_,
1150-
AssocConstValue::TraitDefault(&ci.kind),
1151-
link,
1152-
if parent == ItemType::Trait { 4 } else { 0 },
1153-
cx,
1154-
),
1155-
clean::ImplAssocConstItem(ci) => assoc_const(
1156-
w,
1157-
item,
1158-
&ci.generics,
1159-
&ci.type_,
1160-
AssocConstValue::Impl(&ci.kind),
1161-
link,
1162-
if parent == ItemType::Trait { 4 } else { 0 },
1163-
cx,
1164-
),
1134+
clean::RequiredAssocConstItem(generics, ty) => {
1135+
write_str(
1136+
w,
1137+
format_args!(
1138+
"{}",
1139+
assoc_const(
1140+
item,
1141+
generics,
1142+
ty,
1143+
AssocConstValue::None,
1144+
link,
1145+
if parent == ItemType::Trait { 4 } else { 0 },
1146+
cx,
1147+
)
1148+
),
1149+
);
1150+
}
1151+
clean::ProvidedAssocConstItem(ci) => {
1152+
write_str(
1153+
w,
1154+
format_args!(
1155+
"{}",
1156+
assoc_const(
1157+
item,
1158+
&ci.generics,
1159+
&ci.type_,
1160+
AssocConstValue::TraitDefault(&ci.kind),
1161+
link,
1162+
if parent == ItemType::Trait { 4 } else { 0 },
1163+
cx,
1164+
)
1165+
),
1166+
);
1167+
}
1168+
clean::ImplAssocConstItem(ci) => {
1169+
write_str(
1170+
w,
1171+
format_args!(
1172+
"{}",
1173+
assoc_const(
1174+
item,
1175+
&ci.generics,
1176+
&ci.type_,
1177+
AssocConstValue::Impl(&ci.kind),
1178+
link,
1179+
if parent == ItemType::Trait { 4 } else { 0 },
1180+
cx,
1181+
)
1182+
),
1183+
);
1184+
}
11651185
clean::RequiredAssocTypeItem(ref generics, ref bounds) => assoc_type(
11661186
w,
11671187
item,
@@ -1779,18 +1799,21 @@ fn render_impl(
17791799
// Anchors are only used on trait impls.
17801800
write_str(w, format_args!("<a href=\"#{id}\" class=\"anchor\">§</a>"));
17811801
}
1782-
w.push_str("<h4 class=\"code-header\">");
1783-
assoc_const(
1802+
write_str(
17841803
w,
1785-
item,
1786-
generics,
1787-
ty,
1788-
AssocConstValue::None,
1789-
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1790-
0,
1791-
cx,
1804+
format_args!(
1805+
"<h4 class=\"code-header\">{}</h4></section>",
1806+
assoc_const(
1807+
item,
1808+
generics,
1809+
ty,
1810+
AssocConstValue::None,
1811+
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1812+
0,
1813+
cx,
1814+
)
1815+
),
17921816
);
1793-
w.push_str("</h4></section>");
17941817
}
17951818
clean::ProvidedAssocConstItem(ci) | clean::ImplAssocConstItem(ci) => {
17961819
let source_id = format!("{item_type}.{name}");
@@ -1804,22 +1827,26 @@ fn render_impl(
18041827
// Anchors are only used on trait impls.
18051828
write_str(w, format_args!("<a href=\"#{id}\" class=\"anchor\">§</a>"));
18061829
}
1807-
w.push_str("<h4 class=\"code-header\">");
1808-
assoc_const(
1830+
write_str(
18091831
w,
1810-
item,
1811-
&ci.generics,
1812-
&ci.type_,
1813-
match item.kind {
1814-
clean::ProvidedAssocConstItem(_) => AssocConstValue::TraitDefault(&ci.kind),
1815-
clean::ImplAssocConstItem(_) => AssocConstValue::Impl(&ci.kind),
1816-
_ => unreachable!(),
1817-
},
1818-
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1819-
0,
1820-
cx,
1832+
format_args!(
1833+
"<h4 class=\"code-header\">{}</h4></section>",
1834+
assoc_const(
1835+
item,
1836+
&ci.generics,
1837+
&ci.type_,
1838+
match item.kind {
1839+
clean::ProvidedAssocConstItem(_) =>
1840+
AssocConstValue::TraitDefault(&ci.kind),
1841+
clean::ImplAssocConstItem(_) => AssocConstValue::Impl(&ci.kind),
1842+
_ => unreachable!(),
1843+
},
1844+
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1845+
0,
1846+
cx,
1847+
)
1848+
),
18211849
);
1822-
w.push_str("</h4></section>");
18231850
}
18241851
clean::RequiredAssocTypeItem(ref generics, ref bounds) => {
18251852
let source_id = format!("{item_type}.{name}");

0 commit comments

Comments
 (0)