@@ -494,6 +494,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
494
494
///
495
495
/// Panics if `new_child_id` is not valid.
496
496
pub fn append_id ( & mut self , new_child_id : NodeId ) -> NodeMut < T > {
497
+ assert_ne ! (
498
+ self . id( ) ,
499
+ new_child_id,
500
+ "Cannot append node as a child to itself"
501
+ ) ;
502
+
497
503
let last_child_id = self . node ( ) . children . map ( |( _, id) | id) ;
498
504
{
499
505
let mut new_child = self . tree . get_mut ( new_child_id) . unwrap ( ) ;
@@ -509,11 +515,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
509
515
}
510
516
511
517
{
512
- if let Some ( ( first_child_id, _) ) = self . node ( ) . children {
513
- self . node ( ) . children = Some ( ( first_child_id, new_child_id) ) ;
514
- } else {
515
- self . node ( ) . children = Some ( ( new_child_id, new_child_id) ) ;
516
- }
518
+ self . node ( ) . children = match self . node ( ) . children {
519
+ Some ( ( first_child_id, _) ) => Some ( ( first_child_id, new_child_id) ) ,
520
+ None => Some ( ( new_child_id, new_child_id) ) ,
521
+ } ;
517
522
}
518
523
519
524
unsafe { self . tree . get_unchecked_mut ( new_child_id) }
@@ -525,6 +530,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
525
530
///
526
531
/// Panics if `new_child_id` is not valid.
527
532
pub fn prepend_id ( & mut self , new_child_id : NodeId ) -> NodeMut < T > {
533
+ assert_ne ! (
534
+ self . id( ) ,
535
+ new_child_id,
536
+ "Cannot prepend node as a child to itself"
537
+ ) ;
538
+
528
539
let first_child_id = self . node ( ) . children . map ( |( id, _) | id) ;
529
540
{
530
541
let mut new_child = self . tree . get_mut ( new_child_id) . unwrap ( ) ;
@@ -540,11 +551,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
540
551
}
541
552
542
553
{
543
- if let Some ( ( _, last_child_id) ) = self . node ( ) . children {
544
- self . node ( ) . children = Some ( ( new_child_id, last_child_id) ) ;
545
- } else {
546
- self . node ( ) . children = Some ( ( new_child_id, new_child_id) ) ;
547
- }
554
+ self . node ( ) . children = match self . node ( ) . children {
555
+ Some ( ( _, last_child_id) ) => Some ( ( new_child_id, last_child_id) ) ,
556
+ None => Some ( ( new_child_id, new_child_id) ) ,
557
+ } ;
548
558
}
549
559
550
560
unsafe { self . tree . get_unchecked_mut ( new_child_id) }
@@ -557,6 +567,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
557
567
/// - Panics if `new_sibling_id` is not valid.
558
568
/// - Panics if this node is an orphan.
559
569
pub fn insert_id_before ( & mut self , new_sibling_id : NodeId ) -> NodeMut < T > {
570
+ assert_ne ! (
571
+ self . id( ) ,
572
+ new_sibling_id,
573
+ "Cannot insert node as a sibling of itself"
574
+ ) ;
575
+
560
576
let parent_id = self . node ( ) . parent . unwrap ( ) ;
561
577
let prev_sibling_id = self . node ( ) . prev_sibling ;
562
578
@@ -594,6 +610,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
594
610
/// - Panics if `new_sibling_id` is not valid.
595
611
/// - Panics if this node is an orphan.
596
612
pub fn insert_id_after ( & mut self , new_sibling_id : NodeId ) -> NodeMut < T > {
613
+ assert_ne ! (
614
+ self . id( ) ,
615
+ new_sibling_id,
616
+ "Cannot insert node as a sibling of itself"
617
+ ) ;
618
+
597
619
let parent_id = self . node ( ) . parent . unwrap ( ) ;
598
620
let next_sibling_id = self . node ( ) . next_sibling ;
599
621
@@ -630,6 +652,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
630
652
///
631
653
/// Panics if `from_id` is not valid.
632
654
pub fn reparent_from_id_append ( & mut self , from_id : NodeId ) {
655
+ assert_ne ! (
656
+ self . id( ) ,
657
+ from_id,
658
+ "Cannot reparent node's children to itself"
659
+ ) ;
660
+
633
661
let new_child_ids = {
634
662
let mut from = self . tree . get_mut ( from_id) . unwrap ( ) ;
635
663
match from. node ( ) . children . take ( ) {
@@ -663,6 +691,12 @@ impl<'a, T: 'a> NodeMut<'a, T> {
663
691
///
664
692
/// Panics if `from_id` is not valid.
665
693
pub fn reparent_from_id_prepend ( & mut self , from_id : NodeId ) {
694
+ assert_ne ! (
695
+ self . id( ) ,
696
+ from_id,
697
+ "Cannot reparent node's children to itself"
698
+ ) ;
699
+
666
700
let new_child_ids = {
667
701
let mut from = self . tree . get_mut ( from_id) . unwrap ( ) ;
668
702
match from. node ( ) . children . take ( ) {
0 commit comments