@@ -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,88 @@ 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 ) -> Result < Self , Self >
322
+ where
323
+ F : FnOnce ( & mut Node < T > ) -> Option < NodeId > ,
324
+ {
325
+ let id = f ( self . node ( ) ) ;
326
+ match id {
327
+ Some ( id) => Ok ( unsafe { self . tree . get_unchecked_mut ( id) } ) ,
328
+ None => Err ( self ) ,
329
+ }
330
+ }
331
+
316
332
/// Returns the parent of this node.
317
333
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) } )
334
+ self . axis ( |node| node. parent )
335
+ }
336
+
337
+ /// Returns the parent of this node.
338
+ ///
339
+ /// Returns `Ok(parent)` if possible and `Err(self)` otherwise
340
+ /// so the caller can recover the current position.
341
+ pub fn into_parent ( self ) -> Result < Self , Self > {
342
+ self . into_axis ( |node| node. parent )
320
343
}
321
344
322
345
/// Returns the previous sibling of this node.
323
346
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) } )
347
+ self . axis ( |node| node. prev_sibling )
348
+ }
349
+
350
+ /// Returns the previous sibling of this node.
351
+ ///
352
+ /// Returns `Ok(prev_sibling)` if possible and `Err(self)` otherwise
353
+ /// so the caller can recover the current position.
354
+ pub fn into_prev_sibling ( self ) -> Result < Self , Self > {
355
+ self . into_axis ( |node| node. prev_sibling )
326
356
}
327
357
328
358
/// Returns the next sibling of this node.
329
359
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) } )
360
+ self . axis ( |node| node. next_sibling )
361
+ }
362
+
363
+ /// Returns the next sibling of this node.
364
+ ///
365
+ /// Returns `Ok(next_sibling)` if possible and `Err(self)` otherwise
366
+ /// so the caller can recover the current position.
367
+ pub fn into_next_sibling ( self ) -> Result < Self , Self > {
368
+ self . into_axis ( |node| node. next_sibling )
332
369
}
333
370
334
371
/// Returns the first child of this node.
335
372
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) } )
373
+ self . axis ( |node| node. children . map ( |( id, _) | id) )
374
+ }
375
+
376
+ /// Returns the first child of this node.
377
+ ///
378
+ /// Returns `Ok(first_child)` if possible and `Err(self)` otherwise
379
+ /// so the caller can recover the current position.
380
+ pub fn into_first_child ( self ) -> Result < Self , Self > {
381
+ self . into_axis ( |node| node. children . map ( |( id, _) | id) )
338
382
}
339
383
340
384
/// Returns the last child of this node.
341
385
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) } )
386
+ self . axis ( |node| node. children . map ( |( _, id) | id) )
387
+ }
388
+
389
+ /// Returns the last child of this node.
390
+ ///
391
+ /// Returns `Ok(last_child)` if possible and `Err(self)` otherwise
392
+ /// so the caller can recover the current position.
393
+ pub fn into_last_child ( self ) -> Result < Self , Self > {
394
+ self . into_axis ( |node| node. children . map ( |( _, id) | id) )
344
395
}
345
396
346
397
/// Returns true if this node has siblings.
0 commit comments