ast: factor out as_node

Shrink another macro.
This commit is contained in:
Peter Ammon
2025-04-20 20:31:17 -07:00
parent 27dc4b3c8a
commit 31edcf029b

View File

@@ -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<T: Node + Sized> 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
}
}
};
}