Skip to content

Commit 8bc75c4

Browse files
committed
return Iterator instead of Vec for combined lifetime and argument parameters
1 parent 9942cc4 commit 8bc75c4

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

crates/hir/src/lib.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use hir_def::{
4242
adt::VariantData,
4343
body::{BodyDiagnostic, SyntheticSyntax},
4444
expr::{BindingAnnotation, ExprOrPatId, LabelId, Pat, PatId},
45-
generics::{TypeOrConstParamData, TypeParamProvenance, LifetimeParamData},
45+
generics::{LifetimeParamData, TypeOrConstParamData, TypeParamProvenance},
4646
item_tree::ItemTreeNode,
4747
lang_item::{LangItem, LangItemTarget},
4848
layout::{Layout, LayoutError, ReprOptions},
@@ -1177,13 +1177,16 @@ impl Adt {
11771177
Adt::Union(u) => u.id.resolver(db.upcast()),
11781178
Adt::Enum(e) => e.id.resolver(db.upcast()),
11791179
};
1180-
resolver.generic_params().and_then(|gp| {
1181-
(&gp.lifetimes)
1182-
.iter()
1183-
// there should only be a single lifetime
1184-
// but `Arena` requires to use an iterator
1185-
.nth(0)
1186-
}).map(|arena| arena.1.clone())
1180+
resolver
1181+
.generic_params()
1182+
.and_then(|gp| {
1183+
(&gp.lifetimes)
1184+
.iter()
1185+
// there should only be a single lifetime
1186+
// but `Arena` requires to use an iterator
1187+
.nth(0)
1188+
})
1189+
.map(|arena| arena.1.clone())
11871190
}
11881191

11891192
pub fn as_enum(&self) -> Option<Enum> {
@@ -3355,23 +3358,15 @@ impl Type {
33553358
.map(move |ty| self.derived(ty))
33563359
}
33573360

3358-
/// Combines lifetime indicators and type arguments into a single `Vec<SmolStr>`
3359-
pub fn lifetime_and_type_arguments<'a>(&'a self, db: &'a dyn HirDatabase) -> Vec<SmolStr> {
3360-
let mut names = if let Some(lt) = self
3361-
.as_adt()
3362-
.and_then(|a| {
3363-
a.lifetime(db)
3364-
.and_then(|lt| Some((&lt.name).to_smol_str().clone()))
3365-
}) {
3366-
vec![lt]
3367-
} else {
3368-
vec![]
3369-
};
3370-
3371-
for ty in self.type_arguments() {
3372-
names.push(SmolStr::new(ty.display(db).to_string()))
3373-
}
3374-
names
3361+
/// Combines lifetime indicators and type arguments into a single `Iterator`
3362+
pub fn lifetime_and_type_arguments<'a>(
3363+
&'a self,
3364+
db: &'a dyn HirDatabase,
3365+
) -> impl Iterator<Item = SmolStr> + 'a {
3366+
self.as_adt()
3367+
.and_then(|a| a.lifetime(db).and_then(|lt| Some((&lt.name).to_smol_str())))
3368+
.into_iter()
3369+
.chain(self.type_arguments().map(|ty| SmolStr::new(ty.display(db).to_string())))
33753370
}
33763371

33773372
pub fn iterate_method_candidates_with_traits<T>(

crates/ide/src/runnables.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub(crate) fn runnable_impl(
370370
let nav = def.try_to_nav(sema.db)?;
371371
let ty = def.self_ty(sema.db);
372372
let adt_name = ty.as_adt()?.name(sema.db);
373-
let mut ty_args = ty.lifetime_and_type_arguments(sema.db).into_iter().peekable();
373+
let mut ty_args = ty.lifetime_and_type_arguments(sema.db).peekable();
374374
let params = if ty_args.peek().is_some() {
375375
format!("<{}>", ty_args.format_with(",", |ty, cb| cb(&ty)))
376376
} else {
@@ -436,14 +436,10 @@ fn module_def_doctest(db: &RootDatabase, def: Definition) -> Option<Runnable> {
436436
let ty = imp.self_ty(db);
437437
if let Some(adt) = ty.as_adt() {
438438
let name = adt.name(db);
439-
let mut ty_args = ty.lifetime_and_type_arguments(db).into_iter().peekable();
439+
let mut ty_args = ty.lifetime_and_type_arguments(db).peekable();
440440
format_to!(path, "{}", name);
441441
if ty_args.peek().is_some() {
442-
format_to!(
443-
path,
444-
"<{}>",
445-
ty_args.format_with(",", |ty, cb| cb(&ty))
446-
);
442+
format_to!(path, "<{}>", ty_args.format_with(",", |ty, cb| cb(&ty)));
447443
}
448444
format_to!(path, "::{}", def_name);
449445
path.retain(|c| c != ' ');

0 commit comments

Comments
 (0)