Skip to content

Commit faccb0f

Browse files
authored
Rollup merge of rust-lang#71878 - main--:patch-2, r=Amanieu
Add remove_current_as_list to LinkedList's CursorMut 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. (per rust-lang#58533 (comment))
2 parents db7b381 + c5cdf7f commit faccb0f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/liballoc/collections/linked_list.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,31 @@ impl<'a, T> CursorMut<'a, T> {
14961496
}
14971497
}
14981498

1499+
/// Removes the current element from the `LinkedList` without deallocating the list node.
1500+
///
1501+
/// The node that was removed is returned as a new `LinkedList` containing only this node.
1502+
/// The cursor is moved to point to the next element in the current `LinkedList`.
1503+
///
1504+
/// If the cursor is currently pointing to the "ghost" non-element then no element
1505+
/// is removed and `None` is returned.
1506+
#[unstable(feature = "linked_list_cursors", issue = "58533")]
1507+
pub fn remove_current_as_list(&mut self) -> Option<LinkedList<T>> {
1508+
let mut unlinked_node = self.current?;
1509+
unsafe {
1510+
self.current = unlinked_node.as_ref().next;
1511+
self.list.unlink_node(unlinked_node);
1512+
1513+
unlinked_node.as_mut().prev = None;
1514+
unlinked_node.as_mut().next = None;
1515+
Some(LinkedList {
1516+
head: Some(unlinked_node),
1517+
tail: Some(unlinked_node),
1518+
len: 1,
1519+
marker: PhantomData,
1520+
})
1521+
}
1522+
}
1523+
14991524
/// Inserts the elements from the given `LinkedList` after the current one.
15001525
///
15011526
/// If the cursor is pointing at the "ghost" non-element then the new elements are

0 commit comments

Comments
 (0)