From cfa721f501f2203c658b2fa1f00861e998b2dad9 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Sun, 28 Dec 2025 17:24:17 +0300 Subject: [PATCH] Extracted common macros to separate file --- src/_common_macros.rs | 45 ++++++++++++++ src/directed_context/dynamic_wrapper.rs | 48 ++++++--------- src/directed_context/fixed_wrapper.rs | 57 +++--------------- src/parent2node_clojure/dynamic_wrappers.rs | 7 +-- src/parent2node_clojure/fixed_wrapper.rs | 7 +-- src/relation_context/converter.rs | 65 +++++---------------- 6 files changed, 86 insertions(+), 143 deletions(-) create mode 100644 src/_common_macros.rs diff --git a/src/_common_macros.rs b/src/_common_macros.rs new file mode 100644 index 0000000..80fac2b --- /dev/null +++ b/src/_common_macros.rs @@ -0,0 +1,45 @@ +macro_rules! _mut_switch { + ($macro:tt $excl_mark:tt ( $($args:tt)+) ) => { + $macro$excl_mark($($args)+); + $macro$excl_mark($($args)+ mut); + }; +} + + +macro_rules! _ctx_wrapper_constructor { + ($name:ident) => { + impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx Ctx> { + pub fn wrap(ctx: &'ctx Ctx) -> Self { + return Self { ctx }; + } + } + }; + + ($name:ident mut) => { + impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx mut Ctx> { + pub fn wrap_mut(ctx: &'ctx mut Ctx) -> Self { + return Self { ctx }; + } + } + }; +} + +macro_rules! _delegate_node_ref { + ($name:ident $($mut:tt)?) => { + impl NodeRefContainer for $name<& $($mut)? Ctx> { + type NodeRef = Ctx::NodeRef; + } + }; +} + +macro_rules! _delegate_parent_getter { + ($name:ident $($mut:tt)?) => { + impl BinaryTreeParentGetterContext + for $name<& $($mut)? Ctx> + { + fn getParent(&self, node: Self::NodeRef) -> Option { + return self.ctx.getParent(node); + } + } + }; +} diff --git a/src/directed_context/dynamic_wrapper.rs b/src/directed_context/dynamic_wrapper.rs index b04d2eb..8f69fb0 100644 --- a/src/directed_context/dynamic_wrapper.rs +++ b/src/directed_context/dynamic_wrapper.rs @@ -6,7 +6,12 @@ use crate::base_context::{ use crate::directed_context::context::{ DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext, }; -use crate::relation_context::{BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext, DirectedBinaryTreeRelationSetterContext}; +use crate::relation_context::{ + BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext, + DirectedBinaryTreeRelationSetterContext, +}; + +include!("../_common_macros.rs"); pub struct DynamicDirectedBinaryTreeContextFromContext { ctx: CtxRef, @@ -37,34 +42,17 @@ impl DynamicDirectedBinaryTreeContextFromContext { } } -impl NodeRefContainer for DynamicDirectedBinaryTreeContextFromContext<&Ctx> { - type NodeRef = Ctx::NodeRef; -} +_mut_switch!(_delegate_node_ref!( + DynamicDirectedBinaryTreeContextFromContext +)); -impl NodeRefContainer -for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> -{ - type NodeRef = Ctx::NodeRef; -} +_mut_switch!(_delegate_parent_getter!( + DynamicDirectedBinaryTreeContextFromContext +)); -impl BinaryTreeParentGetterContext -for DynamicDirectedBinaryTreeContextFromContext<&Ctx> -{ - fn getParent(&self, node: Self::NodeRef) -> Option { - return self.ctx.getParent(node); - } -} - -impl BinaryTreeParentGetterContext -for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> -{ - fn getParent(&self, node: Self::NodeRef) -> Option { - return self.ctx.getParent(node); - } -} impl DirectedBinaryTreeChildrenGetterContext -for DynamicDirectedBinaryTreeContextFromContext<&Ctx> + for DynamicDirectedBinaryTreeContextFromContext<&Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { match self.dir { @@ -82,7 +70,7 @@ for DynamicDirectedBinaryTreeContextFromContext<&Ctx> } impl DirectedBinaryTreeChildrenGetterContext -for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> + for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn getForwardChild(&self, node: Self::NodeRef) -> Option { match self.dir { @@ -100,7 +88,7 @@ for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> } unsafe impl DirectedBinaryTreeChildrenSetterContext -for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> + for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option) { match self.dir { @@ -146,7 +134,7 @@ for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> } unsafe impl BinaryTreeParentSetterContext -for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> + for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option) { self.ctx.xSetParent(node, newParent); @@ -162,7 +150,7 @@ for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> } unsafe impl BinaryTreeRootRelationSetterContext -for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> + for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn xSetRootRelation(&mut self, newRoot: Option) { self.ctx.xSetRootRelation(newRoot) @@ -178,7 +166,7 @@ for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> } unsafe impl DirectedBinaryTreeRelationSetterContext -for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> + for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx> { fn xSetForwardRelation(&mut self, node: Self::NodeRef, newChild: Option) { match self.dir { diff --git a/src/directed_context/fixed_wrapper.rs b/src/directed_context/fixed_wrapper.rs index 28a2dd3..2e53710 100644 --- a/src/directed_context/fixed_wrapper.rs +++ b/src/directed_context/fixed_wrapper.rs @@ -11,12 +11,7 @@ use crate::relation_context::{ DirectedBinaryTreeRelationSetterContext, }; -macro_rules! _mut_switch { - ($macro:tt $excl_mark:tt ( $($args:tt)+) ) => { - $macro$excl_mark($($args)+); - $macro$excl_mark($($args)+ mut); - }; -} +include!("../_common_macros.rs"); pub struct FixedLeftDirectedBinaryTreeContextFromContext { ctx: CtxRef, @@ -26,52 +21,14 @@ pub struct FixedRightDirectedBinaryTreeContextFromContext { ctx: CtxRef, } -macro_rules! _constructor { - ($name:ident) => { - impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx Ctx> { - pub fn wrap(ctx: &'ctx Ctx) -> Self { - return Self { ctx }; - } - } - }; +_mut_switch!(_ctx_wrapper_constructor!(FixedLeftDirectedBinaryTreeContextFromContext)); +_mut_switch!(_ctx_wrapper_constructor!(FixedRightDirectedBinaryTreeContextFromContext)); - ($name:ident mut) => { - impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx mut Ctx> { - pub fn wrap_mut(ctx: &'ctx mut Ctx) -> Self { - return Self { ctx }; - } - } - }; -} +_mut_switch!(_delegate_node_ref!(FixedLeftDirectedBinaryTreeContextFromContext)); +_mut_switch!(_delegate_node_ref!(FixedRightDirectedBinaryTreeContextFromContext)); -_mut_switch!(_constructor!(FixedLeftDirectedBinaryTreeContextFromContext)); -_mut_switch!(_constructor!(FixedRightDirectedBinaryTreeContextFromContext)); - -macro_rules! _node_ref { - ($name:ident $($mut:tt)?) => { - impl NodeRefContainer for $name<& $($mut)? Ctx> { - type NodeRef = Ctx::NodeRef; - } - }; -} - -_mut_switch!(_node_ref!(FixedLeftDirectedBinaryTreeContextFromContext)); -_mut_switch!(_node_ref!(FixedRightDirectedBinaryTreeContextFromContext)); - -macro_rules! _parent_get { - ($name:ident $($mut:tt)?) => { - impl BinaryTreeParentGetterContext - for $name<& $($mut)? Ctx> - { - fn getParent(&self, node: Self::NodeRef) -> Option { - return self.ctx.getParent(node); - } - } - }; -} - -_mut_switch!(_parent_get!(FixedLeftDirectedBinaryTreeContextFromContext)); -_mut_switch!(_parent_get!(FixedRightDirectedBinaryTreeContextFromContext)); +_mut_switch!(_delegate_parent_getter!(FixedLeftDirectedBinaryTreeContextFromContext)); +_mut_switch!(_delegate_parent_getter!(FixedRightDirectedBinaryTreeContextFromContext)); macro_rules! _children_get { ($name:ident $fwd:ident $op:ident $($mut:tt)? ) => { diff --git a/src/parent2node_clojure/dynamic_wrappers.rs b/src/parent2node_clojure/dynamic_wrappers.rs index fc48a07..4bca9d4 100644 --- a/src/parent2node_clojure/dynamic_wrappers.rs +++ b/src/parent2node_clojure/dynamic_wrappers.rs @@ -16,12 +16,7 @@ use crate::relation_context::{ use std::mem::ManuallyDrop; use std::ops::Deref; -macro_rules! _mut_switch { - ($macro:tt $excl_mark:tt ( $($args:tt)+) ) => { - $macro$excl_mark($($args)+); - $macro$excl_mark($($args)+ mut); - }; -} +include!("../_common_macros.rs"); union _NodeOrNothing { nothing: (), diff --git a/src/parent2node_clojure/fixed_wrapper.rs b/src/parent2node_clojure/fixed_wrapper.rs index 80129c2..d6c7850 100644 --- a/src/parent2node_clojure/fixed_wrapper.rs +++ b/src/parent2node_clojure/fixed_wrapper.rs @@ -11,12 +11,7 @@ use crate::parent2node_clojure::clojure::{ }; use crate::relation_context::{BinaryTreeRootRelationSetterContext, Parent2NodeRelationSetterClojure}; -macro_rules! _mut_switch { - ($macro:tt $excl_mark:tt ( $($args:tt)+) ) => { - $macro$excl_mark($($args)+); - $macro$excl_mark($($args)+ mut); - }; -} +include!("../_common_macros.rs"); macro_rules! _struct { ($name:ident) => { diff --git a/src/relation_context/converter.rs b/src/relation_context/converter.rs index 88b9086..21c9720 100644 --- a/src/relation_context/converter.rs +++ b/src/relation_context/converter.rs @@ -1,17 +1,17 @@ use crate::NodeRefContainer; -use crate::base_context::{BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, BinaryTreeParentGetterContext, BinaryTreeParentSetterContext, BinaryTreeRootSetter}; -use crate::directed_context::{DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext}; +use crate::base_context::{ + BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, + BinaryTreeParentGetterContext, BinaryTreeParentSetterContext, BinaryTreeRootSetter, +}; +use crate::directed_context::{ + DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext, +}; use crate::relation_context::{ BinaryTreeRelationSetterContext, BinaryTreeRootRelationSetterContext, DirectedBinaryTreeRelationSetterContext, }; -macro_rules! _mut_switch { - ($macro:tt $excl_mark:tt ( $($args:tt)+) ) => { - $macro$excl_mark($($args)+); - $macro$excl_mark($($args)+ mut); - }; -} +include!("../_common_macros.rs"); pub struct BinaryTreeRelationContextFromBaseContextConverter { ctx: CtxRef, @@ -21,62 +21,25 @@ pub struct DirectedBinaryTreeRelationContextFromBaseContextConverter { ctx: CtxRef, } -macro_rules! _constructor { - ($name:ident) => { - impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx Ctx> { - pub fn wrap(ctx: &'ctx Ctx) -> Self { - return Self { ctx }; - } - } - }; - - ($name:ident mut) => { - impl<'ctx, Ctx: NodeRefContainer> $name<&'ctx mut Ctx> { - pub fn wrap_mut(ctx: &'ctx mut Ctx) -> Self { - return Self { ctx }; - } - } - }; -} - -_mut_switch!(_constructor!( +_mut_switch!(_ctx_wrapper_constructor!( BinaryTreeRelationContextFromBaseContextConverter )); -_mut_switch!(_constructor!( +_mut_switch!(_ctx_wrapper_constructor!( DirectedBinaryTreeRelationContextFromBaseContextConverter )); -macro_rules! _node_ref { - ($name:ident $($mut:tt)?) => { - impl NodeRefContainer for $name<& $($mut)? Ctx> { - type NodeRef = Ctx::NodeRef; - } - }; -} - -_mut_switch!(_node_ref!( +_mut_switch!(_delegate_node_ref!( BinaryTreeRelationContextFromBaseContextConverter )); -_mut_switch!(_node_ref!( +_mut_switch!(_delegate_node_ref!( DirectedBinaryTreeRelationContextFromBaseContextConverter )); -macro_rules! _parent_get { - ($name:ident $($mut:tt)?) => { - impl BinaryTreeParentGetterContext - for $name<& $($mut)? Ctx> - { - fn getParent(&self, node: Self::NodeRef) -> Option { - return self.ctx.getParent(node); - } - } - }; -} -_mut_switch!(_parent_get!( +_mut_switch!(_delegate_parent_getter!( BinaryTreeRelationContextFromBaseContextConverter )); -_mut_switch!(_parent_get!( +_mut_switch!(_delegate_parent_getter!( DirectedBinaryTreeRelationContextFromBaseContextConverter ));