Skip to content

Commit 34a805b

Browse files
authored
Rollup merge of #146645 - yotamofek:pr/rustdoc/fndecl_inner_full_print, r=fmease
Cleanup `FnDecl::inner_full_print` `inner_full_print` was pretty hard to follow IMHO. Hopefully this cleans it up a little bit. Also, it was checking whether `self.inputs` is empty twice, and then handling an unreachable match arm: https://github.com/yotamofek/rust/blob/f836ae4e663b6e8938096b8559e094d18361be55/src/librustdoc/html/format.rs#L1368C1-L1368C33 `last_input_index` could only be `None` if the fn has no parameters, in which case the loop body would never run. r? ``@GuillaumeGomez`` if you have the capacity :) BTW, can we rename `FnDecl::inputs` to `parameters` or something? And `output` to `return_ty`?
2 parents d5aa8d5 + 1d8971c commit 34a805b

File tree

2 files changed

+62
-51
lines changed

2 files changed

+62
-51
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: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
@@ -1275,6 +1276,37 @@ impl Display for Indent {
12751276
}
12761277
}
12771278

1279+
impl clean::Parameter {
1280+
fn print(&self, cx: &Context<'_>) -> impl fmt::Display {
1281+
fmt::from_fn(move |f| {
1282+
if let Some(self_ty) = self.to_receiver() {
1283+
match self_ty {
1284+
clean::SelfTy => f.write_str("self"),
1285+
clean::BorrowedRef { lifetime, mutability, type_: box clean::SelfTy } => {
1286+
f.write_str(if f.alternate() { "&" } else { "&amp;" })?;
1287+
if let Some(lt) = lifetime {
1288+
write!(f, "{lt} ", lt = lt.print())?;
1289+
}
1290+
write!(f, "{mutability}self", mutability = mutability.print_with_space())
1291+
}
1292+
_ => {
1293+
f.write_str("self: ")?;
1294+
self_ty.print(cx).fmt(f)
1295+
}
1296+
}
1297+
} else {
1298+
if self.is_const {
1299+
write!(f, "const ")?;
1300+
}
1301+
if let Some(name) = self.name {
1302+
write!(f, "{name}: ")?;
1303+
}
1304+
self.type_.print(cx).fmt(f)
1305+
}
1306+
})
1307+
}
1308+
}
1309+
12781310
impl clean::FnDecl {
12791311
pub(crate) fn print(&self, cx: &Context<'_>) -> impl Display {
12801312
fmt::from_fn(move |f| {
@@ -1333,63 +1365,42 @@ impl clean::FnDecl {
13331365
f: &mut fmt::Formatter<'_>,
13341366
cx: &Context<'_>,
13351367
) -> fmt::Result {
1336-
let amp = if f.alternate() { "&" } else { "&amp;" };
1368+
f.write_char('(')?;
13371369

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-
}
1370+
if !self.inputs.is_empty() {
1371+
let line_wrapping_indent = line_wrapping_indent.map(|n| Indent(n + 4));
13441372

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-
}
1352-
clean::BorrowedRef { lifetime, mutability, type_: box clean::SelfTy } => {
1353-
write!(f, "{amp}")?;
1354-
if let Some(lt) = lifetime {
1355-
write!(f, "{lt} ", lt = lt.print())?;
1356-
}
1357-
write!(f, "{mutability}self", mutability = mutability.print_with_space())?;
1358-
}
1359-
_ => {
1360-
write!(f, "self: ")?;
1361-
selfty.print(cx).fmt(f)?;
1362-
}
1363-
}
1364-
} else {
1365-
if param.is_const {
1366-
write!(f, "const ")?;
1367-
}
1368-
if let Some(name) = param.name {
1369-
write!(f, "{name}: ")?;
1373+
if let Some(indent) = line_wrapping_indent {
1374+
write!(f, "\n{indent}")?;
1375+
}
1376+
1377+
let sep = fmt::from_fn(|f| {
1378+
if let Some(indent) = line_wrapping_indent {
1379+
write!(f, ",\n{indent}")
1380+
} else {
1381+
f.write_str(", ")
13701382
}
1371-
param.type_.print(cx).fmt(f)?;
1383+
});
1384+
1385+
self.inputs.iter().map(|param| param.print(cx)).joined(sep, f)?;
1386+
1387+
if line_wrapping_indent.is_some() {
1388+
writeln!(f, ",")?
13721389
}
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, ",")?,
1390+
1391+
if self.c_variadic {
1392+
match line_wrapping_indent {
1393+
None => write!(f, ", ...")?,
1394+
Some(indent) => writeln!(f, "{indent}...")?,
1395+
};
13791396
}
13801397
}
13811398

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

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

13941405
self.print_output(cx).fmt(f)
13951406
}

0 commit comments

Comments
 (0)