@@ -369,6 +369,11 @@ impl<'a, T: 'a> NodeMut<'a, T> {
369
369
& mut self . node ( ) . value
370
370
}
371
371
372
+ /// Downcast `NodeMut` to `NodeRef`.
373
+ pub fn as_ref ( & mut self ) -> NodeRef < ' _ , T > {
374
+ unsafe { self . tree . get_unchecked ( self . id ) }
375
+ }
376
+
372
377
fn axis < F > ( & mut self , f : F ) -> Option < NodeMut < T > >
373
378
where
374
379
F : FnOnce ( & mut Node < T > ) -> Option < NodeId > ,
@@ -463,6 +468,115 @@ impl<'a, T: 'a> NodeMut<'a, T> {
463
468
unsafe { self . tree . get_unchecked ( self . id ) . has_children ( ) }
464
469
}
465
470
471
+ /// Apply function for each ancestor mutable node reference.
472
+ pub fn for_each_ancestor < F > ( & mut self , mut f : F )
473
+ where
474
+ F : FnMut ( & mut NodeMut < T > ) ,
475
+ {
476
+ let mut current = self . parent ( ) ;
477
+ while let Some ( mut node) = current {
478
+ f ( & mut node) ;
479
+ current = node. into_parent ( ) . ok ( ) ;
480
+ }
481
+ }
482
+
483
+ /// Apply function for each next sibling mutable node reference.
484
+ pub fn for_each_next_sibling < F > ( & mut self , mut f : F )
485
+ where
486
+ F : FnMut ( & mut NodeMut < T > ) ,
487
+ {
488
+ let mut current = self . next_sibling ( ) ;
489
+ while let Some ( mut node) = current {
490
+ f ( & mut node) ;
491
+ current = node. into_next_sibling ( ) . ok ( ) ;
492
+ }
493
+ }
494
+
495
+ /// Apply function for each previout sibling mutable node reference.
496
+ pub fn for_each_prev_sibling < F > ( & mut self , mut f : F )
497
+ where
498
+ F : FnMut ( & mut NodeMut < T > ) ,
499
+ {
500
+ let mut current = self . prev_sibling ( ) ;
501
+ while let Some ( mut node) = current {
502
+ f ( & mut node) ;
503
+ current = node. into_prev_sibling ( ) . ok ( ) ;
504
+ }
505
+ }
506
+
507
+ /// Apply function for this node and each sibling mutable node reference.
508
+ pub fn for_each_sibling < F > ( & mut self , mut f : F )
509
+ where
510
+ F : FnMut ( & mut NodeMut < T > ) ,
511
+ {
512
+ self . for_each_prev_sibling ( & mut f) ;
513
+ f ( self ) ;
514
+ self . for_each_next_sibling ( & mut f) ;
515
+ }
516
+
517
+ /// Apply function for each children mutable node reference.
518
+ pub fn for_each_child < F > ( & mut self , f : F )
519
+ where
520
+ F : FnMut ( & mut NodeMut < T > ) ,
521
+ {
522
+ if let Some ( mut first_child) = self . first_child ( ) {
523
+ first_child. for_each_sibling ( f) ;
524
+ }
525
+ }
526
+
527
+ /// Apply function for this node and each descendant mutable node reference.
528
+ pub fn for_each_descendant < F > ( & mut self , mut f : F )
529
+ where
530
+ F : FnMut ( & mut NodeMut < T > ) ,
531
+ {
532
+ let id = self . id ( ) ;
533
+
534
+ f ( self ) ;
535
+
536
+ // Start at our first child, if any.
537
+ let Some ( mut node) = self . first_child ( ) else {
538
+ return ;
539
+ } ;
540
+
541
+ loop {
542
+ f ( & mut node) ;
543
+
544
+ // Try to go deeper into its first child.
545
+ match node. into_first_child ( ) {
546
+ Ok ( child) => {
547
+ node = child;
548
+ continue ;
549
+ }
550
+ Err ( n) => {
551
+ node = n;
552
+ }
553
+ }
554
+
555
+ // No deeper child, so climb until we find a next sibling or hit self.
556
+ loop {
557
+ match node. into_next_sibling ( ) {
558
+ Ok ( sib) => {
559
+ node = sib;
560
+ break ;
561
+ }
562
+ Err ( n) => {
563
+ node = n;
564
+ }
565
+ }
566
+
567
+ // No sibling, so climb up.
568
+ if let Ok ( parent) = node. into_parent ( ) {
569
+ if parent. id ( ) == id {
570
+ return ;
571
+ }
572
+ node = parent;
573
+ } else {
574
+ unreachable ! ( ) ;
575
+ }
576
+ }
577
+ }
578
+ }
579
+
466
580
/// Appends a new child to this node.
467
581
pub fn append ( & mut self , value : T ) -> NodeMut < T > {
468
582
let id = self . tree . orphan ( value) . id ;
0 commit comments