rustdoc: use std's (unstable) fmt::from_fn instead of open-coding it

This commit is contained in:
Yotam Ofek 2025-01-14 12:17:44 +00:00
parent 73fc7af716
commit 1dee842c92
7 changed files with 73 additions and 91 deletions

View file

@ -150,8 +150,9 @@ pub(crate) fn comma_sep<T: Display>(
items: impl Iterator<Item = T>,
space_after_comma: bool,
) -> impl Display {
display_fn(move |f| {
for (i, item) in items.enumerate() {
let items = Cell::new(Some(items));
fmt::from_fn(move |f| {
for (i, item) in items.take().unwrap().enumerate() {
if i != 0 {
write!(f, ",{}", if space_after_comma { " " } else { "" })?;
}
@ -165,7 +166,7 @@ pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
bounds: &'a [clean::GenericBound],
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
let mut bounds_dup = FxHashSet::default();
for (i, bound) in bounds.iter().filter(|b| bounds_dup.insert(*b)).enumerate() {
@ -183,7 +184,7 @@ impl clean::GenericParamDef {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| match &self.kind {
fmt::from_fn(move |f| match &self.kind {
clean::GenericParamDefKind::Lifetime { outlives } => {
write!(f, "{}", self.name)?;
@ -238,7 +239,7 @@ impl clean::Generics {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
let mut real_params = self.params.iter().filter(|p| !p.is_synthetic_param()).peekable();
if real_params.peek().is_none() {
return Ok(());
@ -268,12 +269,12 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
indent: usize,
ending: Ending,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
let mut where_predicates = gens
.where_predicates
.iter()
.map(|pred| {
display_fn(move |f| {
fmt::from_fn(move |f| {
if f.alternate() {
f.write_str(" ")?;
} else {
@ -376,17 +377,15 @@ impl clean::Lifetime {
impl clean::ConstantKind {
pub(crate) fn print(&self, tcx: TyCtxt<'_>) -> impl Display + '_ {
let expr = self.expr(tcx);
display_fn(
move |f| {
if f.alternate() { f.write_str(&expr) } else { write!(f, "{}", Escape(&expr)) }
},
)
fmt::from_fn(move |f| {
if f.alternate() { f.write_str(&expr) } else { write!(f, "{}", Escape(&expr)) }
})
}
}
impl clean::PolyTrait {
fn print<'a, 'tcx: 'a>(&'a self, cx: &'a Context<'tcx>) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
print_higher_ranked_params_with_space(&self.generic_params, cx, "for").fmt(f)?;
self.trait_.print(cx).fmt(f)
})
@ -398,7 +397,7 @@ impl clean::GenericBound {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| match self {
fmt::from_fn(move |f| match self {
clean::GenericBound::Outlives(lt) => write!(f, "{}", lt.print()),
clean::GenericBound::TraitBound(ty, modifiers) => {
// `const` and `~const` trait bounds are experimental; don't render them.
@ -430,7 +429,7 @@ impl clean::GenericBound {
impl clean::GenericArgs {
fn print<'a, 'tcx: 'a>(&'a self, cx: &'a Context<'tcx>) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
match self {
clean::GenericArgs::AngleBracketed { args, constraints } => {
if !args.is_empty() || !constraints.is_empty() {
@ -950,7 +949,7 @@ fn tybounds<'a, 'tcx: 'a>(
lt: &'a Option<clean::Lifetime>,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
for (i, bound) in bounds.iter().enumerate() {
if i > 0 {
write!(f, " + ")?;
@ -971,7 +970,7 @@ fn print_higher_ranked_params_with_space<'a, 'tcx: 'a>(
cx: &'a Context<'tcx>,
keyword: &'static str,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
if !params.is_empty() {
f.write_str(keyword)?;
f.write_str(if f.alternate() { "<" } else { "&lt;" })?;
@ -982,13 +981,13 @@ fn print_higher_ranked_params_with_space<'a, 'tcx: 'a>(
})
}
pub(crate) fn anchor<'a, 'cx: 'a>(
pub(crate) fn anchor<'a: 'cx, 'cx>(
did: DefId,
text: Symbol,
cx: &'cx Context<'_>,
) -> impl Display + 'a {
let parts = href(did, cx);
display_fn(move |f| {
cx: &'cx Context<'a>,
) -> impl Display + Captures<'a> + 'cx {
fmt::from_fn(move |f| {
let parts = href(did, cx);
if let Ok((url, short_ty, fqp)) = parts {
write!(
f,
@ -1150,7 +1149,7 @@ fn fmt_type(
}
}
clean::BorrowedRef { lifetime: ref l, mutability, type_: ref ty } => {
let lt = display_fn(|f| match l {
let lt = fmt::from_fn(|f| match l {
Some(l) => write!(f, "{} ", l.print()),
_ => Ok(()),
});
@ -1270,7 +1269,7 @@ impl clean::Type {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'b + Captures<'tcx> {
display_fn(move |f| fmt_type(self, f, false, cx))
fmt::from_fn(move |f| fmt_type(self, f, false, cx))
}
}
@ -1279,7 +1278,7 @@ impl clean::Path {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'b + Captures<'tcx> {
display_fn(move |f| resolved_path(f, self.def_id(), self, false, false, cx))
fmt::from_fn(move |f| resolved_path(f, self.def_id(), self, false, false, cx))
}
}
@ -1289,7 +1288,7 @@ impl clean::Impl {
use_absolute: bool,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
f.write_str("impl")?;
self.generics.print(cx).fmt(f)?;
f.write_str(" ")?;
@ -1407,7 +1406,7 @@ impl clean::Arguments {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
for (i, input) in self.values.iter().enumerate() {
write!(f, "{}: ", input.name)?;
input.type_.print(cx).fmt(f)?;
@ -1447,7 +1446,7 @@ impl clean::FnDecl {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'b + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
let ellipsis = if self.c_variadic { ", ..." } else { "" };
if f.alternate() {
write!(
@ -1481,10 +1480,10 @@ impl clean::FnDecl {
indent: usize,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
// First, generate the text form of the declaration, with no line wrapping, and count the bytes.
let mut counter = WriteCounter(0);
write!(&mut counter, "{:#}", display_fn(|f| { self.inner_full_print(None, f, cx) }))
write!(&mut counter, "{:#}", fmt::from_fn(|f| { self.inner_full_print(None, f, cx) }))
.unwrap();
// If the text form was over 80 characters wide, we will line-wrap our output.
let line_wrapping_indent =
@ -1566,7 +1565,7 @@ impl clean::FnDecl {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| match &self.output {
fmt::from_fn(move |f| match &self.output {
clean::Tuple(tys) if tys.is_empty() => Ok(()),
ty if f.alternate() => {
write!(f, " -> {:#}", ty.print(cx))
@ -1618,7 +1617,7 @@ pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
};
let is_doc_hidden = item.is_doc_hidden();
display_fn(move |f| {
fmt::from_fn(move |f| {
if is_doc_hidden {
f.write_str("#[doc(hidden)] ")?;
}
@ -1692,7 +1691,7 @@ impl clean::Import {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| match self.kind {
fmt::from_fn(move |f| match self.kind {
clean::ImportKind::Simple(name) => {
if name == self.source.path.last() {
write!(f, "use {};", self.source.print(cx))
@ -1716,7 +1715,7 @@ impl clean::ImportSource {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| match self.did {
fmt::from_fn(move |f| match self.did {
Some(did) => resolved_path(f, did, &self.path, true, false, cx),
_ => {
for seg in &self.path.segments[..self.path.segments.len() - 1] {
@ -1744,7 +1743,7 @@ impl clean::AssocItemConstraint {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
f.write_str(self.assoc.name.as_str())?;
self.assoc.args.print(cx).fmt(f)?;
match self.kind {
@ -1765,7 +1764,7 @@ impl clean::AssocItemConstraint {
}
pub(crate) fn print_abi_with_space(abi: ExternAbi) -> impl Display {
display_fn(move |f| {
fmt::from_fn(move |f| {
let quot = if f.alternate() { "\"" } else { "&quot;" };
match abi {
ExternAbi::Rust => Ok(()),
@ -1783,7 +1782,7 @@ impl clean::GenericArg {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| match self {
fmt::from_fn(move |f| match self {
clean::GenericArg::Lifetime(lt) => lt.print().fmt(f),
clean::GenericArg::Type(ty) => ty.print(cx).fmt(f),
clean::GenericArg::Const(ct) => ct.print(cx.tcx()).fmt(f),
@ -1797,24 +1796,9 @@ impl clean::Term {
&'a self,
cx: &'a Context<'tcx>,
) -> impl Display + 'a + Captures<'tcx> {
display_fn(move |f| match self {
fmt::from_fn(move |f| match self {
clean::Term::Type(ty) => ty.print(cx).fmt(f),
clean::Term::Constant(ct) => ct.print(cx.tcx()).fmt(f),
})
}
}
pub(crate) fn display_fn(f: impl FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl Display {
struct WithFormatter<F>(Cell<Option<F>>);
impl<F> Display for WithFormatter<F>
where
F: FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(self.0.take()).unwrap()(f)
}
}
WithFormatter(Cell::new(Some(f)))
}

View file

@ -69,9 +69,9 @@ use crate::formats::cache::Cache;
use crate::formats::item_type::ItemType;
use crate::html::escape::Escape;
use crate::html::format::{
Buffer, Ending, HrefError, PrintWithSpace, display_fn, href, join_with_double_colon,
print_abi_with_space, print_constness_with_space, print_default_space, print_generic_bounds,
print_where_clause, visibility_print_with_space,
Buffer, Ending, HrefError, PrintWithSpace, href, join_with_double_colon, print_abi_with_space,
print_constness_with_space, print_default_space, print_generic_bounds, print_where_clause,
visibility_print_with_space,
};
use crate::html::markdown::{
HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine,
@ -82,13 +82,14 @@ use crate::scrape_examples::{CallData, CallLocation};
use crate::{DOC_RUST_LANG_ORG_CHANNEL, try_none};
pub(crate) fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ {
crate::html::format::display_fn(move |f| {
fmt::from_fn(move |f| {
if !v.ends_with('/') && !v.is_empty() { write!(f, "{v}/") } else { f.write_str(v) }
})
}
/// Specifies whether rendering directly implemented trait items or ones from a certain Deref
/// impl.
#[derive(Copy, Clone)]
pub(crate) enum AssocItemRender<'a> {
All,
DerefFor { trait_: &'a clean::Path, type_: &'a clean::Type, deref_mut_: bool },
@ -309,9 +310,7 @@ impl ItemEntry {
impl ItemEntry {
pub(crate) fn print(&self) -> impl fmt::Display + '_ {
crate::html::format::display_fn(move |f| {
write!(f, "<a href=\"{}\">{}</a>", self.url, Escape(&self.name))
})
fmt::from_fn(move |f| write!(f, "<a href=\"{}\">{}</a>", self.url, Escape(&self.name)))
}
}
@ -513,7 +512,7 @@ fn document<'a, 'cx: 'a>(
info!("Documenting {name}");
}
display_fn(move |f| {
fmt::from_fn(move |f| {
document_item_info(cx, item, parent).render_into(f).unwrap();
if parent.is_none() {
write!(f, "{}", document_full_collapsible(item, cx, heading_offset))
@ -530,7 +529,7 @@ fn render_markdown<'a, 'cx: 'a>(
links: Vec<RenderedLink>,
heading_offset: HeadingOffset,
) -> impl fmt::Display + 'a + Captures<'cx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
write!(
f,
"<div class=\"docblock\">{}</div>",
@ -557,7 +556,7 @@ fn document_short<'a, 'cx: 'a>(
parent: &'a clean::Item,
show_def_docs: bool,
) -> impl fmt::Display + 'a + Captures<'cx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
document_item_info(cx, item, Some(parent)).render_into(f).unwrap();
if !show_def_docs {
return Ok(());
@ -605,7 +604,7 @@ fn document_full_inner<'a, 'cx: 'a>(
is_collapsible: bool,
heading_offset: HeadingOffset,
) -> impl fmt::Display + 'a + Captures<'cx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
if let Some(s) = item.opt_doc_value() {
debug!("Doc block: =====\n{s}\n=====");
if is_collapsible {
@ -1159,7 +1158,7 @@ fn render_attributes_in_pre<'a, 'tcx: 'a>(
prefix: &'a str,
cx: &'a Context<'tcx>,
) -> impl fmt::Display + Captures<'a> + Captures<'tcx> {
crate::html::format::display_fn(move |f| {
fmt::from_fn(move |f| {
for a in it.attributes(cx.tcx(), cx.cache(), false) {
writeln!(f, "{prefix}{a}")?;
}
@ -1256,9 +1255,9 @@ fn render_assoc_items<'a, 'cx: 'a>(
it: DefId,
what: AssocItemRender<'a>,
) -> impl fmt::Display + 'a + Captures<'cx> {
let mut derefs = DefIdSet::default();
derefs.insert(it);
display_fn(move |f| {
fmt::from_fn(move |f| {
let mut derefs = DefIdSet::default();
derefs.insert(it);
render_assoc_items_inner(f, cx, containing_item, it, what, &mut derefs);
Ok(())
})

View file

@ -30,7 +30,7 @@ use crate::formats::Impl;
use crate::formats::item_type::ItemType;
use crate::html::escape::{Escape, EscapeBodyTextWithWbr};
use crate::html::format::{
Buffer, Ending, PrintWithSpace, display_fn, join_with_double_colon, print_abi_with_space,
Buffer, Ending, PrintWithSpace, join_with_double_colon, print_abi_with_space,
print_constness_with_space, print_where_clause, visibility_print_with_space,
};
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
@ -92,7 +92,7 @@ macro_rules! item_template_methods {
() => {};
(document $($rest:tt)*) => {
fn document<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
let (item, cx) = self.item_and_cx();
let v = document(cx, item, None, HeadingOffset::H2);
write!(f, "{v}")
@ -102,7 +102,7 @@ macro_rules! item_template_methods {
};
(document_type_layout $($rest:tt)*) => {
fn document_type_layout<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
let (item, cx) = self.item_and_cx();
let def_id = item.item_id.expect_def_id();
let v = document_type_layout(cx, def_id);
@ -113,7 +113,7 @@ macro_rules! item_template_methods {
};
(render_attributes_in_pre $($rest:tt)*) => {
fn render_attributes_in_pre<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
let (item, cx) = self.item_and_cx();
let v = render_attributes_in_pre(item, "", cx);
write!(f, "{v}")
@ -123,7 +123,7 @@ macro_rules! item_template_methods {
};
(render_assoc_items $($rest:tt)*) => {
fn render_assoc_items<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
let (item, cx) = self.item_and_cx();
let def_id = item.item_id.expect_def_id();
let v = render_assoc_items(cx, item, def_id, AssocItemRender::All);
@ -520,13 +520,13 @@ fn extra_info_tags<'a, 'tcx: 'a>(
parent: &'a clean::Item,
import_def_id: Option<DefId>,
) -> impl fmt::Display + 'a + Captures<'tcx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
fn tag_html<'a>(
class: &'a str,
title: &'a str,
contents: &'a str,
) -> impl fmt::Display + 'a {
display_fn(move |f| {
fmt::from_fn(move |f| {
write!(
f,
r#"<wbr><span class="stab {class}" title="{title}">{contents}</span>"#,
@ -1376,7 +1376,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
fn render_union<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
let v = render_union(self.it, Some(&self.s.generics), &self.s.fields, self.cx);
write!(f, "{v}")
})
@ -1386,7 +1386,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
&'b self,
field: &'a clean::Item,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
let v = document(self.cx, field, Some(self.it), HeadingOffset::H3);
write!(f, "{v}")
})
@ -1400,7 +1400,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
&'b self,
ty: &'a clean::Type,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
let v = ty.print(self.cx);
write!(f, "{v}")
})
@ -1427,7 +1427,7 @@ fn print_tuple_struct_fields<'a, 'cx: 'a>(
cx: &'a Context<'cx>,
s: &'a [clean::Item],
) -> impl fmt::Display + 'a + Captures<'cx> {
display_fn(|f| {
fmt::from_fn(|f| {
if !s.is_empty()
&& s.iter().all(|field| {
matches!(field.kind, clean::StrippedItem(box clean::StructFieldItem(..)))
@ -2152,7 +2152,7 @@ fn render_union<'a, 'cx: 'a>(
fields: &'a [clean::Item],
cx: &'a Context<'cx>,
) -> impl fmt::Display + 'a + Captures<'cx> {
display_fn(move |mut f| {
fmt::from_fn(move |mut f| {
write!(f, "{}union {}", visibility_print_with_space(it, cx), it.name.unwrap(),)?;
let where_displayed = g
@ -2332,7 +2332,7 @@ fn document_non_exhaustive_header(item: &clean::Item) -> &str {
}
fn document_non_exhaustive(item: &clean::Item) -> impl fmt::Display + '_ {
display_fn(|f| {
fmt::from_fn(|f| {
if item.is_non_exhaustive() {
write!(
f,

View file

@ -100,18 +100,17 @@ impl<'a> Link<'a> {
}
pub(crate) mod filters {
use std::fmt::Display;
use std::fmt::{self, Display};
use rinja::filters::Safe;
use crate::html::escape::EscapeBodyTextWithWbr;
use crate::html::render::display_fn;
pub(crate) fn wrapped<T>(v: T) -> rinja::Result<Safe<impl Display>>
where
T: Display,
{
let string = v.to_string();
Ok(Safe(display_fn(move |f| EscapeBodyTextWithWbr(&string).fmt(f))))
Ok(Safe(fmt::from_fn(move |f| EscapeBodyTextWithWbr(&string).fmt(f))))
}
}

View file

@ -9,7 +9,6 @@ use rustc_middle::ty::layout::LayoutError;
use rustc_middle::ty::{self};
use rustc_span::symbol::Symbol;
use crate::html::format::display_fn;
use crate::html::render::Context;
#[derive(Template)]
@ -31,7 +30,7 @@ pub(crate) fn document_type_layout<'a, 'cx: 'a>(
cx: &'a Context<'cx>,
ty_def_id: DefId,
) -> impl fmt::Display + 'a + Captures<'cx> {
display_fn(move |f| {
fmt::from_fn(move |f| {
if !cx.shared.show_type_layout {
return Ok(());
}

View file

@ -17,7 +17,7 @@ use crate::clean::utils::has_doc_flag;
use crate::docfs::PathError;
use crate::error::Error;
use crate::html::render::Context;
use crate::html::{format, highlight, layout};
use crate::html::{highlight, layout};
use crate::visit::DocVisitor;
pub(crate) fn render(cx: &mut Context<'_>, krate: &clean::Crate) -> Result<(), Error> {
@ -331,10 +331,10 @@ pub(crate) fn print_src(
decoration_info: &highlight::DecorationInfo,
source_context: SourceContext<'_>,
) {
let current_href = context
.href_from_span(clean::Span::new(file_span), false)
.expect("only local crates should have sources emitted");
let code = format::display_fn(move |fmt| {
let code = fmt::from_fn(move |fmt| {
let current_href = context
.href_from_span(clean::Span::new(file_span), false)
.expect("only local crates should have sources emitted");
highlight::write_code(
fmt,
s,

View file

@ -5,6 +5,7 @@
#![feature(rustc_private)]
#![feature(assert_matches)]
#![feature(box_patterns)]
#![feature(debug_closure_helpers)]
#![feature(file_buffered)]
#![feature(if_let_guard)]
#![feature(impl_trait_in_assoc_type)]