Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/librustdoc/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ 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<I, T> Joined for I
where
I: IntoIterator<Item = T>,
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(())
Expand Down
107 changes: 59 additions & 48 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 { "&amp;" })?;
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| {
Expand Down Expand Up @@ -1333,63 +1365,42 @@ impl clean::FnDecl {
f: &mut fmt::Formatter<'_>,
cx: &Context<'_>,
) -> fmt::Result {
let amp = if f.alternate() { "&" } else { "&amp;" };
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)
}
Expand Down
Loading