Rollup merge of #99017 - GuillaumeGomez:rustdoc-ending-enum, r=notriddle

Replace boolean argument for print_where_clause with an enum to make code more clear

As you suggested ``@notriddle.`` Just not sure if the naming seems good to you?

r? ``@notriddle``
This commit is contained in:
Matthias Krüger 2022-07-07 20:33:32 +02:00 committed by GitHub
commit ec0c1560be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 17 deletions

View File

@ -268,6 +268,12 @@ impl clean::Generics {
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Ending {
Newline,
NoNewline,
}
/// * The Generics from which to emit a where-clause.
/// * The number of spaces to indent each line with.
/// * Whether the where-clause needs to add a comma and newline after the last bound.
@ -275,7 +281,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
gens: &'a clean::Generics,
cx: &'a Context<'tcx>,
indent: usize,
end_newline: bool,
ending: Ending,
) -> impl fmt::Display + 'a + Captures<'tcx> {
use fmt::Write;
@ -342,7 +348,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
let where_preds = comma_sep(where_predicates, false);
let clause = if f.alternate() {
if end_newline {
if ending == Ending::Newline {
// add a space so stripping <br> tags and breaking spaces still renders properly
format!(" where{where_preds}, ")
} else {
@ -356,7 +362,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
}
let where_preds = where_preds.to_string().replace("<br>", &br_with_padding);
if end_newline {
if ending == Ending::Newline {
let mut clause = "&nbsp;".repeat(indent.saturating_sub(1));
// add a space so stripping <br> tags and breaking spaces still renders properly
write!(
@ -1167,7 +1173,7 @@ impl clean::Impl {
fmt_type(&self.for_, f, use_absolute, cx)?;
}
fmt::Display::fmt(&print_where_clause(&self.generics, cx, 0, true), f)?;
fmt::Display::fmt(&print_where_clause(&self.generics, cx, 0, Ending::Newline), f)?;
Ok(())
})
}

View File

@ -70,7 +70,7 @@ use crate::formats::{AssocItemRender, Impl, RenderMode};
use crate::html::escape::Escape;
use crate::html::format::{
href, join_with_double_colon, print_abi_with_space, print_constness_with_space,
print_default_space, print_generic_bounds, print_where_clause, Buffer, HrefError,
print_default_space, print_generic_bounds, print_where_clause, Buffer, Ending, HrefError,
PrintWithSpace,
};
use crate::html::highlight;
@ -747,7 +747,7 @@ fn assoc_type(
if !bounds.is_empty() {
write!(w, ": {}", print_generic_bounds(bounds, cx))
}
write!(w, "{}", print_where_clause(generics, cx, indent, false));
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline));
if let Some(default) = default {
write!(w, " = {}", default.print(cx))
}
@ -796,10 +796,10 @@ fn assoc_method(
header_len += 4;
let indent_str = " ";
render_attributes_in_pre(w, meth, indent_str);
(4, indent_str, false)
(4, indent_str, Ending::NoNewline)
} else {
render_attributes_in_code(w, meth);
(0, "", true)
(0, "", Ending::Newline)
};
w.reserve(header_len + "<a href=\"\" class=\"fnname\">{".len() + "</a>".len());
write!(

View File

@ -29,7 +29,7 @@ use crate::formats::{AssocItemRender, Impl, RenderMode};
use crate::html::escape::Escape;
use crate::html::format::{
join_with_double_colon, print_abi_with_space, print_constness_with_space, print_where_clause,
Buffer, PrintWithSpace,
Buffer, Ending, PrintWithSpace,
};
use crate::html::highlight;
use crate::html::layout::Page;
@ -69,7 +69,7 @@ fn print_where_clause_and_check<'a, 'tcx: 'a>(
cx: &'a Context<'tcx>,
) -> bool {
let len_before = buffer.len();
write!(buffer, "{}", print_where_clause(gens, cx, 0, true));
write!(buffer, "{}", print_where_clause(gens, cx, 0, Ending::Newline));
len_before != buffer.len()
}
@ -519,7 +519,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
abi = abi,
name = name,
generics = f.generics.print(cx),
where_clause = print_where_clause(&f.generics, cx, 0, true),
where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline),
decl = f.decl.full_print(header_len, 0, header.asyncness, cx),
notable_traits = notable_traits_decl(&f.decl, cx),
);
@ -556,7 +556,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
);
if !t.generics.where_predicates.is_empty() {
write!(w, "{}", print_where_clause(&t.generics, cx, 0, true));
write!(w, "{}", print_where_clause(&t.generics, cx, 0, Ending::Newline));
} else {
w.write_str(" ");
}
@ -1025,7 +1025,7 @@ fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &
"trait {}{}{} = {};",
it.name.unwrap(),
t.generics.print(cx),
print_where_clause(&t.generics, cx, 0, true),
print_where_clause(&t.generics, cx, 0, Ending::Newline),
bounds(&t.bounds, true, cx)
);
});
@ -1049,7 +1049,7 @@ fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &cl
"type {}{}{where_clause} = impl {bounds};",
it.name.unwrap(),
t.generics.print(cx),
where_clause = print_where_clause(&t.generics, cx, 0, true),
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
bounds = bounds(&t.bounds, false, cx),
);
});
@ -1074,7 +1074,7 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea
"type {}{}{where_clause} = {type_};",
it.name.unwrap(),
t.generics.print(cx),
where_clause = print_where_clause(&t.generics, cx, 0, true),
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
type_ = t.type_.print(cx),
);
});
@ -1784,7 +1784,7 @@ fn render_struct(
}
w.write_str(")");
if let Some(g) = g {
write!(w, "{}", print_where_clause(g, cx, 0, false));
write!(w, "{}", print_where_clause(g, cx, 0, Ending::NoNewline));
}
// We only want a ";" when we are displaying a tuple struct, not a variant tuple struct.
if structhead {
@ -1794,7 +1794,7 @@ fn render_struct(
CtorKind::Const => {
// Needed for PhantomData.
if let Some(g) = g {
write!(w, "{}", print_where_clause(g, cx, 0, false));
write!(w, "{}", print_where_clause(g, cx, 0, Ending::NoNewline));
}
w.write_str(";");
}