Now node references is clone instead of copy (e.g. for Python's Py_IncRef) and node ref now is separate trait to forbid use of existing types

This commit is contained in:
Andrew Golovashevich 2025-12-27 14:45:41 +03:00
parent fbe424c276
commit 67917161f5
5 changed files with 33 additions and 31 deletions

View File

@ -11,14 +11,14 @@ fn goDown<Ctx: BinaryTreeChildrenGetterContext>(
let mut p = root; let mut p = root;
match dir { match dir {
BinaryTreeDirection::LEFT => loop { BinaryTreeDirection::LEFT => loop {
match ctx.getLeftChild(p) { match ctx.getLeftChild(p.clone()) {
None => return p, None => return p,
Some(c) => p = c, Some(c) => p = c,
} }
}, },
BinaryTreeDirection::RIGHT => loop { BinaryTreeDirection::RIGHT => loop {
match ctx.getRightChild(p) { match ctx.getRightChild(p.clone()) {
None => return p, None => return p,
Some(c) => p = c, Some(c) => p = c,
} }
@ -34,14 +34,14 @@ fn goDownDirected<Ctx: DirectedBinaryTreeChildrenGetterContext>(
let mut p = root; let mut p = root;
match dir { match dir {
DirectedBinaryTreeDirection::FORWARD => loop { DirectedBinaryTreeDirection::FORWARD => loop {
match ctx.getForwardChild(p) { match ctx.getForwardChild(p.clone()) {
None => return p, None => return p,
Some(c) => p = c, Some(c) => p = c,
} }
}, },
DirectedBinaryTreeDirection::OPPOSITE => loop { DirectedBinaryTreeDirection::OPPOSITE => loop {
match ctx.getOppositeChild(p) { match ctx.getOppositeChild(p.clone()) {
None => return p, None => return p,
Some(c) => p = c, Some(c) => p = c,
} }

View File

@ -16,23 +16,23 @@ unsafe fn swapNeighbors<
parent: Ctx::NodeRef, parent: Ctx::NodeRef,
child: Ctx::NodeRef, child: Ctx::NodeRef,
) { ) {
ctx.xSetParent(child, grandparent); ctx.xSetParent(child.clone(), grandparent);
parentLocation.setChild(child); parentLocation.setChild(child.clone());
ctx.xSetForwardChild(parent, ctx.getForwardChild(child)); ctx.xSetForwardChild(parent.clone(), ctx.getForwardChild(child.clone()));
let oppositeChild = ctx.getOppositeChild(parent); let oppositeChild = ctx.getOppositeChild(parent.clone());
ctx.xSetOppositeChild(parent, ctx.getOppositeChild(child)); ctx.xSetOppositeChild(parent.clone(), ctx.getOppositeChild(child.clone()));
ctx.xSetOppositeChild(child, oppositeChild); ctx.xSetOppositeChild(child.clone(), oppositeChild);
if let Some(n) = ctx.getOppositeChild(child) { if let Some(n) = ctx.getOppositeChild(child.clone()) {
ctx.setParent(n, child) ctx.setParent(n, child.clone())
} }
if let Some(n) = ctx.getOppositeChild(parent) { if let Some(n) = ctx.getOppositeChild(parent.clone()) {
ctx.setParent(n, parent) ctx.setParent(n, parent.clone())
} }
if let Some(n) = ctx.getForwardChild(child) { if let Some(n) = ctx.getForwardChild(child.clone()) {
ctx.setParent(n, parent) ctx.setParent(n, parent.clone())
} }
ctx.setForwardChild(child, parent); ctx.setForwardChild(child.clone(), parent.clone());
ctx.setParent(parent, child); ctx.setParent(parent, child);
} }

View File

@ -1,5 +1,7 @@
pub trait NodeRef: Sized + Clone {}
pub trait NodeRefContainer { pub trait NodeRefContainer {
type NodeRef: Sized + Copy; type NodeRef: NodeRef;
} }
pub trait BinaryTreeRootGetter: NodeRefContainer { pub trait BinaryTreeRootGetter: NodeRefContainer {

View File

@ -82,10 +82,10 @@ macro_rules! _get_child {
match self.dir { match self.dir {
None => return self.ctx.getRoot(), None => return self.ctx.getRoot(),
Some($dir_enum_lbl::$d1_lbl) => unsafe { Some($dir_enum_lbl::$d1_lbl) => unsafe {
self.ctx.$d1_get (*self.node.node.deref()) self.ctx.$d1_get ((*self.node.node.deref()).clone())
}, },
Some($dir_enum_lbl::$d2_lbl) => unsafe { Some($dir_enum_lbl::$d2_lbl) => unsafe {
self.ctx.$d2_get (*self.node.node.deref()) self.ctx.$d2_get ((*self.node.node.deref()).clone())
} }
} }
} }
@ -118,10 +118,10 @@ macro_rules! _set_child {
match self.dir { match self.dir {
None => self.ctx.xSetRoot(newChild), None => self.ctx.xSetRoot(newChild),
Some($dir_enum_lbl::$d1_lbl) => unsafe { Some($dir_enum_lbl::$d1_lbl) => unsafe {
self.ctx.$d1_xset (*self.node.node.deref(), newChild) self.ctx.$d1_xset ((*self.node.node.deref()).clone(), newChild)
}, },
Some($dir_enum_lbl::$d2_lbl) => unsafe { Some($dir_enum_lbl::$d2_lbl) => unsafe {
self.ctx.$d2_xset (*self.node.node.deref(), newChild) self.ctx.$d2_xset ((*self.node.node.deref()).clone(), newChild)
} }
}; };
return; return;
@ -131,10 +131,10 @@ macro_rules! _set_child {
match self.dir { match self.dir {
None => self.ctx.setRoot(newChild), None => self.ctx.setRoot(newChild),
Some($dir_enum_lbl::$d1_lbl) => unsafe { Some($dir_enum_lbl::$d1_lbl) => unsafe {
self.ctx.$d1_set (*self.node.node.deref(), newChild) self.ctx.$d1_set ((*self.node.node.deref()).clone(), newChild)
}, },
Some($dir_enum_lbl::$d2_lbl) => unsafe { Some($dir_enum_lbl::$d2_lbl) => unsafe {
self.ctx.$d2_set (*self.node.node.deref(), newChild) self.ctx.$d2_set ((*self.node.node.deref()).clone(), newChild)
} }
}; };
return; return;
@ -144,10 +144,10 @@ macro_rules! _set_child {
match self.dir { match self.dir {
None => self.ctx.clearRoot(), None => self.ctx.clearRoot(),
Some($dir_enum_lbl::$d1_lbl) => unsafe { Some($dir_enum_lbl::$d1_lbl) => unsafe {
self.ctx.$d1_clear (*self.node.node.deref()) self.ctx.$d1_clear ((*self.node.node.deref()).clone())
}, },
Some($dir_enum_lbl::$d2_lbl) => unsafe { Some($dir_enum_lbl::$d2_lbl) => unsafe {
self.ctx.$d2_clear (*self.node.node.deref()) self.ctx.$d2_clear ((*self.node.node.deref()).clone())
} }
}; };
return; return;

View File

@ -100,7 +100,7 @@ macro_rules! _get_child {
for $name <&'ctx $($mut)? Ctx, Ctx::NodeRef> for $name <&'ctx $($mut)? Ctx, Ctx::NodeRef>
{ {
fn getChild(&self) -> Option<Self::NodeRef> { fn getChild(&self) -> Option<Self::NodeRef> {
return self.ctx.$fn (self.node); return self.ctx.$fn (self.node.clone());
} }
} }
}; };
@ -143,15 +143,15 @@ macro_rules! _set_child {
($name:ident $ctx:ident $xset:ident $set:ident $clear:ident) => { ($name:ident $ctx:ident $xset:ident $set:ident $clear:ident) => {
impl<'ctx, Ctx: $ctx> Parent2NodeSetterClojure for $name<&'ctx mut Ctx, Ctx::NodeRef> { impl<'ctx, Ctx: $ctx> Parent2NodeSetterClojure for $name<&'ctx mut Ctx, Ctx::NodeRef> {
fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) { fn xSetChild(&mut self, newChild: Option<Self::NodeRef>) {
self.ctx.$xset(self.node, newChild); self.ctx.$xset(self.node.clone(), newChild);
} }
fn setChild(&mut self, newChild: Self::NodeRef) { fn setChild(&mut self, newChild: Self::NodeRef) {
self.ctx.$set(self.node, newChild); self.ctx.$set(self.node.clone(), newChild);
} }
fn clearChild(&mut self) { fn clearChild(&mut self) {
self.ctx.$clear(self.node); self.ctx.$clear(self.node.clone());
} }
} }
}; };