@@ -550,6 +550,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
550550 ///
551551 /// Panics if `new_child_id` is not valid.
552552 pub fn append_id ( & mut self , new_child_id : NodeId ) -> NodeMut < T > {
553+ assert_ne ! (
554+ self . id( ) ,
555+ new_child_id,
556+ "Cannot append node as a child to itself"
557+ ) ;
558+
553559 let last_child_id = self . node ( ) . children . map ( |( _, id) | id) ;
554560 {
555561 let mut new_child = self . tree . get_mut ( new_child_id) . unwrap ( ) ;
@@ -565,11 +571,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
565571 }
566572
567573 {
568- if let Some ( ( first_child_id, _) ) = self . node ( ) . children {
569- self . node ( ) . children = Some ( ( first_child_id, new_child_id) ) ;
570- } else {
571- self . node ( ) . children = Some ( ( new_child_id, new_child_id) ) ;
572- }
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+ } ;
573578 }
574579
575580 unsafe { self . tree . get_unchecked_mut ( new_child_id) }
@@ -581,6 +586,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
581586 ///
582587 /// Panics if `new_child_id` is not valid.
583588 pub fn prepend_id ( & mut self , new_child_id : NodeId ) -> NodeMut < T > {
589+ assert_ne ! (
590+ self . id( ) ,
591+ new_child_id,
592+ "Cannot prepend node as a child to itself"
593+ ) ;
594+
584595 let first_child_id = self . node ( ) . children . map ( |( id, _) | id) ;
585596 {
586597 let mut new_child = self . tree . get_mut ( new_child_id) . unwrap ( ) ;
@@ -596,11 +607,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
596607 }
597608
598609 {
599- if let Some ( ( _, last_child_id) ) = self . node ( ) . children {
600- self . node ( ) . children = Some ( ( new_child_id, last_child_id) ) ;
601- } else {
602- self . node ( ) . children = Some ( ( new_child_id, new_child_id) ) ;
603- }
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+ } ;
604614 }
605615
606616 unsafe { self . tree . get_unchecked_mut ( new_child_id) }
@@ -613,6 +623,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
613623 /// - Panics if `new_sibling_id` is not valid.
614624 /// - Panics if this node is an orphan.
615625 pub fn insert_id_before ( & mut self , new_sibling_id : NodeId ) -> NodeMut < T > {
626+ assert_ne ! (
627+ self . id( ) ,
628+ new_sibling_id,
629+ "Cannot insert node as a sibling of itself"
630+ ) ;
631+
616632 let parent_id = self . node ( ) . parent . unwrap ( ) ;
617633 let prev_sibling_id = self . node ( ) . prev_sibling ;
618634
@@ -650,6 +666,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
650666 /// - Panics if `new_sibling_id` is not valid.
651667 /// - Panics if this node is an orphan.
652668 pub fn insert_id_after ( & mut self , new_sibling_id : NodeId ) -> NodeMut < T > {
669+ assert_ne ! (
670+ self . id( ) ,
671+ new_sibling_id,
672+ "Cannot insert node as a sibling of itself"
673+ ) ;
674+
653675 let parent_id = self . node ( ) . parent . unwrap ( ) ;
654676 let next_sibling_id = self . node ( ) . next_sibling ;
655677
@@ -686,6 +708,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
686708 ///
687709 /// Panics if `from_id` is not valid.
688710 pub fn reparent_from_id_append ( & mut self , from_id : NodeId ) {
711+ assert_ne ! (
712+ self . id( ) ,
713+ from_id,
714+ "Cannot reparent node's children to itself"
715+ ) ;
716+
689717 let new_child_ids = {
690718 let mut from = self . tree . get_mut ( from_id) . unwrap ( ) ;
691719 match from. node ( ) . children . take ( ) {
@@ -719,6 +747,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
719747 ///
720748 /// Panics if `from_id` is not valid.
721749 pub fn reparent_from_id_prepend ( & mut self , from_id : NodeId ) {
750+ assert_ne ! (
751+ self . id( ) ,
752+ from_id,
753+ "Cannot reparent node's children to itself"
754+ ) ;
755+
722756 let new_child_ids = {
723757 let mut from = self . tree . get_mut ( from_id) . unwrap ( ) ;
724758 match from. node ( ) . children . take ( ) {
0 commit comments