about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2025-03-16 21:47:43 -0400
committerGitHub <noreply@github.com>2025-03-16 21:47:43 -0400
commit04032ab233130d16a68e0f2a0c55bb0f5ec01d6a (patch)
treed89539b290f96c109b143cc4beb0df6ea6ae8976
parentfeb6cb413246ce9132fbd2a3e5c5f4fa05c94d7e (diff)
parent9d9bac0e968362ff3da4788cc04c65d5f0b6898f (diff)
downloadrust-04032ab233130d16a68e0f2a0c55bb0f5ec01d6a.tar.gz
rust-04032ab233130d16a68e0f2a0c55bb0f5ec01d6a.zip
Rollup merge of #136816 - yotamofek:pr/notable-traits-button-cleanup, r=aDotInTheVoid
refactor `notable_traits_button` to use iterator combinators  instead of for loop

~Small cleanup.
Use `Iterator::any` instead of `for` loop with `predicate = true;`.
I think this makes the code more readable... and also has the additional benefit of short-circuiting the iterator when a notable trait is found (a `break` statement was missing in the `for` loop version, I think). Probably won't be significant enough to show on perf results, though.~

Three commits, each attempting to optimize `notable_trait_buttons` by a little bit.
-rw-r--r--src/librustdoc/html/render/mod.rs32
1 files changed, 11 insertions, 21 deletions
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs
index b2ad2fa773a..8dfde1679fe 100644
--- a/src/librustdoc/html/render/mod.rs
+++ b/src/librustdoc/html/render/mod.rs
@@ -1465,8 +1465,6 @@ pub(crate) fn notable_traits_button(
     ty: &clean::Type,
     cx: &Context<'_>,
 ) -> Option<impl fmt::Display> {
-    let mut has_notable_trait = false;
-
     if ty.is_unit() {
         // Very common fast path.
         return None;
@@ -1484,27 +1482,19 @@ pub(crate) fn notable_traits_button(
         return None;
     }
 
-    if let Some(impls) = cx.cache().impls.get(&did) {
-        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()) {
+    let impls = cx.cache().impls.get(&did)?;
+    let has_notable_trait = impls
+        .iter()
+        .map(Impl::inner_impl)
+        .filter(|impl_| {
+            impl_.polarity == ty::ImplPolarity::Positive
                 // 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())) {
-                    has_notable_trait = true;
-                }
-            }
-        }
-    }
+                && ty.is_doc_subtype_of(&impl_.for_, cx.cache())
+        })
+        .filter_map(|impl_| impl_.trait_.as_ref())
+        .filter_map(|trait_| cx.cache().traits.get(&trait_.def_id()))
+        .any(|t| t.is_notable_trait(cx.tcx()));
 
     has_notable_trait.then(|| {
         cx.types_with_notable_traits.borrow_mut().insert(ty.clone());