Skip to content

Commit cf097d5

Browse files
committed
librustdoc: make notable_traits_button formatting lazy
1 parent fb33cd2 commit cf097d5

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/librustdoc/html/render/mod.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ fn assoc_method(
984984
+ name.as_str().len()
985985
+ generics_len;
986986

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

989989
let (indent, indent_str, end_newline) = if parent == ItemType::Trait {
990990
header_len += 4;
@@ -1012,7 +1012,6 @@ fn assoc_method(
10121012
name = name,
10131013
generics = g.print(cx),
10141014
decl = d.full_print(header_len, indent, cx),
1015-
notable_traits = notable_traits.unwrap_or_default(),
10161015
where_clause = print_where_clause(g, cx, indent, end_newline),
10171016
),
10181017
);
@@ -1460,7 +1459,10 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) ->
14601459
}
14611460
}
14621461

1463-
pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Option<String> {
1462+
pub(crate) fn notable_traits_button<'a, 'tcx>(
1463+
ty: &'a clean::Type,
1464+
cx: &'a Context<'tcx>,
1465+
) -> Option<impl fmt::Display + 'a + Captures<'tcx>> {
14641466
let mut has_notable_trait = false;
14651467

14661468
if ty.is_unit() {
@@ -1502,15 +1504,16 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Optio
15021504
}
15031505
}
15041506

1505-
if has_notable_trait {
1507+
has_notable_trait.then(|| {
15061508
cx.types_with_notable_traits.borrow_mut().insert(ty.clone());
1507-
Some(format!(
1508-
" <a href=\"#\" class=\"tooltip\" data-notable-ty=\"{ty}\">ⓘ</a>",
1509-
ty = Escape(&format!("{:#}", ty.print(cx))),
1510-
))
1511-
} else {
1512-
None
1513-
}
1509+
fmt::from_fn(|f| {
1510+
write!(
1511+
f,
1512+
" <a href=\"#\" class=\"tooltip\" data-notable-ty=\"{ty}\">ⓘ</a>",
1513+
ty = Escape(&format!("{:#}", ty.print(cx))),
1514+
)
1515+
})
1516+
})
15141517
}
15151518

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

src/librustdoc/html/render/print_item.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ fn item_function(w: &mut String, cx: &Context<'_>, it: &clean::Item, f: &clean::
619619
+ name.as_str().len()
620620
+ generics_len;
621621

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

624624
wrap_item(w, |w| {
625625
w.reserve(header_len);
@@ -638,7 +638,6 @@ fn item_function(w: &mut String, cx: &Context<'_>, it: &clean::Item, f: &clean::
638638
generics = f.generics.print(cx),
639639
where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline),
640640
decl = f.decl.full_print(header_len, 0, cx),
641-
notable_traits = notable_traits.unwrap_or_default(),
642641
),
643642
);
644643
});

0 commit comments

Comments
 (0)