@@ -550,6 +550,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
550
550
///
551
551
/// Panics if `new_child_id` is not valid.
552
552
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
+
553
559
let last_child_id = self . node ( ) . children . map ( |( _, id) | id) ;
554
560
{
555
561
let mut new_child = self . tree . get_mut ( new_child_id) . unwrap ( ) ;
@@ -565,11 +571,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
565
571
}
566
572
567
573
{
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
+ } ;
573
578
}
574
579
575
580
unsafe { self . tree . get_unchecked_mut ( new_child_id) }
@@ -581,6 +586,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
581
586
///
582
587
/// Panics if `new_child_id` is not valid.
583
588
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
+
584
595
let first_child_id = self . node ( ) . children . map ( |( id, _) | id) ;
585
596
{
586
597
let mut new_child = self . tree . get_mut ( new_child_id) . unwrap ( ) ;
@@ -596,11 +607,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
596
607
}
597
608
598
609
{
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
+ } ;
604
614
}
605
615
606
616
unsafe { self . tree . get_unchecked_mut ( new_child_id) }
@@ -613,6 +623,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
613
623
/// - Panics if `new_sibling_id` is not valid.
614
624
/// - Panics if this node is an orphan.
615
625
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
+
616
632
let parent_id = self . node ( ) . parent . unwrap ( ) ;
617
633
let prev_sibling_id = self . node ( ) . prev_sibling ;
618
634
@@ -650,6 +666,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
650
666
/// - Panics if `new_sibling_id` is not valid.
651
667
/// - Panics if this node is an orphan.
652
668
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
+
653
675
let parent_id = self . node ( ) . parent . unwrap ( ) ;
654
676
let next_sibling_id = self . node ( ) . next_sibling ;
655
677
@@ -686,6 +708,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
686
708
///
687
709
/// Panics if `from_id` is not valid.
688
710
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
+
689
717
let new_child_ids = {
690
718
let mut from = self . tree . get_mut ( from_id) . unwrap ( ) ;
691
719
match from. node ( ) . children . take ( ) {
@@ -719,6 +747,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
719
747
///
720
748
/// Panics if `from_id` is not valid.
721
749
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
+
722
756
let new_child_ids = {
723
757
let mut from = self . tree . get_mut ( from_id) . unwrap ( ) ;
724
758
match from. node ( ) . children . take ( ) {
0 commit comments