diff options
| author | Ryo Yoshida <low.ryoshida@gmail.com> | 2023-03-04 00:24:03 +0900 |
|---|---|---|
| committer | Ryo Yoshida <low.ryoshida@gmail.com> | 2023-03-04 00:24:03 +0900 |
| commit | 356d12eae4b8985dc933c13e9281546d532d0845 (patch) | |
| tree | 6ba6b0b19071f7870167893b2519061e6105d602 /crates/syntax/src | |
| parent | 2e7d2c2d045e2a66a4e309cbb71a3264004f8678 (diff) | |
| download | rust-356d12eae4b8985dc933c13e9281546d532d0845.tar.gz rust-356d12eae4b8985dc933c13e9281546d532d0845.zip | |
refactor: leverage `HasAttrs` for code brevity
Diffstat (limited to 'crates/syntax/src')
| -rw-r--r-- | crates/syntax/src/ast.rs | 7 | ||||
| -rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 30 |
2 files changed, 37 insertions, 0 deletions
diff --git a/crates/syntax/src/ast.rs b/crates/syntax/src/ast.rs index 745f2e14e95..1e691befff6 100644 --- a/crates/syntax/src/ast.rs +++ b/crates/syntax/src/ast.rs @@ -129,6 +129,13 @@ where } } +impl<L, R> HasAttrs for Either<L, R> +where + L: HasAttrs, + R: HasAttrs, +{ +} + mod support { use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken}; diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 301fbcebf1c..15bd5ab3c72 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -680,6 +680,36 @@ impl TypeOrConstParam { } } +impl AstNode for TypeOrConstParam { + fn can_cast(kind: SyntaxKind) -> bool + where + Self: Sized, + { + matches!(kind, SyntaxKind::TYPE_PARAM | SyntaxKind::CONST_PARAM) + } + + fn cast(syntax: SyntaxNode) -> Option<Self> + where + Self: Sized, + { + let res = match syntax.kind() { + SyntaxKind::TYPE_PARAM => TypeOrConstParam::Type(ast::TypeParam { syntax }), + SyntaxKind::CONST_PARAM => TypeOrConstParam::Const(ast::ConstParam { syntax }), + _ => return None, + }; + Some(res) + } + + fn syntax(&self) -> &SyntaxNode { + match self { + TypeOrConstParam::Type(it) => it.syntax(), + TypeOrConstParam::Const(it) => it.syntax(), + } + } +} + +impl HasAttrs for TypeOrConstParam {} + #[derive(Debug, Clone)] pub enum TraitOrAlias { Trait(ast::Trait), |
