Skip to content

Commit 596e1e3

Browse files
Rollup merge of #150344 - hkBst:cleanup-linked-list, r=jhpratt
Cleanup linked list - Replaces some checked_sub().unwrap_or(0) with saturating_sub(). - Replaces NonNull::from(Box::leak(node)) with Box::into_non_null(node)
2 parents be715d4 + eea3d79 commit 596e1e3

3 files changed

Lines changed: 22 additions & 18 deletions

File tree

library/alloc/src/collections/linked_list.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl<T, A: Allocator> LinkedList<T, A> {
167167
/// Adds the given node to the front of the list.
168168
///
169169
/// # Safety
170-
/// `node` must point to a valid node that was boxed and leaked using the list's allocator.
170+
/// `node` must point to a valid node in the list's allocator.
171171
/// This method takes ownership of the node, so the pointer should not be used again.
172172
#[inline]
173173
unsafe fn push_front_node(&mut self, node: NonNull<Node<T>>) {
@@ -212,7 +212,7 @@ impl<T, A: Allocator> LinkedList<T, A> {
212212
/// Adds the given node to the back of the list.
213213
///
214214
/// # Safety
215-
/// `node` must point to a valid node that was boxed and leaked using the list's allocator.
215+
/// `node` must point to a valid node in the list's allocator.
216216
/// This method takes ownership of the node, so the pointer should not be used again.
217217
#[inline]
218218
unsafe fn push_back_node(&mut self, node: NonNull<Node<T>>) {
@@ -597,7 +597,7 @@ impl<T, A: Allocator> LinkedList<T, A> {
597597
#[must_use]
598598
#[unstable(feature = "linked_list_cursors", issue = "58533")]
599599
pub fn cursor_back(&self) -> Cursor<'_, T, A> {
600-
Cursor { index: self.len.checked_sub(1).unwrap_or(0), current: self.tail, list: self }
600+
Cursor { index: self.len.saturating_sub(1), current: self.tail, list: self }
601601
}
602602

603603
/// Provides a cursor with editing operations at the back element.
@@ -607,7 +607,7 @@ impl<T, A: Allocator> LinkedList<T, A> {
607607
#[must_use]
608608
#[unstable(feature = "linked_list_cursors", issue = "58533")]
609609
pub fn cursor_back_mut(&mut self) -> CursorMut<'_, T, A> {
610-
CursorMut { index: self.len.checked_sub(1).unwrap_or(0), current: self.tail, list: self }
610+
CursorMut { index: self.len.saturating_sub(1), current: self.tail, list: self }
611611
}
612612

613613
/// Returns `true` if the `LinkedList` is empty.
@@ -866,12 +866,12 @@ impl<T, A: Allocator> LinkedList<T, A> {
866866
#[unstable(feature = "push_mut", issue = "135974")]
867867
#[must_use = "if you don't need a reference to the value, use `LinkedList::push_front` instead"]
868868
pub fn push_front_mut(&mut self, elt: T) -> &mut T {
869-
let node = Box::new_in(Node::new(elt), &self.alloc);
870-
let mut node_ptr = NonNull::from(Box::leak(node));
871-
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
869+
let mut node =
870+
Box::into_non_null_with_allocator(Box::new_in(Node::new(elt), &self.alloc)).0;
871+
// SAFETY: node is a unique pointer to a node in self.alloc
872872
unsafe {
873-
self.push_front_node(node_ptr);
874-
&mut node_ptr.as_mut().element
873+
self.push_front_node(node);
874+
&mut node.as_mut().element
875875
}
876876
}
877877

@@ -938,12 +938,12 @@ impl<T, A: Allocator> LinkedList<T, A> {
938938
#[unstable(feature = "push_mut", issue = "135974")]
939939
#[must_use = "if you don't need a reference to the value, use `LinkedList::push_back` instead"]
940940
pub fn push_back_mut(&mut self, elt: T) -> &mut T {
941-
let node = Box::new_in(Node::new(elt), &self.alloc);
942-
let mut node_ptr = NonNull::from(Box::leak(node));
943-
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
941+
let mut node =
942+
Box::into_non_null_with_allocator(Box::new_in(Node::new(elt), &self.alloc)).0;
943+
// SAFETY: node is a unique pointer to a node in self.alloc
944944
unsafe {
945-
self.push_back_node(node_ptr);
946-
&mut node_ptr.as_mut().element
945+
self.push_back_node(node);
946+
&mut node.as_mut().element
947947
}
948948
}
949949

@@ -1432,7 +1432,7 @@ impl<'a, T, A: Allocator> Cursor<'a, T, A> {
14321432
// No current. We're at the start of the list. Yield None and jump to the end.
14331433
None => {
14341434
self.current = self.list.tail;
1435-
self.index = self.list.len().checked_sub(1).unwrap_or(0);
1435+
self.index = self.list.len().saturating_sub(1);
14361436
}
14371437
// Have a prev. Yield it and go to the previous element.
14381438
Some(current) => unsafe {
@@ -1559,7 +1559,7 @@ impl<'a, T, A: Allocator> CursorMut<'a, T, A> {
15591559
// No current. We're at the start of the list. Yield None and jump to the end.
15601560
None => {
15611561
self.current = self.list.tail;
1562-
self.index = self.list.len().checked_sub(1).unwrap_or(0);
1562+
self.index = self.list.len().saturating_sub(1);
15631563
}
15641564
// Have a prev. Yield it and go to the previous element.
15651565
Some(current) => unsafe {
@@ -1690,7 +1690,8 @@ impl<'a, T, A: Allocator> CursorMut<'a, T, A> {
16901690
#[unstable(feature = "linked_list_cursors", issue = "58533")]
16911691
pub fn insert_after(&mut self, item: T) {
16921692
unsafe {
1693-
let spliced_node = Box::leak(Box::new_in(Node::new(item), &self.list.alloc)).into();
1693+
let spliced_node =
1694+
Box::into_non_null_with_allocator(Box::new_in(Node::new(item), &self.list.alloc)).0;
16941695
let node_next = match self.current {
16951696
None => self.list.head,
16961697
Some(node) => node.as_ref().next,
@@ -1710,7 +1711,8 @@ impl<'a, T, A: Allocator> CursorMut<'a, T, A> {
17101711
#[unstable(feature = "linked_list_cursors", issue = "58533")]
17111712
pub fn insert_before(&mut self, item: T) {
17121713
unsafe {
1713-
let spliced_node = Box::leak(Box::new_in(Node::new(item), &self.list.alloc)).into();
1714+
let spliced_node =
1715+
Box::into_non_null_with_allocator(Box::new_in(Node::new(item), &self.list.alloc)).0;
17141716
let node_prev = match self.current {
17151717
None => self.list.tail,
17161718
Some(node) => node.as_ref().prev,

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#![feature(assert_matches)]
9494
#![feature(async_fn_traits)]
9595
#![feature(async_iterator)]
96+
#![feature(box_vec_non_null)]
9697
#![feature(bstr)]
9798
#![feature(bstr_internals)]
9899
#![feature(cast_maybe_uninit)]

library/alloctests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#![feature(allocator_api)]
1919
#![feature(array_into_iter_constructors)]
2020
#![feature(assert_matches)]
21+
#![feature(box_vec_non_null)]
2122
#![feature(char_internals)]
2223
#![feature(copied_into_inner)]
2324
#![feature(core_intrinsics)]

0 commit comments

Comments
 (0)