Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/item_scope.rs')
-rw-r--r--crates/hir-def/src/item_scope.rs32
1 files changed, 31 insertions, 1 deletions
diff --git a/crates/hir-def/src/item_scope.rs b/crates/hir-def/src/item_scope.rs
index 2b059d1f8d..a60b9f9f3a 100644
--- a/crates/hir-def/src/item_scope.rs
+++ b/crates/hir-def/src/item_scope.rs
@@ -277,13 +277,43 @@ impl ItemScope {
ItemInNs::Types(def) => self.types.iter().find_map(|(name, &(other_def, vis, i))| {
(other_def == def).then_some((name, vis, i.is_none()))
}),
-
ItemInNs::Values(def) => self.values.iter().find_map(|(name, &(other_def, vis, i))| {
(other_def == def).then_some((name, vis, i.is_none()))
}),
}
}
+ /// XXX: this is O(N) rather than O(1), try to not introduce new usages.
+ pub(crate) fn names_of<T>(
+ &self,
+ item: ItemInNs,
+ mut cb: impl FnMut(&Name, Visibility, bool) -> Option<T>,
+ ) -> Option<T> {
+ match item {
+ ItemInNs::Macros(def) => self
+ .macros
+ .iter()
+ .filter_map(|(name, &(other_def, vis, i))| {
+ (other_def == def).then_some((name, vis, i.is_none()))
+ })
+ .find_map(|(a, b, c)| cb(a, b, c)),
+ ItemInNs::Types(def) => self
+ .types
+ .iter()
+ .filter_map(|(name, &(other_def, vis, i))| {
+ (other_def == def).then_some((name, vis, i.is_none()))
+ })
+ .find_map(|(a, b, c)| cb(a, b, c)),
+ ItemInNs::Values(def) => self
+ .values
+ .iter()
+ .filter_map(|(name, &(other_def, vis, i))| {
+ (other_def == def).then_some((name, vis, i.is_none()))
+ })
+ .find_map(|(a, b, c)| cb(a, b, c)),
+ }
+ }
+
pub(crate) fn traits(&self) -> impl Iterator<Item = TraitId> + '_ {
self.types
.values()