Extracted common macros to separate file

This commit is contained in:
Andrew Golovashevich 2025-12-28 17:24:17 +03:00
parent 8aceddd48e
commit cfa721f501
6 changed files with 86 additions and 143 deletions

45
src/_common_macros.rs Normal file
View File

@ -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<Ctx: NodeRefContainer> NodeRefContainer for $name<& $($mut)? Ctx> {
type NodeRef = Ctx::NodeRef;
}
};
}
macro_rules! _delegate_parent_getter {
($name:ident $($mut:tt)?) => {
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext
for $name<& $($mut)? Ctx>
{
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
};
}

View File

@ -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<CtxRef> {
ctx: CtxRef,
@ -37,34 +42,17 @@ impl<CtxRef> DynamicDirectedBinaryTreeContextFromContext<CtxRef> {
}
}
impl<Ctx: NodeRefContainer> NodeRefContainer for DynamicDirectedBinaryTreeContextFromContext<&Ctx> {
type NodeRef = Ctx::NodeRef;
}
_mut_switch!(_delegate_node_ref!(
DynamicDirectedBinaryTreeContextFromContext
));
impl<Ctx: NodeRefContainer> NodeRefContainer
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
type NodeRef = Ctx::NodeRef;
}
_mut_switch!(_delegate_parent_getter!(
DynamicDirectedBinaryTreeContextFromContext
));
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext
for DynamicDirectedBinaryTreeContextFromContext<&Ctx>
{
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
return self.ctx.getParent(node);
}
}
impl<Ctx: BinaryTreeChildrenGetterContext> DirectedBinaryTreeChildrenGetterContext
for DynamicDirectedBinaryTreeContextFromContext<&Ctx>
for DynamicDirectedBinaryTreeContextFromContext<&Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
match self.dir {
@ -82,7 +70,7 @@ for DynamicDirectedBinaryTreeContextFromContext<&Ctx>
}
impl<Ctx: BinaryTreeChildrenGetterContext> DirectedBinaryTreeChildrenGetterContext
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
fn getForwardChild(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
match self.dir {
@ -100,7 +88,7 @@ for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
}
unsafe impl<Ctx: BinaryTreeChildrenSetterContext> DirectedBinaryTreeChildrenSetterContext
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
match self.dir {
@ -146,7 +134,7 @@ for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
}
unsafe impl<Ctx: BinaryTreeParentSetterContext> BinaryTreeParentSetterContext
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option<Self::NodeRef>) {
self.ctx.xSetParent(node, newParent);
@ -162,7 +150,7 @@ for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
}
unsafe impl<Ctx: BinaryTreeRootRelationSetterContext> BinaryTreeRootRelationSetterContext
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
fn xSetRootRelation(&mut self, newRoot: Option<Self::NodeRef>) {
self.ctx.xSetRootRelation(newRoot)
@ -178,7 +166,7 @@ for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
}
unsafe impl<Ctx: BinaryTreeRelationSetterContext> DirectedBinaryTreeRelationSetterContext
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
for DynamicDirectedBinaryTreeContextFromContext<&mut Ctx>
{
fn xSetForwardRelation(&mut self, node: Self::NodeRef, newChild: Option<Self::NodeRef>) {
match self.dir {

View File

@ -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<CtxRef> {
ctx: CtxRef,
@ -26,52 +21,14 @@ pub struct FixedRightDirectedBinaryTreeContextFromContext<CtxRef> {
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<Ctx: NodeRefContainer> 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<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext
for $name<& $($mut)? Ctx>
{
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
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)? ) => {

View File

@ -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<NodeRef> {
nothing: (),

View File

@ -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) => {

View File

@ -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<CtxRef> {
ctx: CtxRef,
@ -21,62 +21,25 @@ pub struct DirectedBinaryTreeRelationContextFromBaseContextConverter<CtxRef> {
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<Ctx: NodeRefContainer> 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<Ctx: BinaryTreeParentGetterContext> BinaryTreeParentGetterContext
for $name<& $($mut)? Ctx>
{
fn getParent(&self, node: Self::NodeRef) -> Option<Self::NodeRef> {
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
));