Skip to content

Commit 50b01d5

Browse files
committed
Cleanup notable_traits_decl
1 parent 8713b4c commit 50b01d5

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
@@ -75,7 +75,6 @@ use crate::html::escape::Escape;
7575
use crate::html::format::{
7676
Ending, HrefError, PrintWithSpace, href, print_abi_with_space, print_constness_with_space,
7777
print_default_space, print_generic_bounds, print_where_clause, visibility_print_with_space,
78-
write_str,
7978
};
8079
use crate::html::markdown::{
8180
HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine,
@@ -1682,69 +1681,77 @@ fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Option<impl fmt:
16821681
}
16831682

16841683
fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {
1685-
let mut out = String::new();
1686-
16871684
let did = ty.def_id(cx.cache()).expect("notable_traits_button already checked this");
16881685

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

1691-
for i in impls {
1692-
let impl_ = i.inner_impl();
1693-
if impl_.polarity != ty::ImplPolarity::Positive {
1694-
continue;
1695-
}
1696-
1697-
if !ty.is_doc_subtype_of(&impl_.for_, cx.cache()) {
1698-
// Two different types might have the same did,
1699-
// without actually being the same.
1700-
continue;
1701-
}
1702-
if let Some(trait_) = &impl_.trait_ {
1703-
let trait_did = trait_.def_id();
1704-
1705-
if cx.cache().traits.get(&trait_did).is_some_and(|t| t.is_notable_trait(cx.tcx())) {
1706-
if out.is_empty() {
1707-
write_str(
1708-
&mut out,
1709-
format_args!(
1710-
"<h3>Notable traits for <code>{}</code></h3>\
1711-
<pre><code>",
1712-
impl_.for_.print(cx)
1713-
),
1714-
);
1688+
let out = fmt::from_fn(|f| {
1689+
let mut notable_impls = impls
1690+
.iter()
1691+
.map(|impl_| impl_.inner_impl())
1692+
.filter(|impl_| impl_.polarity == ty::ImplPolarity::Positive)
1693+
.filter(|impl_| {
1694+
// Two different types might have the same did, without actually being the same.
1695+
ty.is_doc_subtype_of(&impl_.for_, cx.cache())
1696+
})
1697+
.filter_map(|impl_| {
1698+
if let Some(trait_) = &impl_.trait_
1699+
&& let trait_did = trait_.def_id()
1700+
&& let Some(trait_) = cx.cache().traits.get(&trait_did)
1701+
&& trait_.is_notable_trait(cx.tcx())
1702+
{
1703+
Some((impl_, trait_did))
1704+
} else {
1705+
None
17151706
}
1707+
})
1708+
.peekable();
17161709

1717-
write_str(
1718-
&mut out,
1719-
format_args!("<div class=\"where\">{}</div>", impl_.print(false, cx)),
1720-
);
1721-
for it in &impl_.items {
1722-
if let clean::AssocTypeItem(ref tydef, ref _bounds) = it.kind {
1723-
let empty_set = FxIndexSet::default();
1724-
let src_link = AssocItemLink::GotoSource(trait_did.into(), &empty_set);
1725-
write_str(
1726-
&mut out,
1727-
format_args!(
1728-
"<div class=\"where\"> {};</div>",
1729-
assoc_type(
1730-
it,
1731-
&tydef.generics,
1732-
&[], // intentionally leaving out bounds
1733-
Some(&tydef.type_),
1734-
src_link,
1735-
0,
1736-
cx,
1737-
)
1738-
),
1739-
);
1740-
}
1741-
}
1710+
let has_notable_impl = if let Some((impl_, _)) = notable_impls.peek() {
1711+
write!(
1712+
f,
1713+
"<h3>Notable traits for <code>{}</code></h3>\
1714+
<pre><code>",
1715+
impl_.for_.print(cx)
1716+
)?;
1717+
true
1718+
} else {
1719+
false
1720+
};
1721+
1722+
for (impl_, trait_did) in notable_impls {
1723+
write!(f, "<div class=\"where\">{}</div>", impl_.print(false, cx))?;
1724+
for it in &impl_.items {
1725+
let clean::AssocTypeItem(tydef, ..) = &it.kind else {
1726+
continue;
1727+
};
1728+
1729+
let empty_set = FxIndexSet::default();
1730+
let src_link = AssocItemLink::GotoSource(trait_did.into(), &empty_set);
1731+
1732+
write!(
1733+
f,
1734+
"<div class=\"where\"> {};</div>",
1735+
assoc_type(
1736+
it,
1737+
&tydef.generics,
1738+
&[], // intentionally leaving out bounds
1739+
Some(&tydef.type_),
1740+
src_link,
1741+
0,
1742+
cx,
1743+
)
1744+
)?;
17421745
}
17431746
}
1744-
}
1745-
if out.is_empty() {
1746-
out.push_str("</code></pre>");
1747-
}
1747+
1748+
if !has_notable_impl {
1749+
f.write_str("</code></pre>")?;
1750+
}
1751+
1752+
Ok(())
1753+
})
1754+
.to_string();
17481755

17491756
(format!("{:#}", ty.print(cx)), out)
17501757
}

0 commit comments

Comments
 (0)