Unnamed repository; edit this file 'description' to name the repository.
return Iterator instead of Vec for combined lifetime and argument parameters
Jonas Marcello 2023-02-28
parent 9942cc4 · commit 8bc75c4
-rw-r--r--crates/hir/src/lib.rs45
-rw-r--r--crates/ide/src/runnables.rs10
2 files changed, 23 insertions, 32 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 28d87e14e1..883838293e 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -42,7 +42,7 @@ use hir_def::{
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 @@ impl Adt {
Adt::Union(u) => u.id.resolver(db.upcast()),
Adt::Enum(e) => e.id.resolver(db.upcast()),
};
- 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())
+ 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())
}
pub fn as_enum(&self) -> Option<Enum> {
@@ -3355,23 +3358,15 @@ impl 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>(
diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs
index 2e8f3906af..b0477e9678 100644
--- a/crates/ide/src/runnables.rs
+++ b/crates/ide/src/runnables.rs
@@ -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 != ' ');