) -> (String, String) {
- let mut out = String::new();
-
let did = ty.def_id(cx.cache()).expect("notable_traits_button already checked this");
let impls = cx.cache().impls.get(&did).expect("notable_traits_button already checked this");
- for i in impls {
- let impl_ = i.inner_impl();
- if impl_.polarity != ty::ImplPolarity::Positive {
- continue;
- }
-
- if !ty.is_doc_subtype_of(&impl_.for_, cx.cache()) {
- // Two different types might have the same did,
- // without actually being the same.
- continue;
- }
- if let Some(trait_) = &impl_.trait_ {
- let trait_did = trait_.def_id();
-
- if cx.cache().traits.get(&trait_did).is_some_and(|t| t.is_notable_trait(cx.tcx())) {
- if out.is_empty() {
- write_str(
- &mut out,
- format_args!(
- "Notable traits for {}
\
- ",
- impl_.for_.print(cx)
- ),
- );
+ let out = fmt::from_fn(|f| {
+ let mut notable_impls = impls
+ .iter()
+ .map(|impl_| impl_.inner_impl())
+ .filter(|impl_| impl_.polarity == ty::ImplPolarity::Positive)
+ .filter(|impl_| {
+ // Two different types might have the same did, without actually being the same.
+ ty.is_doc_subtype_of(&impl_.for_, cx.cache())
+ })
+ .filter_map(|impl_| {
+ if let Some(trait_) = &impl_.trait_
+ && let trait_did = trait_.def_id()
+ && let Some(trait_) = cx.cache().traits.get(&trait_did)
+ && trait_.is_notable_trait(cx.tcx())
+ {
+ Some((impl_, trait_did))
+ } else {
+ None
}
+ })
+ .peekable();
- write_str(
- &mut out,
- format_args!("{}
", impl_.print(false, cx)),
- );
- for it in &impl_.items {
- if let clean::AssocTypeItem(ref tydef, ref _bounds) = it.kind {
- let empty_set = FxIndexSet::default();
- let src_link = AssocItemLink::GotoSource(trait_did.into(), &empty_set);
- write_str(
- &mut out,
- format_args!(
- " {};
",
- assoc_type(
- it,
- &tydef.generics,
- &[], // intentionally leaving out bounds
- Some(&tydef.type_),
- src_link,
- 0,
- cx,
- )
- ),
- );
- }
- }
+ let has_notable_impl = if let Some((impl_, _)) = notable_impls.peek() {
+ write!(
+ f,
+ "Notable traits for {}
\
+ ",
+ impl_.for_.print(cx)
+ )?;
+ true
+ } else {
+ false
+ };
+
+ for (impl_, trait_did) in notable_impls {
+ write!(f, "{}
", impl_.print(false, cx))?;
+ for it in &impl_.items {
+ let clean::AssocTypeItem(tydef, ..) = &it.kind else {
+ continue;
+ };
+
+ let empty_set = FxIndexSet::default();
+ let src_link = AssocItemLink::GotoSource(trait_did.into(), &empty_set);
+
+ write!(
+ f,
+ " {};
",
+ assoc_type(
+ it,
+ &tydef.generics,
+ &[], // intentionally leaving out bounds
+ Some(&tydef.type_),
+ src_link,
+ 0,
+ cx,
+ )
+ )?;
}
}
- }
- if out.is_empty() {
- out.push_str("
");
- }
+
+ if !has_notable_impl {
+ f.write_str("
")?;
+ }
+
+ Ok(())
+ })
+ .to_string();
(format!("{:#}", ty.print(cx)), out)
}