Skip to content

Commit b30f3e7

Browse files
committed
Auto merge of rust-lang#136816 - yotamofek:pr/notable-traits-button-cleanup, r=<try>
Refactor `notable_trait_buttons` 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.
2 parents 4b293d9 + fee4729 commit b30f3e7

File tree

1 file changed

+12
-21
lines changed
  • src/librustdoc/html/render

1 file changed

+12
-21
lines changed

src/librustdoc/html/render/mod.rs

+12-21
Original file line numberDiff line numberDiff line change
@@ -1432,8 +1432,6 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) ->
14321432
}
14331433

14341434
pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Option<String> {
1435-
let mut has_notable_trait = false;
1436-
14371435
if ty.is_unit() {
14381436
// Very common fast path.
14391437
return None;
@@ -1451,27 +1449,20 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &Context<'_>) -> Optio
14511449
return None;
14521450
}
14531451

1454-
if let Some(impls) = cx.cache().impls.get(&did) {
1455-
for i in impls {
1456-
let impl_ = i.inner_impl();
1457-
if impl_.polarity != ty::ImplPolarity::Positive {
1458-
continue;
1459-
}
1460-
1461-
if !ty.is_doc_subtype_of(&impl_.for_, cx.cache()) {
1452+
let impls = cx.cache().impls.get(&did)?;
1453+
let has_notable_trait = impls
1454+
.iter()
1455+
.map(Impl::inner_impl)
1456+
.filter(|impl_| impl_.polarity == ty::ImplPolarity::Positive)
1457+
.filter(|impl_| {
1458+
impl_.polarity == ty::ImplPolarity::Positive
14621459
// Two different types might have the same did,
14631460
// without actually being the same.
1464-
continue;
1465-
}
1466-
if let Some(trait_) = &impl_.trait_ {
1467-
let trait_did = trait_.def_id();
1468-
1469-
if cx.cache().traits.get(&trait_did).is_some_and(|t| t.is_notable_trait(cx.tcx())) {
1470-
has_notable_trait = true;
1471-
}
1472-
}
1473-
}
1474-
}
1461+
&& ty.is_doc_subtype_of(&impl_.for_, cx.cache())
1462+
})
1463+
.filter_map(|impl_| impl_.trait_.as_ref())
1464+
.filter_map(|trait_| cx.cache().traits.get(&trait_.def_id()))
1465+
.any(|t| t.is_notable_trait(cx.tcx()));
14751466

14761467
if has_notable_trait {
14771468
cx.types_with_notable_traits.borrow_mut().insert(ty.clone());

0 commit comments

Comments
 (0)