Skip to content

Commit 0af1431

Browse files
committed
make print_where_clause return Option<impl fmt::Display>
1 parent 986bec0 commit 0af1431

File tree

3 files changed

+62
-41
lines changed

3 files changed

+62
-41
lines changed

src/librustdoc/html/format.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use super::url_parts_builder::{UrlPartsBuilder, estimate_item_path_byte_length};
3030
use crate::clean::types::ExternalLocation;
3131
use crate::clean::utils::find_nearest_parent_module;
3232
use crate::clean::{self, ExternalCrate, PrimitiveType};
33-
use crate::display::Joined as _;
33+
use crate::display::{Joined as _, MaybeDisplay as _};
3434
use crate::formats::cache::Cache;
3535
use crate::formats::item_type::ItemType;
3636
use crate::html::escape::{Escape, EscapeBodyText};
@@ -178,12 +178,12 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
178178
cx: &'a Context<'tcx>,
179179
indent: usize,
180180
ending: Ending,
181-
) -> impl Display + 'a + Captures<'tcx> {
182-
fmt::from_fn(move |f| {
183-
if gens.where_predicates.is_empty() {
184-
return Ok(());
185-
}
181+
) -> Option<impl Display + 'a + Captures<'tcx>> {
182+
if gens.where_predicates.is_empty() {
183+
return None;
184+
}
186185

186+
Some(fmt::from_fn(move |f| {
187187
let where_preds = fmt::from_fn(|f| {
188188
gens.where_predicates
189189
.iter()
@@ -246,7 +246,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
246246
}
247247
};
248248
write!(f, "{clause}")
249-
})
249+
}))
250250
}
251251

252252
impl clean::Lifetime {
@@ -1179,7 +1179,7 @@ impl clean::Impl {
11791179
self.print_type(&self.for_, f, use_absolute, cx)?;
11801180
}
11811181

1182-
print_where_clause(&self.generics, cx, 0, Ending::Newline).fmt(f)
1182+
print_where_clause(&self.generics, cx, 0, Ending::Newline).maybe_display().fmt(f)
11831183
})
11841184
}
11851185
fn print_type<'a, 'tcx: 'a>(

src/librustdoc/html/render/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ fn assoc_const<'a, 'tcx>(
913913
write!(w, " = {}", Escape(&repr))?;
914914
}
915915
}
916-
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline))
916+
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline).maybe_display())
917917
})
918918
}
919919

@@ -943,7 +943,7 @@ fn assoc_type<'a, 'tcx>(
943943
if let Some(default) = default {
944944
write!(w, " = {}", default.print(cx))?;
945945
}
946-
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline))
946+
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline).maybe_display())
947947
})
948948
}
949949

@@ -1008,7 +1008,7 @@ fn assoc_method<'a, 'tcx>(
10081008
indent = indent_str,
10091009
generics = g.print(cx),
10101010
decl = d.full_print(header_len, indent, cx),
1011-
where_clause = print_where_clause(g, cx, indent, end_newline),
1011+
where_clause = print_where_clause(g, cx, indent, end_newline).maybe_display(),
10121012
)
10131013
})
10141014
}

src/librustdoc/html/render/print_item.rs

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,6 @@ struct ItemVars<'a> {
162162
src_href: Option<&'a str>,
163163
}
164164

165-
/// Calls `print_where_clause` and returns `true` if a `where` clause was generated.
166-
fn print_where_clause_and_check<'a, 'tcx: 'a>(
167-
buffer: &mut String,
168-
gens: &'a clean::Generics,
169-
cx: &'a Context<'tcx>,
170-
) -> bool {
171-
let len_before = buffer.len();
172-
write_str(buffer, format_args!("{}", print_where_clause(gens, cx, 0, Ending::Newline)));
173-
len_before != buffer.len()
174-
}
175-
176165
pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut String) {
177166
debug_assert!(!item.is_stripped());
178167
let typ = match item.kind {
@@ -638,7 +627,8 @@ fn item_function<'a, 'tcx>(
638627
abi = abi,
639628
name = name,
640629
generics = f.generics.print(cx),
641-
where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline),
630+
where_clause =
631+
print_where_clause(&f.generics, cx, 0, Ending::Newline).maybe_display(),
642632
decl = f.decl.full_print(header_len, 0, cx),
643633
)
644634
})?;
@@ -682,7 +672,11 @@ fn item_trait<'a, 'tcx>(
682672
)?;
683673

684674
if !t.generics.where_predicates.is_empty() {
685-
write!(w, "{}", print_where_clause(&t.generics, cx, 0, Ending::Newline))?;
675+
write!(
676+
w,
677+
"{}",
678+
print_where_clause(&t.generics, cx, 0, Ending::Newline).maybe_display()
679+
)?;
686680
} else {
687681
w.write_char(' ')?;
688682
}
@@ -1252,7 +1246,7 @@ fn item_trait_alias(
12521246
attrs = render_attributes_in_pre(it, "", cx),
12531247
name = it.name.unwrap(),
12541248
generics = t.generics.print(cx),
1255-
where_b = print_where_clause(&t.generics, cx, 0, Ending::Newline),
1249+
where_b = print_where_clause(&t.generics, cx, 0, Ending::Newline).maybe_display(),
12561250
bounds = bounds(&t.bounds, true, cx),
12571251
)
12581252
.unwrap();
@@ -1277,7 +1271,8 @@ fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean
12771271
vis = visibility_print_with_space(it, cx),
12781272
name = it.name.unwrap(),
12791273
generics = t.generics.print(cx),
1280-
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
1274+
where_clause =
1275+
print_where_clause(&t.generics, cx, 0, Ending::Newline).maybe_display(),
12811276
type_ = t.type_.print(cx),
12821277
),
12831278
);
@@ -1651,9 +1646,13 @@ fn render_enum_fields(
16511646
enum_def_id: DefId,
16521647
) {
16531648
let should_show_enum_discriminant = should_show_enum_discriminant(cx, enum_def_id, variants);
1654-
if !g.is_some_and(|g| print_where_clause_and_check(w, g, cx)) {
1649+
if let Some(generics) = g
1650+
&& let Some(where_clause) = print_where_clause(generics, cx, 0, Ending::Newline)
1651+
{
1652+
write_str(w, format_args!("{where_clause}"));
1653+
} else {
16551654
// If there wasn't a `where` clause, we add a whitespace.
1656-
w.push_str(" ");
1655+
w.push(' ')
16571656
}
16581657

16591658
let variants_stripped = has_stripped_entries;
@@ -1930,7 +1929,8 @@ fn item_constant(
19301929
name = it.name.unwrap(),
19311930
generics = generics.print(cx),
19321931
typ = ty.print(cx),
1933-
where_clause = print_where_clause(generics, cx, 0, Ending::NoNewline)
1932+
where_clause =
1933+
print_where_clause(generics, cx, 0, Ending::NoNewline).maybe_display(),
19341934
),
19351935
);
19361936

@@ -2295,14 +2295,17 @@ fn render_union<'a, 'cx: 'a>(
22952295
fmt::from_fn(move |mut f| {
22962296
write!(f, "{}union {}", visibility_print_with_space(it, cx), it.name.unwrap(),)?;
22972297

2298-
let where_displayed = g
2299-
.map(|g| {
2300-
let mut buf = g.print(cx).to_string();
2301-
let where_displayed = print_where_clause_and_check(&mut buf, g, cx);
2302-
f.write_str(&buf).unwrap();
2303-
where_displayed
2304-
})
2305-
.unwrap_or(false);
2298+
let where_displayed = if let Some(generics) = g {
2299+
write!(f, "{}", generics.print(cx))?;
2300+
if let Some(where_clause) = print_where_clause(generics, cx, 0, Ending::Newline) {
2301+
write!(f, "{where_clause}")?;
2302+
true
2303+
} else {
2304+
false
2305+
}
2306+
} else {
2307+
false
2308+
};
23062309

23072310
// If there wasn't a `where` clause, we add a whitespace.
23082311
if !where_displayed {
@@ -2386,8 +2389,14 @@ fn render_struct_fields(
23862389
) {
23872390
match ty {
23882391
None => {
2389-
let where_displayed =
2390-
g.map(|g| print_where_clause_and_check(w, g, cx)).unwrap_or(false);
2392+
let where_displayed = if let Some(generics) = g
2393+
&& let Some(where_clause) = print_where_clause(generics, cx, 0, Ending::Newline)
2394+
{
2395+
write_str(w, format_args!("{where_clause}"));
2396+
true
2397+
} else {
2398+
false
2399+
};
23912400

23922401
// If there wasn't a `where` clause, we add a whitespace.
23932402
if !where_displayed {
@@ -2467,7 +2476,13 @@ fn render_struct_fields(
24672476
}
24682477
w.push_str(")");
24692478
if let Some(g) = g {
2470-
write_str(w, format_args!("{}", print_where_clause(g, cx, 0, Ending::NoNewline)));
2479+
write_str(
2480+
w,
2481+
format_args!(
2482+
"{}",
2483+
print_where_clause(g, cx, 0, Ending::NoNewline).maybe_display()
2484+
),
2485+
);
24712486
}
24722487
// We only want a ";" when we are displaying a tuple struct, not a variant tuple struct.
24732488
if structhead {
@@ -2477,7 +2492,13 @@ fn render_struct_fields(
24772492
Some(CtorKind::Const) => {
24782493
// Needed for PhantomData.
24792494
if let Some(g) = g {
2480-
write_str(w, format_args!("{}", print_where_clause(g, cx, 0, Ending::NoNewline)));
2495+
write_str(
2496+
w,
2497+
format_args!(
2498+
"{}",
2499+
print_where_clause(g, cx, 0, Ending::NoNewline).maybe_display()
2500+
),
2501+
);
24812502
}
24822503
w.push_str(";");
24832504
}

0 commit comments

Comments
 (0)