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

Commit 1cbbdd9

Browse files
committed
make item_constant return impl fmt::Display
1 parent 1644d5d commit 1cbbdd9

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut String)
260260
clean::ForeignStaticItem(ref i, safety) => {
261261
write_str(buf, format_args!("{}", item_static(cx, item, i, Some(*safety))))
262262
}
263-
clean::ConstantItem(ci) => item_constant(buf, cx, item, &ci.generics, &ci.type_, &ci.kind),
263+
clean::ConstantItem(ci) => write_str(
264+
buf,
265+
format_args!("{}", item_constant(cx, item, &ci.generics, &ci.type_, &ci.kind)),
266+
),
264267
clean::ForeignTypeItem => item_foreign_type(buf, cx, item),
265268
clean::KeywordItem => item_keyword(buf, cx, item),
266269
clean::TraitAliasItem(ref ta) => item_trait_alias(buf, cx, item, ta),
@@ -1952,64 +1955,64 @@ fn item_primitive<'a, 'tcx>(
19521955
})
19531956
}
19541957

1955-
fn item_constant(
1956-
w: &mut String,
1957-
cx: &Context<'_>,
1958-
it: &clean::Item,
1959-
generics: &clean::Generics,
1960-
ty: &clean::Type,
1961-
c: &clean::ConstantKind,
1962-
) {
1963-
wrap_item(w, |w| {
1964-
let tcx = cx.tcx();
1965-
render_attributes_in_code(w, it, cx);
1958+
fn item_constant<'a, 'tcx>(
1959+
cx: &'a Context<'tcx>,
1960+
it: &'a clean::Item,
1961+
generics: &'a clean::Generics,
1962+
ty: &'a clean::Type,
1963+
c: &'a clean::ConstantKind,
1964+
) -> impl fmt::Display + 'a + Captures<'tcx> {
1965+
fmt::from_fn(|w| {
1966+
wrap_item(w, |w| {
1967+
let tcx = cx.tcx();
1968+
render_attributes_in_code(w, it, cx);
19661969

1967-
write_str(
1968-
w,
1969-
format_args!(
1970+
write!(
1971+
w,
19701972
"{vis}const {name}{generics}: {typ}{where_clause}",
19711973
vis = visibility_print_with_space(it, cx),
19721974
name = it.name.unwrap(),
19731975
generics = generics.print(cx),
19741976
typ = ty.print(cx),
19751977
where_clause =
19761978
print_where_clause(generics, cx, 0, Ending::NoNewline).maybe_display(),
1977-
),
1978-
);
1979+
)?;
19791980

1980-
// FIXME: The code below now prints
1981-
// ` = _; // 100i32`
1982-
// if the expression is
1983-
// `50 + 50`
1984-
// which looks just wrong.
1985-
// Should we print
1986-
// ` = 100i32;`
1987-
// instead?
1988-
1989-
let value = c.value(tcx);
1990-
let is_literal = c.is_literal(tcx);
1991-
let expr = c.expr(tcx);
1992-
if value.is_some() || is_literal {
1993-
write_str(w, format_args!(" = {expr};", expr = Escape(&expr)));
1994-
} else {
1995-
w.push_str(";");
1996-
}
1981+
// FIXME: The code below now prints
1982+
// ` = _; // 100i32`
1983+
// if the expression is
1984+
// `50 + 50`
1985+
// which looks just wrong.
1986+
// Should we print
1987+
// ` = 100i32;`
1988+
// instead?
1989+
1990+
let value = c.value(tcx);
1991+
let is_literal = c.is_literal(tcx);
1992+
let expr = c.expr(tcx);
1993+
if value.is_some() || is_literal {
1994+
write!(w, " = {expr};", expr = Escape(&expr))?;
1995+
} else {
1996+
w.write_str(";")?;
1997+
}
19971998

1998-
if !is_literal {
1999-
if let Some(value) = &value {
2000-
let value_lowercase = value.to_lowercase();
2001-
let expr_lowercase = expr.to_lowercase();
1999+
if !is_literal {
2000+
if let Some(value) = &value {
2001+
let value_lowercase = value.to_lowercase();
2002+
let expr_lowercase = expr.to_lowercase();
20022003

2003-
if value_lowercase != expr_lowercase
2004-
&& value_lowercase.trim_end_matches("i32") != expr_lowercase
2005-
{
2006-
write_str(w, format_args!(" // {value}", value = Escape(value)));
2004+
if value_lowercase != expr_lowercase
2005+
&& value_lowercase.trim_end_matches("i32") != expr_lowercase
2006+
{
2007+
write!(w, " // {value}", value = Escape(value))?;
2008+
}
20072009
}
20082010
}
2009-
}
2010-
});
2011+
Ok(())
2012+
})?;
20112013

2012-
write_str(w, format_args!("{}", document(cx, it, None, HeadingOffset::H2)));
2014+
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
2015+
})
20132016
}
20142017

20152018
fn item_struct<'a, 'tcx>(

0 commit comments

Comments
 (0)