Skip to content

Commit d96d541

Browse files
Replace boolean argument for print_where_clause with an enum to make code more clear
1 parent 5b8cf49 commit d96d541

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;
@@ -748,7 +748,7 @@ fn assoc_type(
748748
if !bounds.is_empty() {
749749
write!(w, ": {}", print_generic_bounds(bounds, cx))
750750
}
751-
write!(w, "{}", print_where_clause(generics, cx, indent, false));
751+
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline));
752752
if let Some(default) = default {
753753
write!(w, " = {}", default.print(cx))
754754
}
@@ -797,10 +797,10 @@ fn assoc_method(
797797
header_len += 4;
798798
let indent_str = " ";
799799
render_attributes_in_pre(w, meth, indent_str);
800-
(4, indent_str, false)
800+
(4, indent_str, Ending::NoNewline)
801801
} else {
802802
render_attributes_in_code(w, meth);
803-
(0, "", true)
803+
(0, "", Ending::Newline)
804804
};
805805
w.reserve(header_len + "<a href=\"\" class=\"fnname\">{".len() + "</a>".len());
806806
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
}
@@ -1026,7 +1026,7 @@ fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &
10261026
"trait {}{}{} = {};",
10271027
it.name.unwrap(),
10281028
t.generics.print(cx),
1029-
print_where_clause(&t.generics, cx, 0, true),
1029+
print_where_clause(&t.generics, cx, 0, Ending::Newline),
10301030
bounds(&t.bounds, true, cx)
10311031
);
10321032
});
@@ -1050,7 +1050,7 @@ fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &cl
10501050
"type {}{}{where_clause} = impl {bounds};",
10511051
it.name.unwrap(),
10521052
t.generics.print(cx),
1053-
where_clause = print_where_clause(&t.generics, cx, 0, true),
1053+
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
10541054
bounds = bounds(&t.bounds, false, cx),
10551055
);
10561056
});
@@ -1075,7 +1075,7 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea
10751075
"type {}{}{where_clause} = {type_};",
10761076
it.name.unwrap(),
10771077
t.generics.print(cx),
1078-
where_clause = print_where_clause(&t.generics, cx, 0, true),
1078+
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
10791079
type_ = t.type_.print(cx),
10801080
);
10811081
});
@@ -1786,7 +1786,7 @@ fn render_struct(
17861786
}
17871787
w.write_str(")");
17881788
if let Some(g) = g {
1789-
write!(w, "{}", print_where_clause(g, cx, 0, false));
1789+
write!(w, "{}", print_where_clause(g, cx, 0, Ending::NoNewline));
17901790
}
17911791
// We only want a ";" when we are displaying a tuple struct, not a variant tuple struct.
17921792
if structhead {
@@ -1796,7 +1796,7 @@ fn render_struct(
17961796
CtorKind::Const => {
17971797
// Needed for PhantomData.
17981798
if let Some(g) = g {
1799-
write!(w, "{}", print_where_clause(g, cx, 0, false));
1799+
write!(w, "{}", print_where_clause(g, cx, 0, Ending::NoNewline));
18001800
}
18011801
w.write_str(";");
18021802
}

0 commit comments

Comments
 (0)