@@ -557,24 +557,27 @@ impl<'a, T: 'a> NodeMut<'a, T> {
557
557
) ;
558
558
559
559
let last_child_id = self . node ( ) . children . map ( |( _, id) | id) ;
560
- {
561
- let mut new_child = self . tree . get_mut ( new_child_id) . unwrap ( ) ;
562
- new_child. detach ( ) ;
563
- new_child. node ( ) . parent = Some ( self . id ) ;
564
- new_child. node ( ) . prev_sibling = last_child_id;
565
- }
566
560
567
- if let Some ( id) = last_child_id {
568
- unsafe {
569
- self . tree . node_mut ( id) . next_sibling = Some ( new_child_id) ;
561
+ if last_child_id != Some ( new_child_id) {
562
+ {
563
+ let mut new_child = self . tree . get_mut ( new_child_id) . unwrap ( ) ;
564
+ new_child. detach ( ) ;
565
+ new_child. node ( ) . parent = Some ( self . id ) ;
566
+ new_child. node ( ) . prev_sibling = last_child_id;
570
567
}
571
- }
572
568
573
- {
574
- self . node ( ) . children = match self . node ( ) . children {
575
- Some ( ( first_child_id, _) ) => Some ( ( first_child_id, new_child_id) ) ,
576
- None => Some ( ( new_child_id, new_child_id) ) ,
577
- } ;
569
+ if let Some ( id) = last_child_id {
570
+ unsafe {
571
+ self . tree . node_mut ( id) . next_sibling = Some ( new_child_id) ;
572
+ }
573
+ }
574
+
575
+ {
576
+ self . node ( ) . children = match self . node ( ) . children {
577
+ Some ( ( first_child_id, _) ) => Some ( ( first_child_id, new_child_id) ) ,
578
+ None => Some ( ( new_child_id, new_child_id) ) ,
579
+ } ;
580
+ }
578
581
}
579
582
580
583
unsafe { self . tree . get_unchecked_mut ( new_child_id) }
@@ -593,24 +596,27 @@ impl<'a, T: 'a> NodeMut<'a, T> {
593
596
) ;
594
597
595
598
let first_child_id = self . node ( ) . children . map ( |( id, _) | id) ;
596
- {
597
- let mut new_child = self . tree . get_mut ( new_child_id) . unwrap ( ) ;
598
- new_child. detach ( ) ;
599
- new_child. node ( ) . parent = Some ( self . id ) ;
600
- new_child. node ( ) . next_sibling = first_child_id;
601
- }
602
599
603
- if let Some ( id) = first_child_id {
604
- unsafe {
605
- self . tree . node_mut ( id) . prev_sibling = Some ( new_child_id) ;
600
+ if first_child_id != Some ( new_child_id) {
601
+ {
602
+ let mut new_child = self . tree . get_mut ( new_child_id) . unwrap ( ) ;
603
+ new_child. detach ( ) ;
604
+ new_child. node ( ) . parent = Some ( self . id ) ;
605
+ new_child. node ( ) . next_sibling = first_child_id;
606
606
}
607
- }
608
607
609
- {
610
- self . node ( ) . children = match self . node ( ) . children {
611
- Some ( ( _, last_child_id) ) => Some ( ( new_child_id, last_child_id) ) ,
612
- None => Some ( ( new_child_id, new_child_id) ) ,
613
- } ;
608
+ if let Some ( id) = first_child_id {
609
+ unsafe {
610
+ self . tree . node_mut ( id) . prev_sibling = Some ( new_child_id) ;
611
+ }
612
+ }
613
+
614
+ {
615
+ self . node ( ) . children = match self . node ( ) . children {
616
+ Some ( ( _, last_child_id) ) => Some ( ( new_child_id, last_child_id) ) ,
617
+ None => Some ( ( new_child_id, new_child_id) ) ,
618
+ } ;
619
+ }
614
620
}
615
621
616
622
unsafe { self . tree . get_unchecked_mut ( new_child_id) }
0 commit comments