@@ -369,6 +369,11 @@ impl<'a, T: 'a> NodeMut<'a, T> {
369369 & mut self . node ( ) . value
370370 }
371371
372+ /// Downcast `NodeMut` to `NodeRef`.
373+ pub fn as_ref ( & mut self ) -> NodeRef < ' _ , T > {
374+ unsafe { self . tree . get_unchecked ( self . id ) }
375+ }
376+
372377 fn axis < F > ( & mut self , f : F ) -> Option < NodeMut < T > >
373378 where
374379 F : FnOnce ( & mut Node < T > ) -> Option < NodeId > ,
@@ -463,6 +468,116 @@ impl<'a, T: 'a> NodeMut<'a, T> {
463468 unsafe { self . tree . get_unchecked ( self . id ) . has_children ( ) }
464469 }
465470
471+ /// Apply function for each ancestor mutable node reference.
472+ pub fn for_each_ancestor < ' b , F > ( & ' b mut self , mut f : F )
473+ where
474+ F : FnMut ( & mut NodeMut < ' b , 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 < ' b , F > ( & ' b mut self , mut f : F )
485+ where
486+ F : FnMut ( & mut NodeMut < ' b , 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 < ' b , F > ( & ' b mut self , mut f : F )
497+ where
498+ F : FnMut ( & mut NodeMut < ' b , 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 : for < ' b > FnMut ( & mut NodeMut < ' b , 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 , mut f : F )
519+ where
520+ F : for < ' b > FnMut ( & mut NodeMut < ' b , T > ) ,
521+ {
522+ let Some ( mut first_child) = self . first_child ( ) else {
523+ return ;
524+ } ;
525+ f ( & mut first_child) ;
526+ first_child. for_each_next_sibling ( f) ;
527+ }
528+
529+ /// Apply function for this node and each descendant mutable node reference.
530+ pub fn for_each_descendant < F > ( & mut self , mut f : F )
531+ where
532+ F : FnMut ( & mut NodeMut < ' _ , T > ) ,
533+ {
534+ let id = self . id ( ) ;
535+
536+ f ( self ) ;
537+
538+ // Start at our first child, if any.
539+ let Some ( mut node) = self . first_child ( ) else {
540+ return ;
541+ } ;
542+
543+ loop {
544+ f ( & mut node) ;
545+
546+ // Try to go deeper into its first child.
547+ match node. into_first_child ( ) {
548+ Ok ( child) => {
549+ node = child;
550+ continue ;
551+ }
552+ Err ( n) => {
553+ node = n;
554+ }
555+ }
556+
557+ // No deeper child, so climb until we find a next sibling or hit self.
558+ loop {
559+ match node. into_next_sibling ( ) {
560+ Ok ( sib) => {
561+ node = sib;
562+ break ;
563+ }
564+ Err ( n) => {
565+ node = n;
566+ }
567+ }
568+
569+ // No sibling, so climb up.
570+ let Ok ( parent) = node. into_parent ( ) else {
571+ unreachable ! ( ) ;
572+ } ;
573+ if parent. id ( ) == id {
574+ return ;
575+ }
576+ node = parent;
577+ }
578+ }
579+ }
580+
466581 /// Appends a new child to this node.
467582 pub fn append ( & mut self , value : T ) -> NodeMut < T > {
468583 let id = self . tree . orphan ( value) . id ;
0 commit comments