@@ -178,7 +178,7 @@ impl<T> Tree<T> {
178
178
}
179
179
180
180
/// Returns a reference to the specified node.
181
- pub fn get ( & self , id : NodeId ) -> Option < NodeRef < T > > {
181
+ pub fn get ( & self , id : NodeId ) -> Option < NodeRef < ' _ , T > > {
182
182
self . vec . get ( id. to_index ( ) ) . map ( |node| NodeRef {
183
183
id,
184
184
node,
@@ -187,7 +187,7 @@ impl<T> Tree<T> {
187
187
}
188
188
189
189
/// Returns a mutator of the specified node.
190
- pub fn get_mut ( & mut self , id : NodeId ) -> Option < NodeMut < T > > {
190
+ pub fn get_mut ( & mut self , id : NodeId ) -> Option < NodeMut < ' _ , T > > {
191
191
let exists = self . vec . get ( id. to_index ( ) ) . map ( |_| ( ) ) ;
192
192
exists. map ( move |_| NodeMut { id, tree : self } )
193
193
}
@@ -203,7 +203,7 @@ impl<T> Tree<T> {
203
203
/// Returns a reference to the specified node.
204
204
/// # Safety
205
205
/// The caller must ensure that `id` is a valid node ID.
206
- pub unsafe fn get_unchecked ( & self , id : NodeId ) -> NodeRef < T > {
206
+ pub unsafe fn get_unchecked ( & self , id : NodeId ) -> NodeRef < ' _ , T > {
207
207
NodeRef {
208
208
id,
209
209
node : self . node ( id) ,
@@ -214,22 +214,22 @@ impl<T> Tree<T> {
214
214
/// Returns a mutator of the specified node.
215
215
/// # Safety
216
216
/// The caller must ensure that `id` is a valid node ID.
217
- pub unsafe fn get_unchecked_mut ( & mut self , id : NodeId ) -> NodeMut < T > {
217
+ pub unsafe fn get_unchecked_mut ( & mut self , id : NodeId ) -> NodeMut < ' _ , T > {
218
218
NodeMut { id, tree : self }
219
219
}
220
220
221
221
/// Returns a reference to the root node.
222
- pub fn root ( & self ) -> NodeRef < T > {
222
+ pub fn root ( & self ) -> NodeRef < ' _ , T > {
223
223
unsafe { self . get_unchecked ( NodeId :: from_index ( 0 ) ) }
224
224
}
225
225
226
226
/// Returns a mutator of the root node.
227
- pub fn root_mut ( & mut self ) -> NodeMut < T > {
227
+ pub fn root_mut ( & mut self ) -> NodeMut < ' _ , T > {
228
228
unsafe { self . get_unchecked_mut ( NodeId :: from_index ( 0 ) ) }
229
229
}
230
230
231
231
/// Creates an orphan node.
232
- pub fn orphan ( & mut self , value : T ) -> NodeMut < T > {
232
+ pub fn orphan ( & mut self , value : T ) -> NodeMut < ' _ , T > {
233
233
let id = unsafe { NodeId :: from_index ( self . vec . len ( ) ) } ;
234
234
self . vec . push ( Node :: new ( value) ) ;
235
235
unsafe { self . get_unchecked_mut ( id) }
@@ -238,7 +238,7 @@ impl<T> Tree<T> {
238
238
/// Merge with another tree as orphan, returning the new root of tree being merged.
239
239
// Allowing this for compactness.
240
240
#[ allow( clippy:: option_map_unit_fn) ]
241
- pub fn extend_tree ( & mut self , mut other_tree : Tree < T > ) -> NodeMut < T > {
241
+ pub fn extend_tree ( & mut self , mut other_tree : Tree < T > ) -> NodeMut < ' _ , T > {
242
242
let offset = self . vec . len ( ) ;
243
243
let offset_id = |id : NodeId | -> NodeId {
244
244
let old_index = id. to_index ( ) ;
@@ -374,7 +374,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
374
374
unsafe { self . tree . get_unchecked ( self . id ) }
375
375
}
376
376
377
- fn axis < F > ( & mut self , f : F ) -> Option < NodeMut < T > >
377
+ fn axis < F > ( & mut self , f : F ) -> Option < NodeMut < ' _ , T > >
378
378
where
379
379
F : FnOnce ( & mut Node < T > ) -> Option < NodeId > ,
380
380
{
@@ -394,7 +394,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
394
394
}
395
395
396
396
/// Returns the parent of this node.
397
- pub fn parent ( & mut self ) -> Option < NodeMut < T > > {
397
+ pub fn parent ( & mut self ) -> Option < NodeMut < ' _ , T > > {
398
398
self . axis ( |node| node. parent )
399
399
}
400
400
@@ -407,7 +407,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
407
407
}
408
408
409
409
/// Returns the previous sibling of this node.
410
- pub fn prev_sibling ( & mut self ) -> Option < NodeMut < T > > {
410
+ pub fn prev_sibling ( & mut self ) -> Option < NodeMut < ' _ , T > > {
411
411
self . axis ( |node| node. prev_sibling )
412
412
}
413
413
@@ -420,7 +420,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
420
420
}
421
421
422
422
/// Returns the next sibling of this node.
423
- pub fn next_sibling ( & mut self ) -> Option < NodeMut < T > > {
423
+ pub fn next_sibling ( & mut self ) -> Option < NodeMut < ' _ , T > > {
424
424
self . axis ( |node| node. next_sibling )
425
425
}
426
426
@@ -433,7 +433,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
433
433
}
434
434
435
435
/// Returns the first child of this node.
436
- pub fn first_child ( & mut self ) -> Option < NodeMut < T > > {
436
+ pub fn first_child ( & mut self ) -> Option < NodeMut < ' _ , T > > {
437
437
self . axis ( |node| node. children . map ( |( id, _) | id) )
438
438
}
439
439
@@ -446,7 +446,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
446
446
}
447
447
448
448
/// Returns the last child of this node.
449
- pub fn last_child ( & mut self ) -> Option < NodeMut < T > > {
449
+ pub fn last_child ( & mut self ) -> Option < NodeMut < ' _ , T > > {
450
450
self . axis ( |node| node. children . map ( |( _, id) | id) )
451
451
}
452
452
@@ -579,25 +579,25 @@ impl<'a, T: 'a> NodeMut<'a, T> {
579
579
}
580
580
581
581
/// Appends a new child to this node.
582
- pub fn append ( & mut self , value : T ) -> NodeMut < T > {
582
+ pub fn append ( & mut self , value : T ) -> NodeMut < ' _ , T > {
583
583
let id = self . tree . orphan ( value) . id ;
584
584
self . append_id ( id)
585
585
}
586
586
587
587
/// Prepends a new child to this node.
588
- pub fn prepend ( & mut self , value : T ) -> NodeMut < T > {
588
+ pub fn prepend ( & mut self , value : T ) -> NodeMut < ' _ , T > {
589
589
let id = self . tree . orphan ( value) . id ;
590
590
self . prepend_id ( id)
591
591
}
592
592
593
593
/// Appends a subtree, return the root of the merged subtree.
594
- pub fn append_subtree ( & mut self , subtree : Tree < T > ) -> NodeMut < T > {
594
+ pub fn append_subtree ( & mut self , subtree : Tree < T > ) -> NodeMut < ' _ , T > {
595
595
let root_id = self . tree . extend_tree ( subtree) . id ;
596
596
self . append_id ( root_id)
597
597
}
598
598
599
599
/// Prepends a subtree, return the root of the merged subtree.
600
- pub fn prepend_subtree ( & mut self , subtree : Tree < T > ) -> NodeMut < T > {
600
+ pub fn prepend_subtree ( & mut self , subtree : Tree < T > ) -> NodeMut < ' _ , T > {
601
601
let root_id = self . tree . extend_tree ( subtree) . id ;
602
602
self . prepend_id ( root_id)
603
603
}
@@ -607,7 +607,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
607
607
/// # Panics
608
608
///
609
609
/// Panics if this node is an orphan.
610
- pub fn insert_before ( & mut self , value : T ) -> NodeMut < T > {
610
+ pub fn insert_before ( & mut self , value : T ) -> NodeMut < ' _ , T > {
611
611
let id = self . tree . orphan ( value) . id ;
612
612
self . insert_id_before ( id)
613
613
}
@@ -617,7 +617,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
617
617
/// # Panics
618
618
///
619
619
/// Panics if this node is an orphan.
620
- pub fn insert_after ( & mut self , value : T ) -> NodeMut < T > {
620
+ pub fn insert_after ( & mut self , value : T ) -> NodeMut < ' _ , T > {
621
621
let id = self . tree . orphan ( value) . id ;
622
622
self . insert_id_after ( id)
623
623
}
@@ -664,7 +664,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
664
664
/// # Panics
665
665
///
666
666
/// Panics if `new_child_id` is not valid.
667
- pub fn append_id ( & mut self , new_child_id : NodeId ) -> NodeMut < T > {
667
+ pub fn append_id ( & mut self , new_child_id : NodeId ) -> NodeMut < ' _ , T > {
668
668
assert_ne ! (
669
669
self . id( ) ,
670
670
new_child_id,
@@ -703,7 +703,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
703
703
/// # Panics
704
704
///
705
705
/// Panics if `new_child_id` is not valid.
706
- pub fn prepend_id ( & mut self , new_child_id : NodeId ) -> NodeMut < T > {
706
+ pub fn prepend_id ( & mut self , new_child_id : NodeId ) -> NodeMut < ' _ , T > {
707
707
assert_ne ! (
708
708
self . id( ) ,
709
709
new_child_id,
@@ -743,7 +743,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
743
743
///
744
744
/// - Panics if `new_sibling_id` is not valid.
745
745
/// - Panics if this node is an orphan.
746
- pub fn insert_id_before ( & mut self , new_sibling_id : NodeId ) -> NodeMut < T > {
746
+ pub fn insert_id_before ( & mut self , new_sibling_id : NodeId ) -> NodeMut < ' _ , T > {
747
747
assert_ne ! (
748
748
self . id( ) ,
749
749
new_sibling_id,
@@ -786,7 +786,7 @@ impl<'a, T: 'a> NodeMut<'a, T> {
786
786
///
787
787
/// - Panics if `new_sibling_id` is not valid.
788
788
/// - Panics if this node is an orphan.
789
- pub fn insert_id_after ( & mut self , new_sibling_id : NodeId ) -> NodeMut < T > {
789
+ pub fn insert_id_after ( & mut self , new_sibling_id : NodeId ) -> NodeMut < ' _ , T > {
790
790
assert_ne ! (
791
791
self . id( ) ,
792
792
new_sibling_id,
@@ -843,9 +843,14 @@ impl<'a, T: 'a> NodeMut<'a, T> {
843
843
}
844
844
} ;
845
845
846
- unsafe {
847
- self . tree . node_mut ( new_child_ids. 0 ) . parent = Some ( self . id ) ;
848
- self . tree . node_mut ( new_child_ids. 1 ) . parent = Some ( self . id ) ;
846
+ let mut child_id = new_child_ids. 0 ;
847
+ loop {
848
+ let child = unsafe { self . tree . node_mut ( child_id) } ;
849
+ child. parent = Some ( self . id ) ;
850
+ child_id = match child. next_sibling {
851
+ Some ( id) => id,
852
+ None => break ,
853
+ } ;
849
854
}
850
855
851
856
if self . node ( ) . children . is_none ( ) {
@@ -882,9 +887,14 @@ impl<'a, T: 'a> NodeMut<'a, T> {
882
887
}
883
888
} ;
884
889
885
- unsafe {
886
- self . tree . node_mut ( new_child_ids. 0 ) . parent = Some ( self . id ) ;
887
- self . tree . node_mut ( new_child_ids. 1 ) . parent = Some ( self . id ) ;
890
+ let mut child_id = new_child_ids. 0 ;
891
+ loop {
892
+ let child = unsafe { self . tree . node_mut ( child_id) } ;
893
+ child. parent = Some ( self . id ) ;
894
+ child_id = match child. next_sibling {
895
+ Some ( id) => id,
896
+ None => break ,
897
+ } ;
888
898
}
889
899
890
900
if self . node ( ) . children . is_none ( ) {
0 commit comments