Skip to content

Commit 118d629

Browse files
committed
Extend consuming movement methods to yield current node reference if navigation is not possible.
1 parent 1651757 commit 118d629

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/lib.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,15 @@ impl<'a, T: 'a> NodeMut<'a, T> {
318318
id.map(move |id| unsafe { self.tree.get_unchecked_mut(id) })
319319
}
320320

321-
fn into_axis<F>(mut self, f: F) -> Option<Self>
321+
fn into_axis<F>(mut self, f: F) -> Result<Self, Self>
322322
where
323323
F: FnOnce(&mut Node<T>) -> Option<NodeId>,
324324
{
325325
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+
}
327330
}
328331

329332
/// Returns the parent of this node.
@@ -332,7 +335,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
332335
}
333336

334337
/// 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> {
336342
self.into_axis(|node| node.parent)
337343
}
338344

@@ -342,7 +348,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
342348
}
343349

344350
/// 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> {
346355
self.into_axis(|node| node.prev_sibling)
347356
}
348357

@@ -352,7 +361,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
352361
}
353362

354363
/// 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> {
356368
self.into_axis(|node| node.next_sibling)
357369
}
358370

@@ -362,7 +374,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
362374
}
363375

364376
/// 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> {
366381
self.into_axis(|node| node.children.map(|(id, _)| id))
367382
}
368383

@@ -372,7 +387,10 @@ impl<'a, T: 'a> NodeMut<'a, T> {
372387
}
373388

374389
/// 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> {
376394
self.into_axis(|node| node.children.map(|(_, id)| id))
377395
}
378396

0 commit comments

Comments
 (0)