From 1d8971cf4e5f954c7fc9a2611dcf359a832f4c7a Mon Sep 17 00:00:00 2001 From: Yotam Ofek Date: Tue, 16 Sep 2025 10:34:04 +0000 Subject: [PATCH] Cleanup `FnDecl::inner_full_print` --- src/librustdoc/display.rs | 6 +- src/librustdoc/html/format.rs | 107 +++++++++++++++++++--------------- 2 files changed, 62 insertions(+), 51 deletions(-) diff --git a/src/librustdoc/display.rs b/src/librustdoc/display.rs index aa0fad265208d..db868c5c9a8f3 100644 --- a/src/librustdoc/display.rs +++ b/src/librustdoc/display.rs @@ -10,7 +10,7 @@ pub(crate) trait Joined: IntoIterator { /// /// The performance of `joined` is slightly better than `format`, since it doesn't need to use a `Cell` to keep track of whether [`fmt`](Display::fmt) /// was already called (`joined`'s API doesn't allow it be called more than once). - fn joined(self, sep: &str, f: &mut Formatter<'_>) -> fmt::Result; + fn joined(self, sep: impl Display, f: &mut Formatter<'_>) -> fmt::Result; } impl Joined for I @@ -18,12 +18,12 @@ where I: IntoIterator, T: Display, { - fn joined(self, sep: &str, f: &mut Formatter<'_>) -> fmt::Result { + fn joined(self, sep: impl Display, f: &mut Formatter<'_>) -> fmt::Result { let mut iter = self.into_iter(); let Some(first) = iter.next() else { return Ok(()) }; first.fmt(f)?; for item in iter { - f.write_str(sep)?; + sep.fmt(f)?; item.fmt(f)?; } Ok(()) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 493fdc6fb1b33..8c75f301841f3 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1264,6 +1264,7 @@ impl std::fmt::Write for WriteCounter { } // Implements Display by emitting the given number of spaces. +#[derive(Clone, Copy)] struct Indent(usize); impl Display for Indent { @@ -1275,6 +1276,37 @@ impl Display for Indent { } } +impl clean::Parameter { + fn print(&self, cx: &Context<'_>) -> impl fmt::Display { + fmt::from_fn(move |f| { + if let Some(self_ty) = self.to_receiver() { + match self_ty { + clean::SelfTy => f.write_str("self"), + clean::BorrowedRef { lifetime, mutability, type_: box clean::SelfTy } => { + f.write_str(if f.alternate() { "&" } else { "&" })?; + if let Some(lt) = lifetime { + write!(f, "{lt} ", lt = lt.print())?; + } + write!(f, "{mutability}self", mutability = mutability.print_with_space()) + } + _ => { + f.write_str("self: ")?; + self_ty.print(cx).fmt(f) + } + } + } else { + if self.is_const { + write!(f, "const ")?; + } + if let Some(name) = self.name { + write!(f, "{name}: ")?; + } + self.type_.print(cx).fmt(f) + } + }) + } +} + impl clean::FnDecl { pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display { fmt::from_fn(move |f| { @@ -1333,63 +1365,42 @@ impl clean::FnDecl { f: &mut fmt::Formatter<'_>, cx: &Context<'_>, ) -> fmt::Result { - let amp = if f.alternate() { "&" } else { "&" }; + f.write_char('(')?; - write!(f, "(")?; - if let Some(n) = line_wrapping_indent - && !self.inputs.is_empty() - { - write!(f, "\n{}", Indent(n + 4))?; - } + if !self.inputs.is_empty() { + let line_wrapping_indent = line_wrapping_indent.map(|n| Indent(n + 4)); - let last_input_index = self.inputs.len().checked_sub(1); - for (i, param) in self.inputs.iter().enumerate() { - if let Some(selfty) = param.to_receiver() { - match selfty { - clean::SelfTy => { - write!(f, "self")?; - } - clean::BorrowedRef { lifetime, mutability, type_: box clean::SelfTy } => { - write!(f, "{amp}")?; - if let Some(lt) = lifetime { - write!(f, "{lt} ", lt = lt.print())?; - } - write!(f, "{mutability}self", mutability = mutability.print_with_space())?; - } - _ => { - write!(f, "self: ")?; - selfty.print(cx).fmt(f)?; - } - } - } else { - if param.is_const { - write!(f, "const ")?; - } - if let Some(name) = param.name { - write!(f, "{name}: ")?; + if let Some(indent) = line_wrapping_indent { + write!(f, "\n{indent}")?; + } + + let sep = fmt::from_fn(|f| { + if let Some(indent) = line_wrapping_indent { + write!(f, ",\n{indent}") + } else { + f.write_str(", ") } - param.type_.print(cx).fmt(f)?; + }); + + self.inputs.iter().map(|param| param.print(cx)).joined(sep, f)?; + + if line_wrapping_indent.is_some() { + writeln!(f, ",")? } - match (line_wrapping_indent, last_input_index) { - (_, None) => (), - (None, Some(last_i)) if i != last_i => write!(f, ", ")?, - (None, Some(_)) => (), - (Some(n), Some(last_i)) if i != last_i => write!(f, ",\n{}", Indent(n + 4))?, - (Some(_), Some(_)) => writeln!(f, ",")?, + + if self.c_variadic { + match line_wrapping_indent { + None => write!(f, ", ...")?, + Some(indent) => writeln!(f, "{indent}...")?, + }; } } - if self.c_variadic { - match line_wrapping_indent { - None => write!(f, ", ...")?, - Some(n) => writeln!(f, "{}...", Indent(n + 4))?, - }; + if let Some(n) = line_wrapping_indent { + write!(f, "{}", Indent(n))? } - match line_wrapping_indent { - None => write!(f, ")")?, - Some(n) => write!(f, "{})", Indent(n))?, - }; + f.write_char(')')?; self.print_output(cx).fmt(f) }