Skip to content

Commit

Permalink
Rollup merge of #114257 - rytheo:linked-list-avoid-unique, r=cuviper
Browse files Browse the repository at this point in the history
Avoid using `ptr::Unique` in `LinkedList` code

Addresses a [comment](rust-lang/rust#103093 (comment)) by `@RalfJung` about avoiding use of `core::ptr::Unique` in the standard library.
  • Loading branch information
compiler-errors authored Aug 11, 2023
2 parents 9dd4c70 + cd15eae commit c28f3b0
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions alloc/src/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use core::hash::{Hash, Hasher};
use core::iter::FusedIterator;
use core::marker::PhantomData;
use core::mem;
use core::ptr::{NonNull, Unique};
use core::ptr::NonNull;

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

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

match self.tail {
None => self.head = node,
Expand Down Expand Up @@ -842,8 +844,8 @@ impl<T, A: Allocator> LinkedList<T, A> {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push_front(&mut self, elt: T) {
let node = Box::new_in(Node::new(elt), &self.alloc);
let node_ptr = Unique::from(Box::leak(node));
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc
let node_ptr = NonNull::from(Box::leak(node));
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
unsafe {
self.push_front_node(node_ptr);
}
Expand Down Expand Up @@ -890,8 +892,8 @@ impl<T, A: Allocator> LinkedList<T, A> {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push_back(&mut self, elt: T) {
let node = Box::new_in(Node::new(elt), &self.alloc);
let node_ptr = Unique::from(Box::leak(node));
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc
let node_ptr = NonNull::from(Box::leak(node));
// SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc and leaked
unsafe {
self.push_back_node(node_ptr);
}
Expand Down

0 comments on commit c28f3b0

Please sign in to comment.