return Iterator instead of Vec for combined lifetime and argument parameters

This commit is contained in:
Jonas Marcello 2023-02-21 21:25:56 +01:00
parent 9942cc425b
commit 8bc75c4c28
2 changed files with 23 additions and 32 deletions

View File

@ -42,7 +42,7 @@
adt::VariantData,
body::{BodyDiagnostic, SyntheticSyntax},
expr::{BindingAnnotation, ExprOrPatId, LabelId, Pat, PatId},
generics::{TypeOrConstParamData, TypeParamProvenance, LifetimeParamData},
generics::{LifetimeParamData, TypeOrConstParamData, TypeParamProvenance},
item_tree::ItemTreeNode,
lang_item::{LangItem, LangItemTarget},
layout::{Layout, LayoutError, ReprOptions},
@ -1177,13 +1177,16 @@ pub fn lifetime(&self, db: &dyn HirDatabase) -> Option<LifetimeParamData> {
Adt::Union(u) => u.id.resolver(db.upcast()),
Adt::Enum(e) => e.id.resolver(db.upcast()),
};
resolver.generic_params().and_then(|gp| {
resolver
.generic_params()
.and_then(|gp| {
(&gp.lifetimes)
.iter()
// there should only be a single lifetime
// but `Arena` requires to use an iterator
.nth(0)
}).map(|arena| arena.1.clone())
})
.map(|arena| arena.1.clone())
}
pub fn as_enum(&self) -> Option<Enum> {
@ -3355,23 +3358,15 @@ pub fn type_arguments(&self) -> impl Iterator<Item = Type> + '_ {
.map(move |ty| self.derived(ty))
}
/// Combines lifetime indicators and type arguments into a single `Vec<SmolStr>`
pub fn lifetime_and_type_arguments<'a>(&'a self, db: &'a dyn HirDatabase) -> Vec<SmolStr> {
let mut names = if let Some(lt) = self
.as_adt()
.and_then(|a| {
a.lifetime(db)
.and_then(|lt| Some((&lt.name).to_smol_str().clone()))
}) {
vec![lt]
} else {
vec![]
};
for ty in self.type_arguments() {
names.push(SmolStr::new(ty.display(db).to_string()))
}
names
/// Combines lifetime indicators and type arguments into a single `Iterator`
pub fn lifetime_and_type_arguments<'a>(
&'a self,
db: &'a dyn HirDatabase,
) -> impl Iterator<Item = SmolStr> + 'a {
self.as_adt()
.and_then(|a| a.lifetime(db).and_then(|lt| Some((&lt.name).to_smol_str())))
.into_iter()
.chain(self.type_arguments().map(|ty| SmolStr::new(ty.display(db).to_string())))
}
pub fn iterate_method_candidates_with_traits<T>(

View File

@ -370,7 +370,7 @@ pub(crate) fn runnable_impl(
let nav = def.try_to_nav(sema.db)?;
let ty = def.self_ty(sema.db);
let adt_name = ty.as_adt()?.name(sema.db);
let mut ty_args = ty.lifetime_and_type_arguments(sema.db).into_iter().peekable();
let mut ty_args = ty.lifetime_and_type_arguments(sema.db).peekable();
let params = if ty_args.peek().is_some() {
format!("<{}>", ty_args.format_with(",", |ty, cb| cb(&ty)))
} else {
@ -436,14 +436,10 @@ fn module_def_doctest(db: &RootDatabase, def: Definition) -> Option<Runnable> {
let ty = imp.self_ty(db);
if let Some(adt) = ty.as_adt() {
let name = adt.name(db);
let mut ty_args = ty.lifetime_and_type_arguments(db).into_iter().peekable();
let mut ty_args = ty.lifetime_and_type_arguments(db).peekable();
format_to!(path, "{}", name);
if ty_args.peek().is_some() {
format_to!(
path,
"<{}>",
ty_args.format_with(",", |ty, cb| cb(&ty))
);
format_to!(path, "<{}>", ty_args.format_with(",", |ty, cb| cb(&ty)));
}
format_to!(path, "::{}", def_name);
path.retain(|c| c != ' ');