Skip to content

Commit c77f03c

Browse files
authored
refactor: Avoid node clones on casting (#128)
1 parent 9396e9b commit c77f03c

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

  • crates/plotnik-lib/src/parser

crates/plotnik-lib/src/parser/ast.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ macro_rules! ast_node {
3030

3131
impl $name {
3232
pub fn cast(node: SyntaxNode) -> Option<Self> {
33-
(node.kind() == SyntaxKind::$kind).then(|| Self(node))
33+
Self::can_cast(node.kind()).then(|| Self(node))
34+
}
35+
36+
pub fn can_cast(kind: SyntaxKind) -> bool {
37+
kind == SyntaxKind::$kind
3438
}
3539

3640
pub fn as_cst(&self) -> &SyntaxNode {
@@ -54,7 +58,8 @@ macro_rules! define_expr {
5458

5559
impl Expr {
5660
pub fn cast(node: SyntaxNode) -> Option<Self> {
57-
$(if let Some(n) = $variant::cast(node.clone()) { return Some(Expr::$variant(n)); })+
61+
let kind = node.kind();
62+
$(if $variant::can_cast(kind) { return Some(Expr::$variant($variant(node))); })+
5863
None
5964
}
6065

@@ -138,7 +143,11 @@ pub struct AnonymousNode(SyntaxNode);
138143

139144
impl AnonymousNode {
140145
pub fn cast(node: SyntaxNode) -> Option<Self> {
141-
matches!(node.kind(), SyntaxKind::Str | SyntaxKind::Wildcard).then(|| Self(node))
146+
Self::can_cast(node.kind()).then(|| Self(node))
147+
}
148+
149+
pub fn can_cast(kind: SyntaxKind) -> bool {
150+
matches!(kind, SyntaxKind::Str | SyntaxKind::Wildcard)
142151
}
143152

144153
pub fn as_cst(&self) -> &SyntaxNode {

0 commit comments

Comments
 (0)