diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2020-05-05 01:49:43 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-05 01:49:43 +0200 |
| commit | faccb0f07a50f1b19644938723f6cd2c6c5877c8 (patch) | |
| tree | d9ccb2cda8e921c5551d5bc4b55c057927dc2dbd /src/liballoc | |
| parent | db7b38181c9f0b0347453780eb61c66d00bc95b9 (diff) | |
| parent | c5cdf7fe920c8cb3b60f5a6e257f6cdfb51102d3 (diff) | |
| download | rust-faccb0f07a50f1b19644938723f6cd2c6c5877c8.tar.gz rust-faccb0f07a50f1b19644938723f6cd2c6c5877c8.zip | |
Rollup merge of #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 https://github.com/rust-lang/rust/issues/58533#issuecomment-623010157)
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/collections/linked_list.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/liballoc/collections/linked_list.rs b/src/liballoc/collections/linked_list.rs index bfa4045787f..cc0f07b8227 100644 --- a/src/liballoc/collections/linked_list.rs +++ b/src/liballoc/collections/linked_list.rs @@ -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 mut 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 |
