@@ -18,7 +18,7 @@ use core::hash::{Hash, Hasher};
18
18
use core:: iter:: FusedIterator ;
19
19
use core:: marker:: PhantomData ;
20
20
use core:: mem;
21
- use core:: ptr:: { NonNull , Unique } ;
21
+ use core:: ptr:: NonNull ;
22
22
23
23
use super :: SpecExtend ;
24
24
use crate :: alloc:: { Allocator , Global } ;
@@ -168,15 +168,16 @@ impl<T, A: Allocator> LinkedList<T, A> {
168
168
/// Adds the given node to the front of the list.
169
169
///
170
170
/// # 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.
172
173
#[ 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 > > ) {
174
175
// This method takes care not to create mutable references to whole nodes,
175
176
// to maintain validity of aliasing pointers into `element`.
176
177
unsafe {
177
178
( * node. as_ptr ( ) ) . next = self . head ;
178
179
( * node. as_ptr ( ) ) . prev = None ;
179
- let node = Some ( NonNull :: from ( node) ) ;
180
+ let node = Some ( node) ;
180
181
181
182
match self . head {
182
183
None => self . tail = node,
@@ -212,15 +213,16 @@ impl<T, A: Allocator> LinkedList<T, A> {
212
213
/// Adds the given node to the back of the list.
213
214
///
214
215
/// # 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.
216
218
#[ 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 > > ) {
218
220
// This method takes care not to create mutable references to whole nodes,
219
221
// to maintain validity of aliasing pointers into `element`.
220
222
unsafe {
221
223
( * node. as_ptr ( ) ) . next = None ;
222
224
( * node. as_ptr ( ) ) . prev = self . tail ;
223
- let node = Some ( NonNull :: from ( node) ) ;
225
+ let node = Some ( node) ;
224
226
225
227
match self . tail {
226
228
None => self . head = node,
@@ -842,8 +844,8 @@ impl<T, A: Allocator> LinkedList<T, A> {
842
844
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
843
845
pub fn push_front ( & mut self , elt : T ) {
844
846
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
847
849
unsafe {
848
850
self . push_front_node ( node_ptr) ;
849
851
}
@@ -890,8 +892,8 @@ impl<T, A: Allocator> LinkedList<T, A> {
890
892
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
891
893
pub fn push_back ( & mut self , elt : T ) {
892
894
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
895
897
unsafe {
896
898
self . push_back_node ( node_ptr) ;
897
899
}
0 commit comments