Skip to content

Commit 356d12e

Browse files
committed
refactor: leverage HasAttrs for code brevity
1 parent 2e7d2c2 commit 356d12e

File tree

3 files changed

+47
-36
lines changed

3 files changed

+47
-36
lines changed

crates/hir-def/src/attr.rs

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -315,26 +315,14 @@ impl AttrsWithOwner {
315315
let src = it.parent().child_source(db);
316316
RawAttrs::from_attrs_owner(
317317
db.upcast(),
318-
src.with_value(src.value[it.local_id()].as_ref().either(
319-
|it| match it {
320-
ast::TypeOrConstParam::Type(it) => it as _,
321-
ast::TypeOrConstParam::Const(it) => it as _,
322-
},
323-
|it| it as _,
324-
)),
318+
src.with_value(&src.value[it.local_id()]),
325319
)
326320
}
327321
GenericParamId::TypeParamId(it) => {
328322
let src = it.parent().child_source(db);
329323
RawAttrs::from_attrs_owner(
330324
db.upcast(),
331-
src.with_value(src.value[it.local_id()].as_ref().either(
332-
|it| match it {
333-
ast::TypeOrConstParam::Type(it) => it as _,
334-
ast::TypeOrConstParam::Const(it) => it as _,
335-
},
336-
|it| it as _,
337-
)),
325+
src.with_value(&src.value[it.local_id()]),
338326
)
339327
}
340328
GenericParamId::LifetimeParamId(it) => {
@@ -412,28 +400,14 @@ impl AttrsWithOwner {
412400
},
413401
AttrDefId::ImplId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
414402
AttrDefId::GenericParamId(id) => match id {
415-
GenericParamId::ConstParamId(id) => {
416-
id.parent().child_source(db).map(|source| match &source[id.local_id()] {
417-
Either::Left(ast::TypeOrConstParam::Type(id)) => {
418-
ast::AnyHasAttrs::new(id.clone())
419-
}
420-
Either::Left(ast::TypeOrConstParam::Const(id)) => {
421-
ast::AnyHasAttrs::new(id.clone())
422-
}
423-
Either::Right(id) => ast::AnyHasAttrs::new(id.clone()),
424-
})
425-
}
426-
GenericParamId::TypeParamId(id) => {
427-
id.parent().child_source(db).map(|source| match &source[id.local_id()] {
428-
Either::Left(ast::TypeOrConstParam::Type(id)) => {
429-
ast::AnyHasAttrs::new(id.clone())
430-
}
431-
Either::Left(ast::TypeOrConstParam::Const(id)) => {
432-
ast::AnyHasAttrs::new(id.clone())
433-
}
434-
Either::Right(id) => ast::AnyHasAttrs::new(id.clone()),
435-
})
436-
}
403+
GenericParamId::ConstParamId(id) => id
404+
.parent()
405+
.child_source(db)
406+
.map(|source| ast::AnyHasAttrs::new(source[id.local_id()].clone())),
407+
GenericParamId::TypeParamId(id) => id
408+
.parent()
409+
.child_source(db)
410+
.map(|source| ast::AnyHasAttrs::new(source[id.local_id()].clone())),
437411
GenericParamId::LifetimeParamId(id) => id
438412
.parent
439413
.child_source(db)

crates/syntax/src/ast.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ where
129129
}
130130
}
131131

132+
impl<L, R> HasAttrs for Either<L, R>
133+
where
134+
L: HasAttrs,
135+
R: HasAttrs,
136+
{
137+
}
138+
132139
mod support {
133140
use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken};
134141

crates/syntax/src/ast/node_ext.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,36 @@ impl TypeOrConstParam {
680680
}
681681
}
682682

683+
impl AstNode for TypeOrConstParam {
684+
fn can_cast(kind: SyntaxKind) -> bool
685+
where
686+
Self: Sized,
687+
{
688+
matches!(kind, SyntaxKind::TYPE_PARAM | SyntaxKind::CONST_PARAM)
689+
}
690+
691+
fn cast(syntax: SyntaxNode) -> Option<Self>
692+
where
693+
Self: Sized,
694+
{
695+
let res = match syntax.kind() {
696+
SyntaxKind::TYPE_PARAM => TypeOrConstParam::Type(ast::TypeParam { syntax }),
697+
SyntaxKind::CONST_PARAM => TypeOrConstParam::Const(ast::ConstParam { syntax }),
698+
_ => return None,
699+
};
700+
Some(res)
701+
}
702+
703+
fn syntax(&self) -> &SyntaxNode {
704+
match self {
705+
TypeOrConstParam::Type(it) => it.syntax(),
706+
TypeOrConstParam::Const(it) => it.syntax(),
707+
}
708+
}
709+
}
710+
711+
impl HasAttrs for TypeOrConstParam {}
712+
683713
#[derive(Debug, Clone)]
684714
pub enum TraitOrAlias {
685715
Trait(ast::Trait),

0 commit comments

Comments
 (0)