Skip to content

Commit f08c0cd

Browse files
Simplify
1 parent 7584260 commit f08c0cd

File tree

1 file changed

+37
-64
lines changed

1 file changed

+37
-64
lines changed

crates/ide_db/src/helpers/import_assets.rs

Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -183,39 +183,41 @@ impl ImportAssets {
183183
}
184184

185185
fn applicable_defs<'a>(
186-
&self,
186+
&'a self,
187187
sema: &'a Semantics<RootDatabase>,
188188
prefixed: Option<hir::PrefixKind>,
189-
unfiltered_imports: Box<dyn Iterator<Item = Either<ModuleDef, MacroDef>> + 'a>,
189+
unfiltered_defs: impl Iterator<Item = Either<ModuleDef, MacroDef>> + 'a,
190190
) -> Box<dyn Iterator<Item = (ModPath, ItemInNs)> + 'a> {
191191
let current_crate = self.module_with_candidate.krate();
192192
let db = sema.db;
193193

194194
match &self.import_candidate {
195-
ImportCandidate::Path(path_candidate) => path_applicable_defs(
195+
ImportCandidate::Path(path_candidate) => Box::new(path_applicable_defs(
196196
sema,
197197
path_candidate,
198-
unfiltered_imports,
199-
self.module_with_candidate,
200-
prefixed,
201-
),
202-
ImportCandidate::TraitAssocItem(trait_candidate) => trait_applicable_defs(
203-
db,
204-
current_crate,
205-
trait_candidate,
206-
true,
207-
unfiltered_imports,
208-
self.module_with_candidate,
209-
prefixed,
198+
unfiltered_defs
199+
.into_iter()
200+
.map(|def| def.either(ItemInNs::from, ItemInNs::from))
201+
.filter_map(move |item_to_search| {
202+
get_mod_path(db, item_to_search, &self.module_with_candidate, prefixed)
203+
.zip(Some(item_to_search))
204+
}),
205+
)),
206+
ImportCandidate::TraitAssocItem(trait_candidate) => Box::new(
207+
trait_applicable_defs(db, current_crate, trait_candidate, true, unfiltered_defs)
208+
.into_iter()
209+
.filter_map(move |item_to_search| {
210+
get_mod_path(db, item_to_search, &self.module_with_candidate, prefixed)
211+
.zip(Some(item_to_search))
212+
}),
210213
),
211-
ImportCandidate::TraitMethod(trait_candidate) => trait_applicable_defs(
212-
db,
213-
current_crate,
214-
trait_candidate,
215-
false,
216-
unfiltered_imports,
217-
self.module_with_candidate,
218-
prefixed,
214+
ImportCandidate::TraitMethod(trait_candidate) => Box::new(
215+
trait_applicable_defs(db, current_crate, trait_candidate, false, unfiltered_defs)
216+
.into_iter()
217+
.filter_map(move |item_to_search| {
218+
get_mod_path(db, item_to_search, &self.module_with_candidate, prefixed)
219+
.zip(Some(item_to_search))
220+
}),
219221
),
220222
}
221223
}
@@ -224,22 +226,12 @@ impl ImportAssets {
224226
fn path_applicable_defs<'a>(
225227
sema: &'a Semantics<RootDatabase>,
226228
path_candidate: &PathImportCandidate,
227-
unfiltered_defs: Box<dyn Iterator<Item = Either<ModuleDef, MacroDef>> + 'a>,
228-
module_with_candidate: Module,
229-
prefixed: Option<hir::PrefixKind>,
230-
) -> Box<dyn Iterator<Item = (ModPath, ItemInNs)> + 'a> {
231-
let applicable_defs = unfiltered_defs
232-
.map(|candidate| candidate.either(ItemInNs::from, ItemInNs::from))
233-
.filter_map(move |item_to_search| {
234-
get_mod_path(sema.db, item_to_search, &module_with_candidate, prefixed)
235-
.zip(Some(item_to_search))
236-
});
237-
229+
unfiltered_defs: impl Iterator<Item = (ModPath, ItemInNs)> + 'a,
230+
) -> impl Iterator<Item = (ModPath, ItemInNs)> + 'a {
238231
let unresolved_qualifier = match &path_candidate.unresolved_qualifier {
239232
Some(qualifier) => qualifier,
240233
None => {
241-
// TODO kb too many boxes tossed around
242-
return Box::new(applicable_defs);
234+
return unfiltered_defs;
243235
}
244236
};
245237

@@ -252,7 +244,7 @@ fn path_applicable_defs<'a>(
252244
// first segment is already unresolved, need to turn it into ModuleDef somehow
253245
}
254246

255-
return Box::new(applicable_defs);
247+
return unfiltered_defs;
256248
}
257249

258250
fn resolve_qualifier_start(
@@ -269,10 +261,8 @@ fn trait_applicable_defs<'a>(
269261
current_crate: Crate,
270262
trait_candidate: &TraitImportCandidate,
271263
trait_assoc_item: bool,
272-
unfiltered_defs: Box<dyn Iterator<Item = Either<ModuleDef, MacroDef>> + 'a>,
273-
module_with_candidate: Module,
274-
prefixed: Option<hir::PrefixKind>,
275-
) -> Box<dyn Iterator<Item = (ModPath, ItemInNs)> + 'a> {
264+
unfiltered_defs: impl Iterator<Item = Either<ModuleDef, MacroDef>> + 'a,
265+
) -> FxHashSet<ItemInNs> {
276266
let mut required_assoc_items = FxHashSet::default();
277267

278268
let trait_candidates = unfiltered_defs
@@ -287,7 +277,7 @@ fn trait_applicable_defs<'a>(
287277
})
288278
.collect();
289279

290-
let mut applicable_defs = FxHashSet::default();
280+
let mut applicable_traits = FxHashSet::default();
291281

292282
if trait_assoc_item {
293283
trait_candidate.receiver_ty.iterate_path_candidates(
@@ -302,7 +292,8 @@ fn trait_applicable_defs<'a>(
302292
return None;
303293
}
304294
}
305-
applicable_defs.insert(assoc_to_module_def(assoc));
295+
applicable_traits
296+
.insert(ItemInNs::from(ModuleDef::from(assoc.containing_trait(db)?)));
306297
}
307298
None::<()>
308299
},
@@ -316,25 +307,15 @@ fn trait_applicable_defs<'a>(
316307
|_, function| {
317308
let assoc = function.as_assoc_item(db)?;
318309
if required_assoc_items.contains(&assoc) {
319-
applicable_defs.insert(assoc_to_module_def(assoc));
310+
applicable_traits
311+
.insert(ItemInNs::from(ModuleDef::from(assoc.containing_trait(db)?)));
320312
}
321313
None::<()>
322314
},
323315
)
324316
};
325317

326-
Box::new(
327-
applicable_defs
328-
.into_iter()
329-
.filter_map(move |candidate| {
330-
let canidate_trait = candidate.as_assoc_item(db)?.containing_trait(db)?;
331-
Some(ItemInNs::from(ModuleDef::from(canidate_trait)))
332-
})
333-
.filter_map(move |item_to_search| {
334-
get_mod_path(db, item_to_search, &module_with_candidate, prefixed)
335-
.zip(Some(item_to_search))
336-
}),
337-
)
318+
applicable_traits
338319
}
339320

340321
fn get_mod_path(
@@ -350,14 +331,6 @@ fn get_mod_path(
350331
}
351332
}
352333

353-
fn assoc_to_module_def(assoc: AssocItem) -> ModuleDef {
354-
match assoc {
355-
AssocItem::Function(f) => f.into(),
356-
AssocItem::Const(c) => c.into(),
357-
AssocItem::TypeAlias(t) => t.into(),
358-
}
359-
}
360-
361334
impl ImportCandidate {
362335
fn for_method_call(
363336
sema: &Semantics<RootDatabase>,

0 commit comments

Comments
 (0)