Skip to content

Commit ec0c156

Browse files
authored
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``
2 parents 16ad2f5 + d96d541 commit ec0c156

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

src/librustdoc/html/format.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,20 @@ impl clean::Generics {
268268
}
269269
}
270270

271+
#[derive(Clone, Copy, PartialEq, Eq)]
272+
pub(crate) enum Ending {
273+
Newline,
274+
NoNewline,
275+
}
276+
271277
/// * The Generics from which to emit a where-clause.
272278
/// * The number of spaces to indent each line with.
273279
/// * Whether the where-clause needs to add a comma and newline after the last bound.
274280
pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
275281
gens: &'a clean::Generics,
276282
cx: &'a Context<'tcx>,
277283
indent: usize,
278-
end_newline: bool,
284+
ending: Ending,
279285
) -> impl fmt::Display + 'a + Captures<'tcx> {
280286
use fmt::Write;
281287

@@ -342,7 +348,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
342348

343349
let where_preds = comma_sep(where_predicates, false);
344350
let clause = if f.alternate() {
345-
if end_newline {
351+
if ending == Ending::Newline {
346352
// add a space so stripping <br> tags and breaking spaces still renders properly
347353
format!(" where{where_preds}, ")
348354
} else {
@@ -356,7 +362,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
356362
}
357363
let where_preds = where_preds.to_string().replace("<br>", &br_with_padding);
358364

359-
if end_newline {
365+
if ending == Ending::Newline {
360366
let mut clause = "&nbsp;".repeat(indent.saturating_sub(1));
361367
// add a space so stripping <br> tags and breaking spaces still renders properly
362368
write!(
@@ -1167,7 +1173,7 @@ impl clean::Impl {
11671173
fmt_type(&self.for_, f, use_absolute, cx)?;
11681174
}
11691175

1170-
fmt::Display::fmt(&print_where_clause(&self.generics, cx, 0, true), f)?;
1176+
fmt::Display::fmt(&print_where_clause(&self.generics, cx, 0, Ending::Newline), f)?;
11711177
Ok(())
11721178
})
11731179
}

src/librustdoc/html/render/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use crate::formats::{AssocItemRender, Impl, RenderMode};
7070
use crate::html::escape::Escape;
7171
use crate::html::format::{
7272
href, join_with_double_colon, print_abi_with_space, print_constness_with_space,
73-
print_default_space, print_generic_bounds, print_where_clause, Buffer, HrefError,
73+
print_default_space, print_generic_bounds, print_where_clause, Buffer, Ending, HrefError,
7474
PrintWithSpace,
7575
};
7676
use crate::html::highlight;
@@ -747,7 +747,7 @@ fn assoc_type(
747747
if !bounds.is_empty() {
748748
write!(w, ": {}", print_generic_bounds(bounds, cx))
749749
}
750-
write!(w, "{}", print_where_clause(generics, cx, indent, false));
750+
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline));
751751
if let Some(default) = default {
752752
write!(w, " = {}", default.print(cx))
753753
}
@@ -796,10 +796,10 @@ fn assoc_method(
796796
header_len += 4;
797797
let indent_str = " ";
798798
render_attributes_in_pre(w, meth, indent_str);
799-
(4, indent_str, false)
799+
(4, indent_str, Ending::NoNewline)
800800
} else {
801801
render_attributes_in_code(w, meth);
802-
(0, "", true)
802+
(0, "", Ending::Newline)
803803
};
804804
w.reserve(header_len + "<a href=\"\" class=\"fnname\">{".len() + "</a>".len());
805805
write!(

src/librustdoc/html/render/print_item.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::formats::{AssocItemRender, Impl, RenderMode};
2929
use crate::html::escape::Escape;
3030
use crate::html::format::{
3131
join_with_double_colon, print_abi_with_space, print_constness_with_space, print_where_clause,
32-
Buffer, PrintWithSpace,
32+
Buffer, Ending, PrintWithSpace,
3333
};
3434
use crate::html::highlight;
3535
use crate::html::layout::Page;
@@ -69,7 +69,7 @@ fn print_where_clause_and_check<'a, 'tcx: 'a>(
6969
cx: &'a Context<'tcx>,
7070
) -> bool {
7171
let len_before = buffer.len();
72-
write!(buffer, "{}", print_where_clause(gens, cx, 0, true));
72+
write!(buffer, "{}", print_where_clause(gens, cx, 0, Ending::Newline));
7373
len_before != buffer.len()
7474
}
7575

@@ -519,7 +519,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
519519
abi = abi,
520520
name = name,
521521
generics = f.generics.print(cx),
522-
where_clause = print_where_clause(&f.generics, cx, 0, true),
522+
where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline),
523523
decl = f.decl.full_print(header_len, 0, header.asyncness, cx),
524524
notable_traits = notable_traits_decl(&f.decl, cx),
525525
);
@@ -556,7 +556,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
556556
);
557557

558558
if !t.generics.where_predicates.is_empty() {
559-
write!(w, "{}", print_where_clause(&t.generics, cx, 0, true));
559+
write!(w, "{}", print_where_clause(&t.generics, cx, 0, Ending::Newline));
560560
} else {
561561
w.write_str(" ");
562562
}
@@ -1025,7 +1025,7 @@ fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &
10251025
"trait {}{}{} = {};",
10261026
it.name.unwrap(),
10271027
t.generics.print(cx),
1028-
print_where_clause(&t.generics, cx, 0, true),
1028+
print_where_clause(&t.generics, cx, 0, Ending::Newline),
10291029
bounds(&t.bounds, true, cx)
10301030
);
10311031
});
@@ -1049,7 +1049,7 @@ fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &cl
10491049
"type {}{}{where_clause} = impl {bounds};",
10501050
it.name.unwrap(),
10511051
t.generics.print(cx),
1052-
where_clause = print_where_clause(&t.generics, cx, 0, true),
1052+
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
10531053
bounds = bounds(&t.bounds, false, cx),
10541054
);
10551055
});
@@ -1074,7 +1074,7 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea
10741074
"type {}{}{where_clause} = {type_};",
10751075
it.name.unwrap(),
10761076
t.generics.print(cx),
1077-
where_clause = print_where_clause(&t.generics, cx, 0, true),
1077+
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
10781078
type_ = t.type_.print(cx),
10791079
);
10801080
});
@@ -1784,7 +1784,7 @@ fn render_struct(
17841784
}
17851785
w.write_str(")");
17861786
if let Some(g) = g {
1787-
write!(w, "{}", print_where_clause(g, cx, 0, false));
1787+
write!(w, "{}", print_where_clause(g, cx, 0, Ending::NoNewline));
17881788
}
17891789
// We only want a ";" when we are displaying a tuple struct, not a variant tuple struct.
17901790
if structhead {
@@ -1794,7 +1794,7 @@ fn render_struct(
17941794
CtorKind::Const => {
17951795
// Needed for PhantomData.
17961796
if let Some(g) = g {
1797-
write!(w, "{}", print_where_clause(g, cx, 0, false));
1797+
write!(w, "{}", print_where_clause(g, cx, 0, Ending::NoNewline));
17981798
}
17991799
w.write_str(";");
18001800
}

0 commit comments

Comments
 (0)