Skip to content

Commit 6a962cd

Browse files
committed
Cleanup FnDecl::inner_full_print
1 parent eec6bd9 commit 6a962cd

File tree

2 files changed

+61
-48
lines changed

2 files changed

+61
-48
lines changed

src/librustdoc/display.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ pub(crate) trait Joined: IntoIterator {
1010
///
1111
/// 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)
1212
/// was already called (`joined`'s API doesn't allow it be called more than once).
13-
fn joined(self, sep: &str, f: &mut Formatter<'_>) -> fmt::Result;
13+
fn joined(self, sep: impl Display, f: &mut Formatter<'_>) -> fmt::Result;
1414
}
1515

1616
impl<I, T> Joined for I
1717
where
1818
I: IntoIterator<Item = T>,
1919
T: Display,
2020
{
21-
fn joined(self, sep: &str, f: &mut Formatter<'_>) -> fmt::Result {
21+
fn joined(self, sep: impl Display, f: &mut Formatter<'_>) -> fmt::Result {
2222
let mut iter = self.into_iter();
2323
let Some(first) = iter.next() else { return Ok(()) };
2424
first.fmt(f)?;
2525
for item in iter {
26-
f.write_str(sep)?;
26+
sep.fmt(f)?;
2727
item.fmt(f)?;
2828
}
2929
Ok(())

src/librustdoc/html/format.rs

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use tracing::{debug, trace};
2929
use super::url_parts_builder::UrlPartsBuilder;
3030
use crate::clean::types::ExternalLocation;
3131
use crate::clean::utils::find_nearest_parent_module;
32-
use crate::clean::{self, ExternalCrate, PrimitiveType};
32+
use crate::clean::{self, ExternalCrate, Parameter, PrimitiveType};
3333
use crate::display::{Joined as _, MaybeDisplay as _};
3434
use crate::formats::cache::Cache;
3535
use crate::formats::item_type::ItemType;
@@ -1264,6 +1264,7 @@ impl std::fmt::Write for WriteCounter {
12641264
}
12651265

12661266
// Implements Display by emitting the given number of spaces.
1267+
#[derive(Clone, Copy)]
12671268
struct Indent(usize);
12681269

12691270
impl Display for Indent {
@@ -1325,40 +1326,21 @@ impl clean::FnDecl {
13251326
})
13261327
}
13271328

1328-
fn inner_full_print(
1329-
&self,
1330-
// For None, the declaration will not be line-wrapped. For Some(n),
1331-
// the declaration will be line-wrapped, with an indent of n spaces.
1332-
line_wrapping_indent: Option<usize>,
1333-
f: &mut fmt::Formatter<'_>,
1334-
cx: &Context<'_>,
1335-
) -> fmt::Result {
1336-
let amp = if f.alternate() { "&" } else { "&amp;" };
1337-
1338-
write!(f, "(")?;
1339-
if let Some(n) = line_wrapping_indent
1340-
&& !self.inputs.is_empty()
1341-
{
1342-
write!(f, "\n{}", Indent(n + 4))?;
1343-
}
1344-
1345-
let last_input_index = self.inputs.len().checked_sub(1);
1346-
for (i, param) in self.inputs.iter().enumerate() {
1347-
if let Some(selfty) = param.to_receiver() {
1348-
match selfty {
1349-
clean::SelfTy => {
1350-
write!(f, "self")?;
1351-
}
1329+
fn print_param(param: &Parameter, cx: &Context<'_>) -> impl fmt::Display {
1330+
fmt::from_fn(move |f| {
1331+
if let Some(self_ty) = param.to_receiver() {
1332+
match self_ty {
1333+
clean::SelfTy => f.write_str("self"),
13521334
clean::BorrowedRef { lifetime, mutability, type_: box clean::SelfTy } => {
1353-
write!(f, "{amp}")?;
1335+
f.write_str(if f.alternate() { "&" } else { "&amp;" })?;
13541336
if let Some(lt) = lifetime {
13551337
write!(f, "{lt} ", lt = lt.print())?;
13561338
}
1357-
write!(f, "{mutability}self", mutability = mutability.print_with_space())?;
1339+
write!(f, "{mutability}self", mutability = mutability.print_with_space())
13581340
}
13591341
_ => {
1360-
write!(f, "self: ")?;
1361-
selfty.print(cx).fmt(f)?;
1342+
f.write_str("self: ")?;
1343+
self_ty.print(cx).fmt(f)
13621344
}
13631345
}
13641346
} else {
@@ -1368,28 +1350,59 @@ impl clean::FnDecl {
13681350
if let Some(name) = param.name {
13691351
write!(f, "{name}: ")?;
13701352
}
1371-
param.type_.print(cx).fmt(f)?;
1353+
param.type_.print(cx).fmt(f)
1354+
}
1355+
})
1356+
}
1357+
1358+
fn inner_full_print(
1359+
&self,
1360+
// For None, the declaration will not be line-wrapped. For Some(n),
1361+
// the declaration will be line-wrapped, with an indent of n spaces.
1362+
line_wrapping_indent: Option<usize>,
1363+
f: &mut fmt::Formatter<'_>,
1364+
cx: &Context<'_>,
1365+
) -> fmt::Result {
1366+
f.write_char('(')?;
1367+
1368+
if !self.inputs.is_empty() {
1369+
if self.inputs.is_empty() {
1370+
return Ok(());
1371+
}
1372+
1373+
let line_wrapping_indent = line_wrapping_indent.map(|n| Indent(n + 4));
1374+
1375+
if let Some(indent) = line_wrapping_indent {
1376+
write!(f, "\n{indent}")?;
1377+
}
1378+
1379+
let sep = fmt::from_fn(|f| {
1380+
if let Some(indent) = line_wrapping_indent {
1381+
write!(f, ",\n{indent}")
1382+
} else {
1383+
f.write_str(", ")
1384+
}
1385+
});
1386+
1387+
self.inputs.iter().map(|param| Self::print_param(param, cx)).joined(sep, f)?;
1388+
1389+
if line_wrapping_indent.is_some() {
1390+
writeln!(f, ",")?
13721391
}
1373-
match (line_wrapping_indent, last_input_index) {
1374-
(_, None) => (),
1375-
(None, Some(last_i)) if i != last_i => write!(f, ", ")?,
1376-
(None, Some(_)) => (),
1377-
(Some(n), Some(last_i)) if i != last_i => write!(f, ",\n{}", Indent(n + 4))?,
1378-
(Some(_), Some(_)) => writeln!(f, ",")?,
1392+
1393+
if self.c_variadic {
1394+
match line_wrapping_indent {
1395+
None => write!(f, ", ...")?,
1396+
Some(indent) => writeln!(f, "{indent}...")?,
1397+
};
13791398
}
13801399
}
13811400

1382-
if self.c_variadic {
1383-
match line_wrapping_indent {
1384-
None => write!(f, ", ...")?,
1385-
Some(n) => writeln!(f, "{}...", Indent(n + 4))?,
1386-
};
1401+
if let Some(n) = line_wrapping_indent {
1402+
write!(f, "{}", Indent(n))?
13871403
}
13881404

1389-
match line_wrapping_indent {
1390-
None => write!(f, ")")?,
1391-
Some(n) => write!(f, "{})", Indent(n))?,
1392-
};
1405+
f.write_char(')')?;
13931406

13941407
self.print_output(cx).fmt(f)
13951408
}

0 commit comments

Comments
 (0)