@@ -557,24 +557,27 @@ impl<'a, T: 'a> NodeMut<'a, T> {
557557 ) ;
558558
559559 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- }
566560
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;
570567 }
571- }
572568
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+ }
578581 }
579582
580583 unsafe { self . tree . get_unchecked_mut ( new_child_id) }
@@ -593,24 +596,27 @@ impl<'a, T: 'a> NodeMut<'a, T> {
593596 ) ;
594597
595598 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- }
602599
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;
606606 }
607- }
608607
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+ }
614620 }
615621
616622 unsafe { self . tree . get_unchecked_mut ( new_child_id) }
0 commit comments