Skip to content

Commit

Permalink
rustdoc: use std's (unstable) fmt::from_fn instead of open-coding it
Browse files Browse the repository at this point in the history
  • Loading branch information
yotamofek committed Jan 22, 2025
1 parent 73fc7af commit 1dee842
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 91 deletions.
90 changes: 37 additions & 53 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { "" })?;
}
Expand All @@ -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() {
Expand All @@ -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)?;

Expand Down Expand Up @@ -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(());
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
})
Expand All @@ -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.
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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, " + ")?;
Expand All @@ -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;" })?;
Expand All @@ -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,
Expand Down Expand Up @@ -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(()),
});
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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))
}
}

Expand All @@ -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(" ")?;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)] ")?;
}
Expand Down Expand Up @@ -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))
Expand All @@ -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] {
Expand Down Expand Up @@ -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 {
Expand All @@ -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(()),
Expand All @@ -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),
Expand All @@ -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)))
}
29 changes: 14 additions & 15 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 },
Expand Down Expand Up @@ -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)))
}
}

Expand Down Expand Up @@ -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))
Expand All @@ -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>",
Expand All @@ -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(());
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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}")?;
}
Expand Down Expand Up @@ -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(())
})
Expand Down
Loading

0 comments on commit 1dee842

Please sign in to comment.