Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,9 +843,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
}
};

unsafe {
self.tree.node_mut(new_child_ids.0).parent = Some(self.id);
self.tree.node_mut(new_child_ids.1).parent = Some(self.id);
// Update parent for all siblings in the chain
let mut child_id = new_child_ids.0;
loop {
unsafe { self.tree.node_mut(child_id).parent = Some(self.id); }
if child_id == new_child_ids.1 { break; }
child_id = unsafe { self.tree.node_mut(child_id).next_sibling.unwrap() };
}

if self.node().children.is_none() {
Expand Down Expand Up @@ -882,9 +885,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
}
};

unsafe {
self.tree.node_mut(new_child_ids.0).parent = Some(self.id);
self.tree.node_mut(new_child_ids.1).parent = Some(self.id);
// Update parent for all siblings in the chain
let mut child_id = new_child_ids.0;
loop {
unsafe { self.tree.node_mut(child_id).parent = Some(self.id); }
if child_id == new_child_ids.1 { break; }
child_id = unsafe { self.tree.node_mut(child_id).next_sibling.unwrap() };
}

if self.node().children.is_none() {
Expand Down
64 changes: 64 additions & 0 deletions tests/node_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,70 @@ fn reparent_from_id_prepend() {
assert_eq!(Some(d), f.prev_sibling());
}

#[test]
fn reparent_from_id_append_multiple_siblings() {
// Test reparenting when source has 3+ children to ensure all siblings get proper parent update
let mut tree = tree! {
'a' => {
'b' => { 'x' }, // destination node
'c' => { 'd', 'e', 'f' }, // source node with 3 children
}
};

let c_id = tree.root().last_child().unwrap().id();
let b_id = tree.root().first_child().unwrap().id();

// Reparent children from 'c' to 'b'
tree.root_mut()
.first_child()
.unwrap()
.reparent_from_id_append(c_id);

let b = tree.get(b_id).unwrap();
let x = b.first_child().unwrap(); // original child of b
let d = x.next_sibling().unwrap(); // first reparented child
let e = d.next_sibling().unwrap(); // middle reparented child
let f = e.next_sibling().unwrap(); // last reparented child

// All children should now have 'b' as their parent
assert_eq!(Some(b), x.parent());
assert_eq!(Some(b), d.parent());
assert_eq!(Some(b), e.parent());
assert_eq!(Some(b), f.parent());
}

#[test]
fn reparent_from_id_prepend_multiple_siblings() {
// Test reparenting when source has 3+ children to ensure all siblings get proper parent update
let mut tree = tree! {
'a' => {
'b' => { 'x' }, // destination node
'c' => { 'd', 'e', 'f' }, // source node with 3 children
}
};

let c_id = tree.root().last_child().unwrap().id();
let b_id = tree.root().first_child().unwrap().id();

// Reparent children from 'c' to 'b'
tree.root_mut()
.first_child()
.unwrap()
.reparent_from_id_prepend(c_id);

let b = tree.get(b_id).unwrap();
let d = b.first_child().unwrap(); // first reparented child
let e = d.next_sibling().unwrap(); // middle reparented child
let f = e.next_sibling().unwrap(); // last reparented child
let x = f.next_sibling().unwrap(); // original child of b

// All children should now have 'b' as their parent
assert_eq!(Some(b), d.parent());
assert_eq!(Some(b), e.parent());
assert_eq!(Some(b), f.parent());
assert_eq!(Some(b), x.parent());
}

#[test]
fn into() {
let mut tree = tree!('a');
Expand Down