From 31edcf029ba1e6f3ba240d8eea1688c59668e8fc Mon Sep 17 00:00:00 2001 From: Peter Ammon Date: Sun, 20 Apr 2025 20:31:17 -0700 Subject: [PATCH] ast: factor out as_node Shrink another macro. --- src/ast.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index a4e6cdd3d..45146b1fe 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -97,7 +97,7 @@ fn accept_mut(&mut self, visitor: &mut dyn NodeVisitorMut, reversed: bool) { } /// Node is the base trait of all AST nodes. -pub trait Node: Acceptor + ConcreteNode + std::fmt::Debug { +pub trait Node: Acceptor + ConcreteNode + AsNode + std::fmt::Debug { /// The type of this node. fn typ(&self) -> Type; @@ -154,11 +154,19 @@ fn source<'s>(&self, orig: &'s wstr) -> &'s wstr { fn self_memory_size(&self) -> usize { std::mem::size_of_val(self) } +} - /// Convert to the dynamic Node type. +// Convert to the dynamic Node type. +pub trait AsNode { fn as_node(&self) -> &dyn Node; } +impl AsNode for T { + fn as_node(&self) -> &dyn Node { + self + } +} + /// Return true if two nodes are the same object. #[inline(always)] pub fn is_same_node(lhs: &dyn Node, rhs: &dyn Node) -> bool { @@ -519,9 +527,6 @@ impl Node for $name { fn typ(&self) -> Type { Type::$type } - fn as_node(&self) -> &dyn Node { - self - } } }; }