Skip to content

Commit 525bcde

Browse files
committed
Cleanup + cargo fmt
Cleaned up loop to skip 2nd access to `tree.node_mut`, and rust fmt the unit tests.
1 parent 72e73f4 commit 525bcde

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/lib.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -843,12 +843,14 @@ impl<'a, T: 'a> NodeMut<'a, T> {
843843
}
844844
};
845845

846-
// Update parent for all siblings in the chain
847846
let mut child_id = new_child_ids.0;
848847
loop {
849-
unsafe { self.tree.node_mut(child_id).parent = Some(self.id); }
850-
if child_id == new_child_ids.1 { break; }
851-
child_id = unsafe { self.tree.node_mut(child_id).next_sibling.unwrap() };
848+
let child = unsafe { self.tree.node_mut(child_id) };
849+
child.parent = Some(self.id);
850+
child_id = match child.next_sibling {
851+
Some(id) => id,
852+
None => break,
853+
};
852854
}
853855

854856
if self.node().children.is_none() {
@@ -885,12 +887,14 @@ impl<'a, T: 'a> NodeMut<'a, T> {
885887
}
886888
};
887889

888-
// Update parent for all siblings in the chain
889890
let mut child_id = new_child_ids.0;
890891
loop {
891-
unsafe { self.tree.node_mut(child_id).parent = Some(self.id); }
892-
if child_id == new_child_ids.1 { break; }
893-
child_id = unsafe { self.tree.node_mut(child_id).next_sibling.unwrap() };
892+
let child = unsafe { self.tree.node_mut(child_id) };
893+
child.parent = Some(self.id);
894+
child_id = match child.next_sibling {
895+
Some(id) => id,
896+
None => break,
897+
};
894898
}
895899

896900
if self.node().children.is_none() {

tests/node_mut.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,10 @@ fn reparent_from_id_append_multiple_siblings() {
512512
.reparent_from_id_append(c_id);
513513

514514
let b = tree.get(b_id).unwrap();
515-
let x = b.first_child().unwrap(); // original child of b
516-
let d = x.next_sibling().unwrap(); // first reparented child
517-
let e = d.next_sibling().unwrap(); // middle reparented child
518-
let f = e.next_sibling().unwrap(); // last reparented child
515+
let x = b.first_child().unwrap(); // original child of b
516+
let d = x.next_sibling().unwrap(); // first reparented child
517+
let e = d.next_sibling().unwrap(); // middle reparented child
518+
let f = e.next_sibling().unwrap(); // last reparented child
519519

520520
// All children should now have 'b' as their parent
521521
assert_eq!(Some(b), x.parent());
@@ -544,7 +544,7 @@ fn reparent_from_id_prepend_multiple_siblings() {
544544
.reparent_from_id_prepend(c_id);
545545

546546
let b = tree.get(b_id).unwrap();
547-
let d = b.first_child().unwrap(); // first reparented child
547+
let d = b.first_child().unwrap(); // first reparented child
548548
let e = d.next_sibling().unwrap(); // middle reparented child
549549
let f = e.next_sibling().unwrap(); // last reparented child
550550
let x = f.next_sibling().unwrap(); // original child of b

0 commit comments

Comments
 (0)