@@ -318,12 +318,15 @@ impl<'a, T: 'a> NodeMut<'a, T> {
318
318
id. map ( move |id| unsafe { self . tree . get_unchecked_mut ( id) } )
319
319
}
320
320
321
- fn into_axis < F > ( mut self , f : F ) -> Option < Self >
321
+ fn into_axis < F > ( mut self , f : F ) -> Result < Self , Self >
322
322
where
323
323
F : FnOnce ( & mut Node < T > ) -> Option < NodeId > ,
324
324
{
325
325
let id = f ( self . node ( ) ) ;
326
- id. map ( move |id| unsafe { self . tree . get_unchecked_mut ( id) } )
326
+ match id {
327
+ Some ( id) => Ok ( unsafe { self . tree . get_unchecked_mut ( id) } ) ,
328
+ None => Err ( self ) ,
329
+ }
327
330
}
328
331
329
332
/// Returns the parent of this node.
@@ -332,7 +335,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
332
335
}
333
336
334
337
/// Returns the parent of this node.
335
- pub fn into_parent ( self ) -> Option < Self > {
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 > {
336
342
self . into_axis ( |node| node. parent )
337
343
}
338
344
@@ -342,7 +348,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
342
348
}
343
349
344
350
/// Returns the previous sibling of this node.
345
- pub fn into_prev_sibling ( self ) -> Option < Self > {
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 > {
346
355
self . into_axis ( |node| node. prev_sibling )
347
356
}
348
357
@@ -352,7 +361,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
352
361
}
353
362
354
363
/// Returns the next sibling of this node.
355
- pub fn into_next_sibling ( self ) -> Option < Self > {
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 > {
356
368
self . into_axis ( |node| node. next_sibling )
357
369
}
358
370
@@ -362,7 +374,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
362
374
}
363
375
364
376
/// Returns the first child of this node.
365
- pub fn into_first_child ( self ) -> Option < Self > {
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 > {
366
381
self . into_axis ( |node| node. children . map ( |( id, _) | id) )
367
382
}
368
383
@@ -372,7 +387,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
372
387
}
373
388
374
389
/// Returns the last child of this node.
375
- pub fn into_last_child ( self ) -> Option < Self > {
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 > {
376
394
self . into_axis ( |node| node. children . map ( |( _, id) | id) )
377
395
}
378
396
0 commit comments