Skip to content

Commit 82fecf0

Browse files
committed
Cleanup notable_traits_decl
1 parent 78ff5dd commit 82fecf0

File tree

1 file changed

+63
-56
lines changed
  • src/librustdoc/html/render

1 file changed

+63
-56
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ use crate::html::escape::Escape;
7777
use crate::html::format::{
7878
Ending, HrefError, PrintWithSpace, href, print_abi_with_space, print_constness_with_space,
7979
print_default_space, print_generic_bounds, print_where_clause, visibility_print_with_space,
80-
write_str,
8180
};
8281
use crate::html::markdown::{
8382
HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine,
@@ -1647,69 +1646,77 @@ fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Option<impl fmt:
16471646
}
16481647

16491648
fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {
1650-
let mut out = String::new();
1651-
16521649
let did = ty.def_id(cx.cache()).expect("notable_traits_button already checked this");
16531650

16541651
let impls = cx.cache().impls.get(&did).expect("notable_traits_button already checked this");
16551652

1656-
for i in impls {
1657-
let impl_ = i.inner_impl();
1658-
if impl_.polarity != ty::ImplPolarity::Positive {
1659-
continue;
1660-
}
1661-
1662-
if !ty.is_doc_subtype_of(&impl_.for_, cx.cache()) {
1663-
// Two different types might have the same did,
1664-
// without actually being the same.
1665-
continue;
1666-
}
1667-
if let Some(trait_) = &impl_.trait_ {
1668-
let trait_did = trait_.def_id();
1669-
1670-
if cx.cache().traits.get(&trait_did).is_some_and(|t| t.is_notable_trait(cx.tcx())) {
1671-
if out.is_empty() {
1672-
write_str(
1673-
&mut out,
1674-
format_args!(
1675-
"<h3>Notable traits for <code>{}</code></h3>\
1676-
<pre><code>",
1677-
impl_.for_.print(cx)
1678-
),
1679-
);
1653+
let out = fmt::from_fn(|f| {
1654+
let mut notable_impls = impls
1655+
.iter()
1656+
.map(|impl_| impl_.inner_impl())
1657+
.filter(|impl_| impl_.polarity == ty::ImplPolarity::Positive)
1658+
.filter(|impl_| {
1659+
// Two different types might have the same did, without actually being the same.
1660+
ty.is_doc_subtype_of(&impl_.for_, cx.cache())
1661+
})
1662+
.filter_map(|impl_| {
1663+
if let Some(trait_) = &impl_.trait_
1664+
&& let trait_did = trait_.def_id()
1665+
&& let Some(trait_) = cx.cache().traits.get(&trait_did)
1666+
&& trait_.is_notable_trait(cx.tcx())
1667+
{
1668+
Some((impl_, trait_did))
1669+
} else {
1670+
None
16801671
}
1672+
})
1673+
.peekable();
16811674

1682-
write_str(
1683-
&mut out,
1684-
format_args!("<div class=\"where\">{}</div>", impl_.print(false, cx)),
1685-
);
1686-
for it in &impl_.items {
1687-
if let clean::AssocTypeItem(ref tydef, ref _bounds) = it.kind {
1688-
let empty_set = FxIndexSet::default();
1689-
let src_link = AssocItemLink::GotoSource(trait_did.into(), &empty_set);
1690-
write_str(
1691-
&mut out,
1692-
format_args!(
1693-
"<div class=\"where\"> {};</div>",
1694-
assoc_type(
1695-
it,
1696-
&tydef.generics,
1697-
&[], // intentionally leaving out bounds
1698-
Some(&tydef.type_),
1699-
src_link,
1700-
0,
1701-
cx,
1702-
)
1703-
),
1704-
);
1705-
}
1706-
}
1675+
let has_notable_impl = if let Some((impl_, _)) = notable_impls.peek() {
1676+
write!(
1677+
f,
1678+
"<h3>Notable traits for <code>{}</code></h3>\
1679+
<pre><code>",
1680+
impl_.for_.print(cx)
1681+
)?;
1682+
true
1683+
} else {
1684+
false
1685+
};
1686+
1687+
for (impl_, trait_did) in notable_impls {
1688+
write!(f, "<div class=\"where\">{}</div>", impl_.print(false, cx))?;
1689+
for it in &impl_.items {
1690+
let clean::AssocTypeItem(tydef, ..) = &it.kind else {
1691+
continue;
1692+
};
1693+
1694+
let empty_set = FxIndexSet::default();
1695+
let src_link = AssocItemLink::GotoSource(trait_did.into(), &empty_set);
1696+
1697+
write!(
1698+
f,
1699+
"<div class=\"where\"> {};</div>",
1700+
assoc_type(
1701+
it,
1702+
&tydef.generics,
1703+
&[], // intentionally leaving out bounds
1704+
Some(&tydef.type_),
1705+
src_link,
1706+
0,
1707+
cx,
1708+
)
1709+
)?;
17071710
}
17081711
}
1709-
}
1710-
if out.is_empty() {
1711-
out.push_str("</code></pre>");
1712-
}
1712+
1713+
if !has_notable_impl {
1714+
f.write_str("</code></pre>")?;
1715+
}
1716+
1717+
Ok(())
1718+
})
1719+
.to_string();
17131720

17141721
(format!("{:#}", ty.print(cx)), out)
17151722
}

0 commit comments

Comments
 (0)