@@ -247,39 +247,36 @@ impl<'a, T: 'a> NodeRef<'a, T> {
247
247
& self . node . value
248
248
}
249
249
250
+ fn axis < F > ( & self , f : F ) -> Option < Self >
251
+ where
252
+ F : FnOnce ( & Node < T > ) -> Option < NodeId > ,
253
+ {
254
+ f ( self . node ) . map ( |id| unsafe { self . tree . get_unchecked ( id) } )
255
+ }
256
+
250
257
/// Returns the parent of this node.
251
258
pub fn parent ( & self ) -> Option < Self > {
252
- self . node
253
- . parent
254
- . map ( |id| unsafe { self . tree . get_unchecked ( id) } )
259
+ self . axis ( |node| node. parent )
255
260
}
256
261
257
262
/// Returns the previous sibling of this node.
258
263
pub fn prev_sibling ( & self ) -> Option < Self > {
259
- self . node
260
- . prev_sibling
261
- . map ( |id| unsafe { self . tree . get_unchecked ( id) } )
264
+ self . axis ( |node| node. prev_sibling )
262
265
}
263
266
264
267
/// Returns the next sibling of this node.
265
268
pub fn next_sibling ( & self ) -> Option < Self > {
266
- self . node
267
- . next_sibling
268
- . map ( |id| unsafe { self . tree . get_unchecked ( id) } )
269
+ self . axis ( |node| node. next_sibling )
269
270
}
270
271
271
272
/// Returns the first child of this node.
272
273
pub fn first_child ( & self ) -> Option < Self > {
273
- self . node
274
- . children
275
- . map ( |( id, _) | unsafe { self . tree . get_unchecked ( id) } )
274
+ self . axis ( |node| node. children . map ( |( id, _) | id) )
276
275
}
277
276
278
277
/// Returns the last child of this node.
279
278
pub fn last_child ( & self ) -> Option < Self > {
280
- self . node
281
- . children
282
- . map ( |( _, id) | unsafe { self . tree . get_unchecked ( id) } )
279
+ self . axis ( |node| node. children . map ( |( _, id) | id) )
283
280
}
284
281
285
282
/// Returns true if this node has siblings.
@@ -313,34 +310,70 @@ impl<'a, T: 'a> NodeMut<'a, T> {
313
310
& mut self . node ( ) . value
314
311
}
315
312
313
+ fn axis < F > ( & mut self , f : F ) -> Option < NodeMut < T > >
314
+ where
315
+ F : FnOnce ( & mut Node < T > ) -> Option < NodeId > ,
316
+ {
317
+ let id = f ( self . node ( ) ) ;
318
+ id. map ( move |id| unsafe { self . tree . get_unchecked_mut ( id) } )
319
+ }
320
+
321
+ fn into_axis < F > ( mut self , f : F ) -> Option < Self >
322
+ where
323
+ F : FnOnce ( & mut Node < T > ) -> Option < NodeId > ,
324
+ {
325
+ let id = f ( self . node ( ) ) ;
326
+ id. map ( move |id| unsafe { self . tree . get_unchecked_mut ( id) } )
327
+ }
328
+
316
329
/// Returns the parent of this node.
317
330
pub fn parent ( & mut self ) -> Option < NodeMut < T > > {
318
- let id = self . node ( ) . parent ;
319
- id. map ( move |id| unsafe { self . tree . get_unchecked_mut ( id) } )
331
+ self . axis ( |node| node. parent )
332
+ }
333
+
334
+ /// Returns the parent of this node.
335
+ pub fn into_parent ( self ) -> Option < Self > {
336
+ self . into_axis ( |node| node. parent )
320
337
}
321
338
322
339
/// Returns the previous sibling of this node.
323
340
pub fn prev_sibling ( & mut self ) -> Option < NodeMut < T > > {
324
- let id = self . node ( ) . prev_sibling ;
325
- id. map ( move |id| unsafe { self . tree . get_unchecked_mut ( id) } )
341
+ self . axis ( |node| node. prev_sibling )
342
+ }
343
+
344
+ /// Returns the previous sibling of this node.
345
+ pub fn into_prev_sibling ( self ) -> Option < Self > {
346
+ self . into_axis ( |node| node. prev_sibling )
326
347
}
327
348
328
349
/// Returns the next sibling of this node.
329
350
pub fn next_sibling ( & mut self ) -> Option < NodeMut < T > > {
330
- let id = self . node ( ) . next_sibling ;
331
- id. map ( move |id| unsafe { self . tree . get_unchecked_mut ( id) } )
351
+ self . axis ( |node| node. next_sibling )
352
+ }
353
+
354
+ /// Returns the next sibling of this node.
355
+ pub fn into_next_sibling ( self ) -> Option < Self > {
356
+ self . into_axis ( |node| node. next_sibling )
332
357
}
333
358
334
359
/// Returns the first child of this node.
335
360
pub fn first_child ( & mut self ) -> Option < NodeMut < T > > {
336
- let ids = self . node ( ) . children ;
337
- ids. map ( move |( id, _) | unsafe { self . tree . get_unchecked_mut ( id) } )
361
+ self . axis ( |node| node. children . map ( |( id, _) | id) )
362
+ }
363
+
364
+ /// Returns the first child of this node.
365
+ pub fn into_first_child ( self ) -> Option < Self > {
366
+ self . into_axis ( |node| node. children . map ( |( id, _) | id) )
338
367
}
339
368
340
369
/// Returns the last child of this node.
341
370
pub fn last_child ( & mut self ) -> Option < NodeMut < T > > {
342
- let ids = self . node ( ) . children ;
343
- ids. map ( move |( _, id) | unsafe { self . tree . get_unchecked_mut ( id) } )
371
+ self . axis ( |node| node. children . map ( |( _, id) | id) )
372
+ }
373
+
374
+ /// Returns the last child of this node.
375
+ pub fn into_last_child ( self ) -> Option < Self > {
376
+ self . into_axis ( |node| node. children . map ( |( _, id) | id) )
344
377
}
345
378
346
379
/// Returns true if this node has siblings.
0 commit comments