Skip to content

Commit 8ba9b8b

Browse files
committed
librustdoc: make notable_traits_button formatting lazy
1 parent 4c1a2c8 commit 8ba9b8b

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ fn assoc_method(
978978
+ name.as_str().len()
979979
+ generics_len;
980980

981-
let notable_traits = notable_traits_button(&d.output, cx);
981+
let notable_traits = notable_traits_button(&d.output, cx).maybe_display();
982982

983983
let (indent, indent_str, end_newline) = if parent == ItemType::Trait {
984984
header_len += 4;
@@ -1005,7 +1005,6 @@ fn assoc_method(
10051005
name = name,
10061006
generics = g.print(cx),
10071007
decl = d.full_print(header_len, indent, cx),
1008-
notable_traits = notable_traits.unwrap_or_default(),
10091008
where_clause = print_where_clause(g, cx, indent, end_newline),
10101009
);
10111010
}
@@ -1453,7 +1452,10 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) ->
14531452
}
14541453
}
14551454

1456-
pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Option<String> {
1455+
pub(crate) fn notable_traits_button<'a, 'tcx>(
1456+
ty: &'a clean::Type,
1457+
cx: &'a Context<'tcx>,
1458+
) -> Option<impl fmt::Display + 'a + Captures<'tcx>> {
14571459
let mut has_notable_trait = false;
14581460

14591461
if ty.is_unit() {
@@ -1495,15 +1497,16 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Optio
14951497
}
14961498
}
14971499

1498-
if has_notable_trait {
1500+
has_notable_trait.then(|| {
14991501
cx.types_with_notable_traits.borrow_mut().insert(ty.clone());
1500-
Some(format!(
1501-
" <a href=\"#\" class=\"tooltip\" data-notable-ty=\"{ty}\">ⓘ</a>",
1502-
ty = Escape(&format!("{:#}", ty.print(cx))),
1503-
))
1504-
} else {
1505-
None
1506-
}
1502+
fmt::from_fn(|f| {
1503+
write!(
1504+
f,
1505+
" <a href=\"#\" class=\"tooltip\" data-notable-ty=\"{ty}\">ⓘ</a>",
1506+
ty = Escape(&format!("{:#}", ty.print(cx))),
1507+
)
1508+
})
1509+
})
15071510
}
15081511

15091512
fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {

src/librustdoc/html/render/print_item.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::
603603
+ name.as_str().len()
604604
+ generics_len;
605605

606-
let notable_traits = notable_traits_button(&f.decl.output, cx);
606+
let notable_traits = notable_traits_button(&f.decl.output, cx).maybe_display();
607607

608608
wrap_item(w, |w| {
609609
w.reserve(header_len);
@@ -621,7 +621,6 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::
621621
generics = f.generics.print(cx),
622622
where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline),
623623
decl = f.decl.full_print(header_len, 0, cx),
624-
notable_traits = notable_traits.unwrap_or_default(),
625624
);
626625
});
627626
write!(w, "{}", document(cx, it, None, HeadingOffset::H2));

0 commit comments

Comments
 (0)