From 6ce69a3ad51903ebc7e69d9c95c8869e8459dd8e Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Fri, 26 Dec 2025 20:05:33 +0300 Subject: [PATCH] Reducing fixed context wrappers with macros --- src/directed/impls/fixed_wrapper.rs | 316 +++++++++++---------------- src/parent2node/fixed_wrapper.rs | 323 +++++++--------------------- 2 files changed, 204 insertions(+), 435 deletions(-) diff --git a/src/directed/impls/fixed_wrapper.rs b/src/directed/impls/fixed_wrapper.rs index 8ede3e3..f0e19bc 100644 --- a/src/directed/impls/fixed_wrapper.rs +++ b/src/directed/impls/fixed_wrapper.rs @@ -1,8 +1,18 @@ use crate::NodeRefContainer; -use crate::context::{BinaryTreeChildrenSetterContext, BinaryTreeChildrenGetterContext, BinaryTreeParentSetterContext, BinaryTreeParentGetterContext}; -use crate::directed::context::{ - DirectedBinaryTreeChildrenSetterContext, DirectedBinaryTreeChildrenGetterContext, +use crate::context::{ + BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, + BinaryTreeParentGetterContext, BinaryTreeParentSetterContext, }; +use crate::directed::context::{ + DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext, +}; + +macro_rules! _mut_switch { + ($macro:tt $excl_mark:tt ( $($args:tt)+) ) => { + $macro$excl_mark($($args)+); + $macro$excl_mark($($args)+ mut); + }; +} pub struct LeftDirectedBinaryTreeWalker { ctx: CtxRef, @@ -12,200 +22,128 @@ pub struct RightDirectedBinaryTreeWalker { ctx: CtxRef, } -impl<'ctx, Ctx: NodeRefContainer> LeftDirectedBinaryTreeWalker<&'ctx Ctx> { - pub fn wrap(ctx: &'ctx Ctx) -> Self { - return Self { ctx }; - } -} -impl<'ctx, Ctx: NodeRefContainer> LeftDirectedBinaryTreeWalker<&'ctx mut Ctx> { - pub fn wrap(ctx: &'ctx mut Ctx) -> Self { - return Self { ctx }; - } +macro_rules! _constructor { + ($name:ident $($mut:tt)?) => { + impl<'ctx, Ctx: NodeRefContainer> $name <&'ctx $($mut)? Ctx> { + pub fn wrap(ctx: &'ctx $($mut)? Ctx) -> Self { + return Self { ctx }; + } + } + }; } -impl<'ctx, Ctx: NodeRefContainer> RightDirectedBinaryTreeWalker<&'ctx Ctx> { - pub fn wrap(ctx: &'ctx Ctx) -> Self { - return Self { ctx }; - } +_mut_switch!(_constructor!(LeftDirectedBinaryTreeWalker)); +_mut_switch!(_constructor!(RightDirectedBinaryTreeWalker)); + +macro_rules! _node_ref { + ($name:ident $($mut:tt)?) => { + impl NodeRefContainer for $name<& $($mut)? Ctx> { + type NodeRef = Ctx::NodeRef; + } + }; } -impl<'ctx, Ctx: NodeRefContainer> RightDirectedBinaryTreeWalker<&'ctx mut Ctx> { - pub fn wrap(ctx: &'ctx mut Ctx) -> Self { - return Self { ctx }; - } +_mut_switch!(_node_ref!(LeftDirectedBinaryTreeWalker)); +_mut_switch!(_node_ref!(RightDirectedBinaryTreeWalker)); + +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); + } + } + }; } -impl NodeRefContainer for LeftDirectedBinaryTreeWalker<&Ctx> { - type NodeRef = Ctx::NodeRef; +_mut_switch!(_parent_get!(LeftDirectedBinaryTreeWalker)); +_mut_switch!(_parent_get!(RightDirectedBinaryTreeWalker)); + +macro_rules! _children_get { + ($name:ident $fwd:ident $op:ident $($mut:tt)? ) => { + impl DirectedBinaryTreeChildrenGetterContext + for $name <& $($mut)? Ctx> + { + fn getForwardChild(&self, node: Self::NodeRef) -> Option { + return self.ctx.$fwd(node); + } + + fn getOppositeChild(&self, node: Self::NodeRef) -> Option { + return self.ctx.$op(node); + } + } + }; } -impl NodeRefContainer for LeftDirectedBinaryTreeWalker<&mut Ctx> { - type NodeRef = Ctx::NodeRef; +_mut_switch!(_children_get!(LeftDirectedBinaryTreeWalker getLeftChild getRightChild)); +_mut_switch!(_children_get!(RightDirectedBinaryTreeWalker getRightChild getLeftChild)); + +macro_rules! _parent_set { + ($name:ident) => { + unsafe impl BinaryTreeParentSetterContext + for $name <&mut Ctx> + { + fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option) { + self.ctx.xSetParent(node, newParent); + } + + fn setParent(&mut self, node: Self::NodeRef, newParent: Self::NodeRef) { + self.ctx.setParent(node, newParent); + } + + fn clearParent(&mut self, node: Self::NodeRef) { + self.ctx.clearParent(node) + } + } + }; } +_parent_set!(LeftDirectedBinaryTreeWalker); +_parent_set!(RightDirectedBinaryTreeWalker); -impl NodeRefContainer for RightDirectedBinaryTreeWalker<&Ctx> { - type NodeRef = Ctx::NodeRef; -} - -impl NodeRefContainer for RightDirectedBinaryTreeWalker<&mut Ctx> { - type NodeRef = Ctx::NodeRef; -} - -impl BinaryTreeParentGetterContext for LeftDirectedBinaryTreeWalker<&Ctx> { - fn getParent(&self, node: Self::NodeRef) -> Option { - return self.ctx.getParent(node); - } -} - -impl BinaryTreeParentGetterContext for LeftDirectedBinaryTreeWalker<&mut Ctx> { - fn getParent(&self, node: Self::NodeRef) -> Option { - return self.ctx.getParent(node); - } -} - -impl BinaryTreeParentGetterContext for RightDirectedBinaryTreeWalker<&Ctx> { - fn getParent(&self, node: Self::NodeRef) -> Option { - return self.ctx.getParent(node); - } -} - -impl BinaryTreeParentGetterContext for RightDirectedBinaryTreeWalker<&mut Ctx> { - fn getParent(&self, node: Self::NodeRef) -> Option { - return self.ctx.getParent(node); - } -} - -impl DirectedBinaryTreeChildrenGetterContext - for LeftDirectedBinaryTreeWalker<&Ctx> -{ - fn getForwardChild(&self, node: Self::NodeRef) -> Option { - return self.ctx.getLeftChild(node); - } - - fn getOppositeChild(&self, node: Self::NodeRef) -> Option { - return self.ctx.getRightChild(node); - } -} - -impl DirectedBinaryTreeChildrenGetterContext - for LeftDirectedBinaryTreeWalker<&mut Ctx> -{ - fn getForwardChild(&self, node: Self::NodeRef) -> Option { - return self.ctx.getLeftChild(node); - } - - fn getOppositeChild(&self, node: Self::NodeRef) -> Option { - return self.ctx.getRightChild(node); - } -} - -impl DirectedBinaryTreeChildrenGetterContext - for RightDirectedBinaryTreeWalker<&Ctx> -{ - fn getForwardChild(&self, node: Self::NodeRef) -> Option { - return self.ctx.getRightChild(node); - } - - fn getOppositeChild(&self, node: Self::NodeRef) -> Option { - return self.ctx.getLeftChild(node); - } -} - -impl DirectedBinaryTreeChildrenGetterContext - for RightDirectedBinaryTreeWalker<&mut Ctx> -{ - fn getForwardChild(&self, node: Self::NodeRef) -> Option { - return self.ctx.getRightChild(node); - } - - fn getOppositeChild(&self, node: Self::NodeRef) -> Option { - return self.ctx.getLeftChild(node); - } -} - -unsafe impl DirectedBinaryTreeChildrenSetterContext - for LeftDirectedBinaryTreeWalker<&mut Ctx> -{ - fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option) { - self.ctx.xSetLeftChild(node, newChild) - } - - fn xSetOppositeChild(&mut self, node: Self::NodeRef, newChild: Option) { - self.ctx.xSetRightChild(node, newChild) - } - - fn setForwardChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) { - self.ctx.setLeftChild(node, newChild) - } - - fn setOppositeChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) { - self.ctx.setRightChild(node, newChild) - } - - fn clearForwardChild(&mut self, node: Self::NodeRef) { - self.ctx.clearLeftChild(node) - } - - fn clearOppositeChild(&mut self, node: Self::NodeRef) { - self.ctx.clearRightChild(node) - } -} - -unsafe impl DirectedBinaryTreeChildrenSetterContext - for RightDirectedBinaryTreeWalker<&mut Ctx> -{ - fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option) { - self.ctx.xSetRightChild(node, newChild) - } - - fn xSetOppositeChild(&mut self, node: Self::NodeRef, newChild: Option) { - self.ctx.xSetLeftChild(node, newChild) - } - - fn setForwardChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) { - self.ctx.setRightChild(node, newChild) - } - - fn setOppositeChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) { - self.ctx.setLeftChild(node, newChild) - } - - fn clearForwardChild(&mut self, node: Self::NodeRef) { - self.ctx.clearRightChild(node) - } - - fn clearOppositeChild(&mut self, node: Self::NodeRef) { - self.ctx.clearLeftChild(node) - } -} -unsafe impl BinaryTreeParentSetterContext - for LeftDirectedBinaryTreeWalker<&mut Ctx> -{ - fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option) { - self.ctx.xSetParent(node, newParent); - } - - fn setParent(&mut self, node: Self::NodeRef, newParent: Self::NodeRef) { - self.ctx.setParent(node, newParent); - } - - fn clearParent(&mut self, node: Self::NodeRef) { - self.ctx.clearParent(node) - } -} - -unsafe impl BinaryTreeParentSetterContext - for RightDirectedBinaryTreeWalker<&mut Ctx> -{ - fn xSetParent(&mut self, node: Self::NodeRef, newParent: Option) { - self.ctx.xSetParent(node, newParent); - } - - fn setParent(&mut self, node: Self::NodeRef, newParent: Self::NodeRef) { - self.ctx.setParent(node, newParent); - } - - fn clearParent(&mut self, node: Self::NodeRef) { - self.ctx.clearParent(node) - } +macro_rules! _children_set { + ( + $name:ident + $fwd_xset:ident $fwd_set:ident $fwd_clear:ident + $op_xset:ident $op_set:ident $op_clear:ident + ) => { + unsafe impl DirectedBinaryTreeChildrenSetterContext + for $name<&mut Ctx> + { + fn xSetForwardChild(&mut self, node: Self::NodeRef, newChild: Option) { + self.ctx.$fwd_xset(node, newChild) + } + + fn xSetOppositeChild(&mut self, node: Self::NodeRef, newChild: Option) { + self.ctx.$op_xset(node, newChild) + } + + fn setForwardChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) { + self.ctx.$fwd_set(node, newChild) + } + + fn setOppositeChild(&mut self, node: Self::NodeRef, newChild: Self::NodeRef) { + self.ctx.$op_set(node, newChild) + } + + fn clearForwardChild(&mut self, node: Self::NodeRef) { + self.ctx.$fwd_clear(node) + } + + fn clearOppositeChild(&mut self, node: Self::NodeRef) { + self.ctx.$op_clear(node) + } + } + }; } +_children_set!( + LeftDirectedBinaryTreeWalker + xSetLeftChild setLeftChild clearLeftChild + xSetRightChild setRightChild clearRightChild +); +_children_set!( + RightDirectedBinaryTreeWalker + xSetRightChild setRightChild clearRightChild + xSetLeftChild setLeftChild clearLeftChild +); diff --git a/src/parent2node/fixed_wrapper.rs b/src/parent2node/fixed_wrapper.rs index 76ff0ce..dc33e28 100644 --- a/src/parent2node/fixed_wrapper.rs +++ b/src/parent2node/fixed_wrapper.rs @@ -1,62 +1,45 @@ use crate::NodeRefContainer; use crate::context::{ - BinaryTreeChildrenSetterContext, BinaryTreeChildrenGetterContext, BinaryTreeRootGetter, BinaryTreeRootSetter, + BinaryTreeChildrenGetterContext, BinaryTreeChildrenSetterContext, BinaryTreeRootGetter, + BinaryTreeRootSetter, +}; +use crate::directed::{ + DirectedBinaryTreeChildrenGetterContext, DirectedBinaryTreeChildrenSetterContext, }; -use crate::directed::{DirectedBinaryTreeChildrenSetterContext, DirectedBinaryTreeChildrenGetterContext}; use crate::parent2node::clojure::{Parent2NodeGetterClojure, Parent2NodeSetterClojure}; -struct Parent2LeftChildContextWrapper { - ctx: CtxRef, - node: NodeRef, +macro_rules! _mut_switch { + ($macro:tt $excl_mark:tt ( $($args:tt)+) ) => { + $macro$excl_mark($($args)+); + $macro$excl_mark($($args)+ mut); + }; } -struct Parent2RightChildContextWrapper { - ctx: CtxRef, - node: NodeRef, +macro_rules! _struct { + ($name:ident) => { + struct $name { + ctx: CtxRef, + node: NodeRef, + } + }; } struct Root2NodeContextWrapper { ctx: NodeRef, } +_struct!(Parent2LeftChildContextWrapper); +_struct!(Parent2RightChildContextWrapper); +_struct!(Parent2ForwardChildContextWrapper); +_struct!(Parent2OppositeChildContextWrapper); -struct Parent2ForwardChildContextWrapper { - ctx: CtxRef, - node: NodeRef, -} - -struct Parent2OppositeChildContextWrapper { - ctx: CtxRef, - node: NodeRef, -} - -impl<'ctx, CtxRef: NodeRefContainer> Parent2LeftChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> { - fn wrap(ctx: &'ctx CtxRef, node: CtxRef::NodeRef) -> Self { - return Self { ctx, node }; - } -} - -impl<'ctx, CtxRef: NodeRefContainer> - Parent2LeftChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn wrap(ctx: &'ctx mut CtxRef, node: CtxRef::NodeRef) -> Self { - return Self { ctx, node }; - } -} - -impl<'ctx, CtxRef: NodeRefContainer> - Parent2RightChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> -{ - fn wrap(ctx: &'ctx CtxRef, node: CtxRef::NodeRef) -> Self { - return Self { ctx, node }; - } -} - -impl<'ctx, CtxRef: NodeRefContainer> - Parent2RightChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn wrap(ctx: &'ctx mut CtxRef, node: CtxRef::NodeRef) -> Self { - return Self { ctx, node }; - } +macro_rules! _constructor { + ($name:ident $($mut:tt)?) => { + impl<'ctx, Ctx: NodeRefContainer> $name <&'ctx $($mut)? Ctx, Ctx::NodeRef> { + pub fn wrap(ctx: &'ctx $($mut)? Ctx, node: Ctx::NodeRef) -> Self { + return Self { ctx, node }; + } + } + }; } impl<'ctx, CtxRef: NodeRefContainer + BinaryTreeRootGetter> Root2NodeContextWrapper<&'ctx CtxRef> { @@ -71,60 +54,17 @@ impl<'ctx, CtxRef: NodeRefContainer> Root2NodeContextWrapper<&'ctx mut CtxRef> { } } -impl<'ctx, CtxRef: NodeRefContainer> - Parent2ForwardChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> -{ - fn wrap(ctx: &'ctx CtxRef, node: CtxRef::NodeRef) -> Self { - return Self { ctx, node }; - } -} +_mut_switch!(_constructor!(Parent2LeftChildContextWrapper)); +_mut_switch!(_constructor!(Parent2RightChildContextWrapper)); +_mut_switch!(_constructor!(Parent2ForwardChildContextWrapper)); +_mut_switch!(_constructor!(Parent2OppositeChildContextWrapper)); -impl<'ctx, CtxRef: NodeRefContainer> - Parent2ForwardChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn wrap(ctx: &'ctx mut CtxRef, node: CtxRef::NodeRef) -> Self { - return Self { ctx, node }; - } -} - -impl<'ctx, CtxRef: NodeRefContainer> - Parent2OppositeChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> -{ - fn wrap(ctx: &'ctx CtxRef, node: CtxRef::NodeRef) -> Self { - return Self { ctx, node }; - } -} - -impl<'ctx, CtxRef: NodeRefContainer> - Parent2OppositeChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn wrap(ctx: &'ctx mut CtxRef, node: CtxRef::NodeRef) -> Self { - return Self { ctx, node }; - } -} - -impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer - for Parent2LeftChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> -{ - type NodeRef = CtxRef::NodeRef; -} - -impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer - for Parent2LeftChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - type NodeRef = CtxRef::NodeRef; -} - -impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer - for Parent2RightChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> -{ - type NodeRef = CtxRef::NodeRef; -} - -impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer - for Parent2RightChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - type NodeRef = CtxRef::NodeRef; +macro_rules! _node_ref { + ($name:ident $($mut:tt)?) => { + impl NodeRefContainer for $name<& $($mut)? Ctx, Ctx::NodeRef> { + type NodeRef = Ctx::NodeRef; + } + }; } impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer for Root2NodeContextWrapper<&'ctx CtxRef> { @@ -137,60 +77,21 @@ impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer type NodeRef = CtxRef::NodeRef; } -impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer - for Parent2ForwardChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> -{ - type NodeRef = CtxRef::NodeRef; -} +_mut_switch!(_node_ref!(Parent2LeftChildContextWrapper)); +_mut_switch!(_node_ref!(Parent2RightChildContextWrapper)); +_mut_switch!(_node_ref!(Parent2ForwardChildContextWrapper)); +_mut_switch!(_node_ref!(Parent2OppositeChildContextWrapper)); -impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer - for Parent2ForwardChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - type NodeRef = CtxRef::NodeRef; -} - -impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer - for Parent2OppositeChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> -{ - type NodeRef = CtxRef::NodeRef; -} - -impl<'ctx, CtxRef: NodeRefContainer> NodeRefContainer - for Parent2OppositeChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - type NodeRef = CtxRef::NodeRef; -} - -impl<'ctx, CtxRef: BinaryTreeChildrenGetterContext> Parent2NodeGetterClojure - for Parent2LeftChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> -{ - fn getChild(&self) -> Self::NodeRef { - return self.ctx.getLeftChild(self.node); - } -} - -impl<'ctx, CtxRef: BinaryTreeChildrenGetterContext> Parent2NodeGetterClojure - for Parent2LeftChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn getChild(&self) -> Self::NodeRef { - return self.ctx.getLeftChild(self.node); - } -} - -impl<'ctx, CtxRef: BinaryTreeChildrenGetterContext> Parent2NodeGetterClojure - for Parent2RightChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> -{ - fn getChild(&self) -> Self::NodeRef { - return self.ctx.getRightChild(self.node); - } -} - -impl<'ctx, CtxRef: BinaryTreeChildrenGetterContext> Parent2NodeGetterClojure - for Parent2RightChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn getChild(&self) -> Self::NodeRef { - return self.ctx.getRightChild(self.node); - } +macro_rules! _get_child { + ($name:ident $fn:ident $($mut:tt)? ) => { + impl<'ctx, CtxRef: BinaryTreeChildrenGetterContext> Parent2NodeGetterClojure + for $name <&'ctx $($mut)? CtxRef, CtxRef::NodeRef> + { + fn getChild(&self) -> Self::NodeRef { + return self.ctx.$fn(self.node); + } + } + }; } impl<'ctx, CtxRef: BinaryTreeRootGetter> Parent2NodeGetterClojure @@ -209,70 +110,29 @@ impl<'ctx, CtxRef: BinaryTreeRootGetter> Parent2NodeGetterClojure } } +_mut_switch!(_get_child!(Parent2LeftChildContextWrapper getLeftChild)); +_mut_switch!(_get_child!(Parent2RightChildContextWrapper getRightChild)); +_mut_switch!(_get_child!(Parent2ForwardChildContextWrapper getForwardChild)); +_mut_switch!(_get_child!(Parent2OppositeChildContextWrapper getOppositeChild)); +macro_rules! _set_child { + ($name:ident $xset:ident $set:ident $clear:ident) => { + impl<'ctx, CtxRef: BinaryTreeChildrenSetterContext> Parent2NodeSetterClojure + for $name<&'ctx mut CtxRef, CtxRef::NodeRef> + { + fn xSetChild(&mut self, newChild: Option) { + self.ctx.$xset(self.node, newChild); + } -impl<'ctx, CtxRef: DirectedBinaryTreeChildrenGetterContext> Parent2NodeGetterClojure -for Parent2ForwardChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> -{ - fn getChild(&self) -> Self::NodeRef { - return self.ctx.getForwardChild(self.node); - } -} + fn setChild(&mut self, newChild: Self::NodeRef) { + self.ctx.$set(self.node, newChild); + } -impl<'ctx, CtxRef: DirectedBinaryTreeChildrenGetterContext> Parent2NodeGetterClojure -for Parent2ForwardChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn getChild(&self) -> Self::NodeRef { - return self.ctx.getForwardChild(self.node); - } -} - -impl<'ctx, CtxRef: DirectedBinaryTreeChildrenGetterContext> Parent2NodeGetterClojure -for Parent2OppositeChildContextWrapper<&'ctx CtxRef, CtxRef::NodeRef> -{ - fn getChild(&self) -> Self::NodeRef { - return self.ctx.getOppositeChild(self.node); - } -} - -impl<'ctx, CtxRef: DirectedBinaryTreeChildrenGetterContext> Parent2NodeGetterClojure -for Parent2OppositeChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn getChild(&self) -> Self::NodeRef { - return self.ctx.getOppositeChild(self.node); - } -} - -impl<'ctx, CtxRef: BinaryTreeChildrenSetterContext> Parent2NodeSetterClojure - for Parent2LeftChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn xSetChild(&mut self, newChild: Option) { - self.ctx.xSetLeftChild(self.node, newChild); - } - - fn setChild(&mut self, newChild: Self::NodeRef) { - self.ctx.setLeftChild(self.node, newChild); - } - - fn clearChild(&mut self) { - self.ctx.clearLeftChild(self.node); - } -} - -impl<'ctx, CtxRef: BinaryTreeChildrenSetterContext> Parent2NodeSetterClojure - for Parent2RightChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn xSetChild(&mut self, newChild: Option) { - self.ctx.xSetRightChild(self.node, newChild); - } - - fn setChild(&mut self, newChild: Self::NodeRef) { - self.ctx.setRightChild(self.node, newChild); - } - - fn clearChild(&mut self) { - self.ctx.clearRightChild(self.node); - } + fn clearChild(&mut self) { + self.ctx.$clear(self.node); + } + } + }; } impl<'ctx, CtxRef: BinaryTreeRootSetter> Parent2NodeSetterClojure @@ -291,36 +151,7 @@ impl<'ctx, CtxRef: BinaryTreeRootSetter> Parent2NodeSetterClojure } } - -impl<'ctx, CtxRef: DirectedBinaryTreeChildrenSetterContext> Parent2NodeSetterClojure -for Parent2ForwardChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn xSetChild(&mut self, newChild: Option) { - self.ctx.xSetLeftChild(self.node, newChild); - } - - fn setChild(&mut self, newChild: Self::NodeRef) { - self.ctx.setLeftChild(self.node, newChild); - } - - fn clearChild(&mut self) { - self.ctx.clearLeftChild(self.node); - } -} - -impl<'ctx, CtxRef: DirectedBinaryTreeChildrenSetterContext> Parent2NodeSetterClojure -for Parent2OppositeChildContextWrapper<&'ctx mut CtxRef, CtxRef::NodeRef> -{ - fn xSetChild(&mut self, newChild: Option) { - self.ctx.xSetRightChild(self.node, newChild); - } - - fn setChild(&mut self, newChild: Self::NodeRef) { - self.ctx.setRightChild(self.node, newChild); - } - - fn clearChild(&mut self) { - self.ctx.clearRightChild(self.node); - } -} - +_set_child!(Parent2LeftChildContextWrapper xSetLeftChild setLeftChild clearLeftChild); +_set_child!(Parent2RightChildContextWrapper xSetRightChild setRightChild clearRightChild); +_set_child!(Parent2ForwardChildContextWrapper xSetForwardChild setForwardChild clearForwardChild); +_set_child!(Parent2OppositeChildContextWrapper xSetOppositeChild setOppositeChild clearOppositeChild);