From 6241567948b644739d2ae904e74f5f10cd5b2bb6 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 15 Mar 2021 13:49:21 +0100 Subject: Speedup trait impl search for goto_implementation --- crates/hir_ty/src/method_resolution.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'crates/hir_ty/src/method_resolution.rs') diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index c7055bee5..57f37ef4b 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -44,7 +44,7 @@ impl TyFingerprint { /// Creates a TyFingerprint for looking up an impl. Only certain types can /// have impls: if we have some `struct S`, we can have an `impl S`, but not /// `impl &S`. Hence, this will return `None` for reference types and such. - pub(crate) fn for_impl(ty: &Ty) -> Option { + pub fn for_impl(ty: &Ty) -> Option { let fp = match *ty.interned(&Interner) { TyKind::Str => TyFingerprint::Str, TyKind::Never => TyFingerprint::Never, @@ -141,6 +141,14 @@ impl TraitImpls { } } + /// Queries all trait impls for the given type. + pub fn for_self_ty(&self, fp: TyFingerprint) -> impl Iterator + '_ { + self.map + .values() + .flat_map(move |impls| impls.get(&None).into_iter().chain(impls.get(&Some(fp)))) + .flat_map(|it| it.iter().copied()) + } + /// Queries all impls of the given trait. pub fn for_trait(&self, trait_: TraitId) -> impl Iterator + '_ { self.map -- cgit v1.2.3