Skip to content

Commit 3791f6d

Browse files
Rollup merge of rust-lang#114257 - rytheo:linked-list-avoid-unique, r=cuviper
Avoid using `ptr::Unique` in `LinkedList` code Addresses a [comment](rust-lang#103093 (comment)) by `@RalfJung` about avoiding use of `core::ptr::Unique` in the standard library.
2 parents 0c241e6 + 80277dd commit 3791f6d

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

library/alloc/src/collections/linked_list.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use core::hash::{Hash, Hasher};
1818
use core::iter::FusedIterator;
1919
use core::marker::PhantomData;
2020
use core::mem;
21-
use core::ptr::{NonNull, Unique};
21+
use core::ptr::NonNull;
2222

2323
use super::SpecExtend;
2424
use crate::alloc::{Allocator, Global};
@@ -168,15 +168,16 @@ impl<T, A: Allocator> LinkedList<T, A> {
168168
/// Adds the given node to the front of the list.
169169
///
170170
/// # Safety
171-
/// `node` must point to a valid node that was boxed using the list's allocator.
171+
/// `node` must point to a valid node that was boxed and leaked using the list's allocator.
172+
/// This method takes ownership of the node, so the pointer should not be used again.
172173
#[inline]
173-
unsafe fn push_front_node(&mut self, node: Unique<Node<T>>) {
174+
unsafe fn push_front_node(&mut self, node: NonNull<Node<T>>) {
174175
// This method takes care not to create mutable references to whole nodes,
175176
// to maintain validity of aliasing pointers into `element`.
176177
unsafe {
177178
(*node.as_ptr()).next = self.head;
178179
(*node.as_ptr()).prev = None;
179-
let node = Some(NonNull::from(node));
180+
let node = Some(node);
180181

181182
match self.head {
182183
None => self.tail = node,
@@ -212,15 +213,16 @@ impl<T, A: Allocator> LinkedList<T, A> {
212213
/// Adds the given node to the back of the list.
213214
///
214215
/// # Safety
215-
/// `node` must point to a valid node that was boxed using the list's allocator.
216+
/// `node` must point to a valid node that was boxed and leaked using the list's allocator.
217+
/// This method takes ownership of the node, so the pointer should not be used again.
216218
#[inline]
217-
unsafe fn push_back_node(&mut self, node: Unique<Node<T>>) {
219+
unsafe fn push_back_node(&mut self, node: NonNull<Node<T>>) {
218220
// This method takes care not to create mutable references to whole nodes,
219221
// to maintain validity of aliasing pointers into `element`.
220222
unsafe {
221223
(*node.as_ptr()).next = None;
222224
(*node.as_ptr()).prev = self.tail;
223-
let node = Some(NonNull::from(node));
225+
let node = Some(node);
224226

225227
match self.tail {
226228
None => self.head = node,
@@ -842,8 +844,8 @@ impl<T, A: Allocator> LinkedList<T, A> {
842844
#[stable(feature = "rust1", since = "1.0.0")]
843845
pub fn push_front(&mut self, elt: T) {
844846
let node = Box::new_in(Node::new(elt), &self.alloc);
845-
let node_ptr = Unique::from(Box::leak(node));
846-
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc
847+
let node_ptr = NonNull::from(Box::leak(node));
848+
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
847849
unsafe {
848850
self.push_front_node(node_ptr);
849851
}
@@ -890,8 +892,8 @@ impl<T, A: Allocator> LinkedList<T, A> {
890892
#[stable(feature = "rust1", since = "1.0.0")]
891893
pub fn push_back(&mut self, elt: T) {
892894
let node = Box::new_in(Node::new(elt), &self.alloc);
893-
let node_ptr = Unique::from(Box::leak(node));
894-
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc
895+
let node_ptr = NonNull::from(Box::leak(node));
896+
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
895897
unsafe {
896898
self.push_back_node(node_ptr);
897899
}

0 commit comments

Comments
 (0)