Skip to content

Commit

Permalink
Add remove_current_as_list to LinkedList's CursorMut
Browse files Browse the repository at this point in the history
The `remove_current` method only returns the inner `T` and deallocates the list node. This is unnecessary for move operations, where the element is going to be linked back into this (or even a different) `LinkedList`. The `remove_current_as_list` method avoids this by returning the unlinked list node as a new single-element `LinkedList` structure .
  • Loading branch information
main-- authored May 4, 2020
1 parent 6318d24 commit 1593e2b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/liballoc/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,31 @@ impl<'a, T> CursorMut<'a, T> {
}
}

/// Removes the current element from the `LinkedList` without deallocating the list node.
///
/// The node that was removed is returned as a new `LinkedList` containing only this node.
/// The cursor is moved to point to the next element in the current `LinkedList`.
///
/// If the cursor is currently pointing to the "ghost" non-element then no element
/// is removed and `None` is returned.
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn remove_current_as_list(&mut self) -> Option<LinkedList<T>> {
let unlinked_node = self.current?;
unsafe {
self.current = unlinked_node.as_ref().next;
self.list.unlink_node(unlinked_node);

unlinked_node.as_mut().prev = None;
unlinked_node.as_mut().next = None;
Some(LinkedList {
head: Some(unlinked_node),
tail: Some(unlinked_node),
len: 1,
marker: PhantomData,
})
}
}

/// Inserts the elements from the given `LinkedList` after the current one.
///
/// If the cursor is pointing at the "ghost" non-element then the new elements are
Expand Down

0 comments on commit 1593e2b

Please sign in to comment.