Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,12 +756,13 @@ impl<'a, T: 'a> NodeMut<'a, T> {
"Cannot insert node as a sibling of itself"
);

self.tree.get_mut(new_sibling_id).unwrap().detach();

let parent_id = self.node().parent.unwrap();
let prev_sibling_id = self.node().prev_sibling;

{
let mut new_sibling = self.tree.get_mut(new_sibling_id).unwrap();
new_sibling.detach();
new_sibling.node().parent = Some(parent_id);
new_sibling.node().prev_sibling = prev_sibling_id;
new_sibling.node().next_sibling = Some(self.id);
Expand Down Expand Up @@ -799,12 +800,13 @@ impl<'a, T: 'a> NodeMut<'a, T> {
"Cannot insert node as a sibling of itself"
);

self.tree.get_mut(new_sibling_id).unwrap().detach();

let parent_id = self.node().parent.unwrap();
let next_sibling_id = self.node().next_sibling;

{
let mut new_sibling = self.tree.get_mut(new_sibling_id).unwrap();
new_sibling.detach();
new_sibling.node().parent = Some(parent_id);
new_sibling.node().prev_sibling = Some(self.id);
new_sibling.node().next_sibling = next_sibling_id;
Expand Down
32 changes: 32 additions & 0 deletions tests/node_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,38 @@ fn prepend_id_noop() {
assert_eq!(a.last_child().unwrap().prev_sibling().unwrap().id(), b_id);
}

#[test]
fn insert_id_before_loop() {
let mut tree = tree! {
'a' => { 'b', 'c' }
};
let mut a = tree.root_mut();
let b_id = a.first_child().unwrap().id();
let mut c = a.last_child().unwrap();
c.insert_id_before(b_id);
let c_id = c.id();

let b = tree.get(b_id).unwrap();
assert_eq!(b.prev_sibling(), None);
assert_eq!(b.next_sibling().unwrap().id(), c_id);
}

#[test]
fn insert_id_after_loop() {
let mut tree = tree! {
'a' => { 'b', 'c' }
};
let mut a = tree.root_mut();
let c_id = a.last_child().unwrap().id();
let mut b = a.first_child().unwrap();
b.insert_id_after(c_id);
let b_id = b.id();

let c = tree.get(c_id).unwrap();
assert_eq!(c.prev_sibling().unwrap().id(), b_id);
assert_eq!(c.next_sibling(), None);
}

#[test]
fn reparent_from_id_append() {
let mut tree = tree! {
Expand Down
Loading