@@ -18,9 +18,10 @@ use core::hash::{Hash, Hasher};
18
18
use core:: iter:: { FromIterator , FusedIterator } ;
19
19
use core:: marker:: PhantomData ;
20
20
use core:: mem;
21
- use core:: ptr:: NonNull ;
21
+ use core:: ptr:: { NonNull , Unique } ;
22
22
23
23
use super :: SpecExtend ;
24
+ use crate :: alloc:: { Allocator , Global } ;
24
25
use crate :: boxed:: Box ;
25
26
26
27
#[ cfg( test) ]
@@ -47,11 +48,15 @@ mod tests;
47
48
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
48
49
#[ cfg_attr( not( test) , rustc_diagnostic_item = "LinkedList" ) ]
49
50
#[ rustc_insignificant_dtor]
50
- pub struct LinkedList < T > {
51
+ pub struct LinkedList <
52
+ T ,
53
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
54
+ > {
51
55
head : Option < NonNull < Node < T > > > ,
52
56
tail : Option < NonNull < Node < T > > > ,
53
57
len : usize ,
54
- marker : PhantomData < Box < Node < T > > > ,
58
+ alloc : A ,
59
+ marker : PhantomData < Box < Node < T > , A > > ,
55
60
}
56
61
57
62
struct Node < T > {
@@ -81,6 +86,7 @@ impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
81
86
head : self . head ,
82
87
tail : self . tail ,
83
88
len : self . len ,
89
+ alloc : Global ,
84
90
marker : PhantomData ,
85
91
} ) )
86
92
. field ( & self . len )
@@ -117,6 +123,7 @@ impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
117
123
head : self . head ,
118
124
tail : self . tail ,
119
125
len : self . len ,
126
+ alloc : Global ,
120
127
marker : PhantomData ,
121
128
} ) )
122
129
. field ( & self . len )
@@ -133,12 +140,15 @@ impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
133
140
/// [`IntoIterator`]: core::iter::IntoIterator
134
141
#[ derive( Clone ) ]
135
142
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
136
- pub struct IntoIter < T > {
137
- list : LinkedList < T > ,
143
+ pub struct IntoIter <
144
+ T ,
145
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
146
+ > {
147
+ list : LinkedList < T , A > ,
138
148
}
139
149
140
150
#[ stable( feature = "collection_debug" , since = "1.17.0" ) ]
141
- impl < T : fmt:: Debug > fmt:: Debug for IntoIter < T > {
151
+ impl < T : fmt:: Debug , A : Allocator > fmt:: Debug for IntoIter < T , A > {
142
152
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
143
153
f. debug_tuple ( "IntoIter" ) . field ( & self . list ) . finish ( )
144
154
}
@@ -149,22 +159,25 @@ impl<T> Node<T> {
149
159
Node { next : None , prev : None , element }
150
160
}
151
161
152
- fn into_element ( self : Box < Self > ) -> T {
162
+ fn into_element < A : Allocator > ( self : Box < Self , A > ) -> T {
153
163
self . element
154
164
}
155
165
}
156
166
157
167
// private methods
158
- impl < T > LinkedList < T > {
168
+ impl < T , A : Allocator > LinkedList < T , A > {
159
169
/// Adds the given node to the front of the list.
170
+ ///
171
+ /// # Safety
172
+ /// `node` must point to a valid node that was boxed using the list's allocator.
160
173
#[ inline]
161
- fn push_front_node ( & mut self , mut node : Box < Node < T > > ) {
174
+ unsafe fn push_front_node ( & mut self , node : Unique < Node < T > > ) {
162
175
// This method takes care not to create mutable references to whole nodes,
163
176
// to maintain validity of aliasing pointers into `element`.
164
177
unsafe {
165
- node. next = self . head ;
166
- node. prev = None ;
167
- let node = Some ( Box :: leak ( node) . into ( ) ) ;
178
+ ( * node. as_ptr ( ) ) . next = self . head ;
179
+ ( * node. as_ptr ( ) ) . prev = None ;
180
+ let node = Some ( NonNull :: from ( node) ) ;
168
181
169
182
match self . head {
170
183
None => self . tail = node,
@@ -179,11 +192,11 @@ impl<T> LinkedList<T> {
179
192
180
193
/// Removes and returns the node at the front of the list.
181
194
#[ inline]
182
- fn pop_front_node ( & mut self ) -> Option < Box < Node < T > > > {
195
+ fn pop_front_node ( & mut self ) -> Option < Box < Node < T > , & A > > {
183
196
// This method takes care not to create mutable references to whole nodes,
184
197
// to maintain validity of aliasing pointers into `element`.
185
198
self . head . map ( |node| unsafe {
186
- let node = Box :: from_raw ( node. as_ptr ( ) ) ;
199
+ let node = Box :: from_raw_in ( node. as_ptr ( ) , & self . alloc ) ;
187
200
self . head = node. next ;
188
201
189
202
match self . head {
@@ -198,14 +211,17 @@ impl<T> LinkedList<T> {
198
211
}
199
212
200
213
/// Adds the given node to the back of the list.
214
+ ///
215
+ /// # Safety
216
+ /// `node` must point to a valid node that was boxed using the list's allocator.
201
217
#[ inline]
202
- fn push_back_node ( & mut self , mut node : Box < Node < T > > ) {
218
+ unsafe fn push_back_node ( & mut self , node : Unique < Node < T > > ) {
203
219
// This method takes care not to create mutable references to whole nodes,
204
220
// to maintain validity of aliasing pointers into `element`.
205
221
unsafe {
206
- node. next = None ;
207
- node. prev = self . tail ;
208
- let node = Some ( Box :: leak ( node) . into ( ) ) ;
222
+ ( * node. as_ptr ( ) ) . next = None ;
223
+ ( * node. as_ptr ( ) ) . prev = self . tail ;
224
+ let node = Some ( NonNull :: from ( node) ) ;
209
225
210
226
match self . tail {
211
227
None => self . head = node,
@@ -220,11 +236,11 @@ impl<T> LinkedList<T> {
220
236
221
237
/// Removes and returns the node at the back of the list.
222
238
#[ inline]
223
- fn pop_back_node ( & mut self ) -> Option < Box < Node < T > > > {
239
+ fn pop_back_node ( & mut self ) -> Option < Box < Node < T > , & A > > {
224
240
// This method takes care not to create mutable references to whole nodes,
225
241
// to maintain validity of aliasing pointers into `element`.
226
242
self . tail . map ( |node| unsafe {
227
- let node = Box :: from_raw ( node. as_ptr ( ) ) ;
243
+ let node = Box :: from_raw_in ( node. as_ptr ( ) , & self . alloc ) ;
228
244
self . tail = node. prev ;
229
245
230
246
match self . tail {
@@ -322,7 +338,10 @@ impl<T> LinkedList<T> {
322
338
& mut self ,
323
339
split_node : Option < NonNull < Node < T > > > ,
324
340
at : usize ,
325
- ) -> Self {
341
+ ) -> Self
342
+ where
343
+ A : Clone ,
344
+ {
326
345
// The split node is the new head node of the second part
327
346
if let Some ( mut split_node) = split_node {
328
347
let first_part_head;
@@ -343,6 +362,7 @@ impl<T> LinkedList<T> {
343
362
head : first_part_head,
344
363
tail : first_part_tail,
345
364
len : at,
365
+ alloc : self . alloc . clone ( ) ,
346
366
marker : PhantomData ,
347
367
} ;
348
368
@@ -352,7 +372,7 @@ impl<T> LinkedList<T> {
352
372
353
373
first_part
354
374
} else {
355
- mem:: replace ( self , LinkedList :: new ( ) )
375
+ mem:: replace ( self , LinkedList :: new_in ( self . alloc . clone ( ) ) )
356
376
}
357
377
}
358
378
@@ -361,7 +381,10 @@ impl<T> LinkedList<T> {
361
381
& mut self ,
362
382
split_node : Option < NonNull < Node < T > > > ,
363
383
at : usize ,
364
- ) -> Self {
384
+ ) -> Self
385
+ where
386
+ A : Clone ,
387
+ {
365
388
// The split node is the new tail node of the first part and owns
366
389
// the head of the second part.
367
390
if let Some ( mut split_node) = split_node {
@@ -383,6 +406,7 @@ impl<T> LinkedList<T> {
383
406
head : second_part_head,
384
407
tail : second_part_tail,
385
408
len : self . len - at,
409
+ alloc : self . alloc . clone ( ) ,
386
410
marker : PhantomData ,
387
411
} ;
388
412
@@ -392,7 +416,7 @@ impl<T> LinkedList<T> {
392
416
393
417
second_part
394
418
} else {
395
- mem:: replace ( self , LinkedList :: new ( ) )
419
+ mem:: replace ( self , LinkedList :: new_in ( self . alloc . clone ( ) ) )
396
420
}
397
421
}
398
422
}
@@ -421,7 +445,7 @@ impl<T> LinkedList<T> {
421
445
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
422
446
#[ must_use]
423
447
pub const fn new ( ) -> Self {
424
- LinkedList { head : None , tail : None , len : 0 , marker : PhantomData }
448
+ LinkedList { head : None , tail : None , len : 0 , alloc : Global , marker : PhantomData }
425
449
}
426
450
427
451
/// Moves all elements from `other` to the end of the list.
@@ -472,7 +496,26 @@ impl<T> LinkedList<T> {
472
496
}
473
497
}
474
498
}
499
+ }
475
500
501
+ impl < T , A : Allocator > LinkedList < T , A > {
502
+ /// Constructs an empty `LinkedList<T, A>`.
503
+ ///
504
+ /// # Examples
505
+ ///
506
+ /// ```
507
+ /// #![feature(allocator_api)]
508
+ ///
509
+ /// use std::alloc::System;
510
+ /// use std::collections::LinkedList;
511
+ ///
512
+ /// let list: LinkedList<u32, _> = LinkedList::new_in(System);
513
+ /// ```
514
+ #[ inline]
515
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
516
+ pub const fn new_in ( alloc : A ) -> Self {
517
+ LinkedList { head : None , tail : None , len : 0 , alloc, marker : PhantomData }
518
+ }
476
519
/// Provides a forward iterator.
477
520
///
478
521
/// # Examples
@@ -533,7 +576,7 @@ impl<T> LinkedList<T> {
533
576
#[ inline]
534
577
#[ must_use]
535
578
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
536
- pub fn cursor_front ( & self ) -> Cursor < ' _ , T > {
579
+ pub fn cursor_front ( & self ) -> Cursor < ' _ , T , A > {
537
580
Cursor { index : 0 , current : self . head , list : self }
538
581
}
539
582
@@ -543,7 +586,7 @@ impl<T> LinkedList<T> {
543
586
#[ inline]
544
587
#[ must_use]
545
588
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
546
- pub fn cursor_front_mut ( & mut self ) -> CursorMut < ' _ , T > {
589
+ pub fn cursor_front_mut ( & mut self ) -> CursorMut < ' _ , T , A > {
547
590
CursorMut { index : 0 , current : self . head , list : self }
548
591
}
549
592
@@ -553,7 +596,7 @@ impl<T> LinkedList<T> {
553
596
#[ inline]
554
597
#[ must_use]
555
598
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
556
- pub fn cursor_back ( & self ) -> Cursor < ' _ , T > {
599
+ pub fn cursor_back ( & self ) -> Cursor < ' _ , T , A > {
557
600
Cursor { index : self . len . checked_sub ( 1 ) . unwrap_or ( 0 ) , current : self . tail , list : self }
558
601
}
559
602
@@ -563,7 +606,7 @@ impl<T> LinkedList<T> {
563
606
#[ inline]
564
607
#[ must_use]
565
608
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
566
- pub fn cursor_back_mut ( & mut self ) -> CursorMut < ' _ , T > {
609
+ pub fn cursor_back_mut ( & mut self ) -> CursorMut < ' _ , T , A > {
567
610
CursorMut { index : self . len . checked_sub ( 1 ) . unwrap_or ( 0 ) , current : self . tail , list : self }
568
611
}
569
612
@@ -639,7 +682,15 @@ impl<T> LinkedList<T> {
639
682
#[ inline]
640
683
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
641
684
pub fn clear ( & mut self ) {
642
- * self = Self :: new ( ) ;
685
+ // We need to drop the nodes while keeping self.alloc
686
+ // We can do this by moving (head, tail, len) into a new list that borrows self.alloc
687
+ drop ( LinkedList {
688
+ head : self . head . take ( ) ,
689
+ tail : self . tail . take ( ) ,
690
+ len : mem:: take ( & mut self . len ) ,
691
+ alloc : & self . alloc ,
692
+ marker : PhantomData ,
693
+ } ) ;
643
694
}
644
695
645
696
/// Returns `true` if the `LinkedList` contains an element equal to the
@@ -791,7 +842,12 @@ impl<T> LinkedList<T> {
791
842
/// ```
792
843
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
793
844
pub fn push_front ( & mut self , elt : T ) {
794
- self . push_front_node ( Box :: new ( Node :: new ( elt) ) ) ;
845
+ let node = Box :: new_in ( Node :: new ( elt) , & self . alloc ) ;
846
+ let node_ptr = Unique :: from ( Box :: leak ( node) ) ;
847
+ // SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc
848
+ unsafe {
849
+ self . push_front_node ( node_ptr) ;
850
+ }
795
851
}
796
852
797
853
/// Removes the first element and returns it, or `None` if the list is
@@ -834,7 +890,12 @@ impl<T> LinkedList<T> {
834
890
/// ```
835
891
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
836
892
pub fn push_back ( & mut self , elt : T ) {
837
- self . push_back_node ( Box :: new ( Node :: new ( elt) ) ) ;
893
+ let node = Box :: new_in ( Node :: new ( elt) , & self . alloc ) ;
894
+ let node_ptr = Unique :: from ( Box :: leak ( node) ) ;
895
+ // SAFETY: node_ptr is a unique pointer to a node we boxed with self.alloc
896
+ unsafe {
897
+ self . push_back_node ( node_ptr) ;
898
+ }
838
899
}
839
900
840
901
/// Removes the last element from a list and returns it, or `None` if
@@ -884,13 +945,16 @@ impl<T> LinkedList<T> {
884
945
/// assert_eq!(split.pop_front(), None);
885
946
/// ```
886
947
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
887
- pub fn split_off ( & mut self , at : usize ) -> LinkedList < T > {
948
+ pub fn split_off ( & mut self , at : usize ) -> LinkedList < T , A >
949
+ where
950
+ A : Clone ,
951
+ {
888
952
let len = self . len ( ) ;
889
953
assert ! ( at <= len, "Cannot split off at a nonexistent index" ) ;
890
954
if at == 0 {
891
- return mem:: take ( self ) ;
955
+ return mem:: replace ( self , Self :: new_in ( self . alloc . clone ( ) ) ) ;
892
956
} else if at == len {
893
- return Self :: new ( ) ;
957
+ return Self :: new_in ( self . alloc . clone ( ) ) ;
894
958
}
895
959
896
960
// Below, we iterate towards the `i-1`th node, either from the start or the end,
@@ -988,7 +1052,7 @@ impl<T> LinkedList<T> {
988
1052
/// assert_eq!(odds.into_iter().collect::<Vec<_>>(), vec![1, 3, 5, 9, 11, 13, 15]);
989
1053
/// ```
990
1054
#[ unstable( feature = "drain_filter" , reason = "recently added" , issue = "43244" ) ]
991
- pub fn drain_filter < F > ( & mut self , filter : F ) -> DrainFilter < ' _ , T , F >
1055
+ pub fn drain_filter < F > ( & mut self , filter : F ) -> DrainFilter < ' _ , T , F , A >
992
1056
where
993
1057
F : FnMut ( & mut T ) -> bool ,
994
1058
{
@@ -1001,23 +1065,22 @@ impl<T> LinkedList<T> {
1001
1065
}
1002
1066
1003
1067
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1004
- unsafe impl < #[ may_dangle] T > Drop for LinkedList < T > {
1068
+ unsafe impl < #[ may_dangle] T , A : Allocator > Drop for LinkedList < T , A > {
1005
1069
fn drop ( & mut self ) {
1006
- struct DropGuard < ' a , T > ( & ' a mut LinkedList < T > ) ;
1070
+ struct DropGuard < ' a , T , A : Allocator > ( & ' a mut LinkedList < T , A > ) ;
1007
1071
1008
- impl < ' a , T > Drop for DropGuard < ' a , T > {
1072
+ impl < ' a , T , A : Allocator > Drop for DropGuard < ' a , T , A > {
1009
1073
fn drop ( & mut self ) {
1010
1074
// Continue the same loop we do below. This only runs when a destructor has
1011
1075
// panicked. If another one panics this will abort.
1012
1076
while self . 0 . pop_front_node ( ) . is_some ( ) { }
1013
1077
}
1014
1078
}
1015
1079
1016
- while let Some ( node) = self . pop_front_node ( ) {
1017
- let guard = DropGuard ( self ) ;
1018
- drop ( node) ;
1019
- mem:: forget ( guard) ;
1020
- }
1080
+ // Wrap self so that if a destructor panics, we can try to keep looping
1081
+ let guard = DropGuard ( self ) ;
1082
+ while guard. 0 . pop_front_node ( ) . is_some ( ) { }
1083
+ mem:: forget ( guard) ;
1021
1084
}
1022
1085
}
1023
1086
@@ -1139,22 +1202,26 @@ impl<T> FusedIterator for IterMut<'_, T> {}
1139
1202
///
1140
1203
/// When created, cursors start at the front of the list, or the "ghost" non-element if the list is empty.
1141
1204
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1142
- pub struct Cursor < ' a , T : ' a > {
1205
+ pub struct Cursor <
1206
+ ' a ,
1207
+ T : ' a ,
1208
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
1209
+ > {
1143
1210
index : usize ,
1144
1211
current : Option < NonNull < Node < T > > > ,
1145
- list : & ' a LinkedList < T > ,
1212
+ list : & ' a LinkedList < T , A > ,
1146
1213
}
1147
1214
1148
1215
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1149
- impl < T > Clone for Cursor < ' _ , T > {
1216
+ impl < T , A : Allocator > Clone for Cursor < ' _ , T , A > {
1150
1217
fn clone ( & self ) -> Self {
1151
1218
let Cursor { index, current, list } = * self ;
1152
1219
Cursor { index, current, list }
1153
1220
}
1154
1221
}
1155
1222
1156
1223
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1157
- impl < T : fmt:: Debug > fmt:: Debug for Cursor < ' _ , T > {
1224
+ impl < T : fmt:: Debug , A : Allocator > fmt:: Debug for Cursor < ' _ , T , A > {
1158
1225
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1159
1226
f. debug_tuple ( "Cursor" ) . field ( & self . list ) . field ( & self . index ( ) ) . finish ( )
1160
1227
}
@@ -1171,20 +1238,24 @@ impl<T: fmt::Debug> fmt::Debug for Cursor<'_, T> {
1171
1238
/// To accommodate this, there is a "ghost" non-element that yields `None` between the head and
1172
1239
/// tail of the list.
1173
1240
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1174
- pub struct CursorMut < ' a , T : ' a > {
1241
+ pub struct CursorMut <
1242
+ ' a ,
1243
+ T : ' a ,
1244
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
1245
+ > {
1175
1246
index : usize ,
1176
1247
current : Option < NonNull < Node < T > > > ,
1177
- list : & ' a mut LinkedList < T > ,
1248
+ list : & ' a mut LinkedList < T , A > ,
1178
1249
}
1179
1250
1180
1251
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1181
- impl < T : fmt:: Debug > fmt:: Debug for CursorMut < ' _ , T > {
1252
+ impl < T : fmt:: Debug , A : Allocator > fmt:: Debug for CursorMut < ' _ , T , A > {
1182
1253
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1183
1254
f. debug_tuple ( "CursorMut" ) . field ( & self . list ) . field ( & self . index ( ) ) . finish ( )
1184
1255
}
1185
1256
}
1186
1257
1187
- impl < ' a , T > Cursor < ' a , T > {
1258
+ impl < ' a , T , A : Allocator > Cursor < ' a , T , A > {
1188
1259
/// Returns the cursor position index within the `LinkedList`.
1189
1260
///
1190
1261
/// This returns `None` if the cursor is currently pointing to the
@@ -1301,7 +1372,7 @@ impl<'a, T> Cursor<'a, T> {
1301
1372
}
1302
1373
}
1303
1374
1304
- impl < ' a , T > CursorMut < ' a , T > {
1375
+ impl < ' a , T , A : Allocator > CursorMut < ' a , T , A > {
1305
1376
/// Returns the cursor position index within the `LinkedList`.
1306
1377
///
1307
1378
/// This returns `None` if the cursor is currently pointing to the
@@ -1406,22 +1477,67 @@ impl<'a, T> CursorMut<'a, T> {
1406
1477
/// `CursorMut` is frozen for the lifetime of the `Cursor`.
1407
1478
#[ must_use]
1408
1479
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1409
- pub fn as_cursor ( & self ) -> Cursor < ' _ , T > {
1480
+ pub fn as_cursor ( & self ) -> Cursor < ' _ , T , A > {
1410
1481
Cursor { list : self . list , current : self . current , index : self . index }
1411
1482
}
1412
1483
}
1413
1484
1414
1485
// Now the list editing operations
1415
1486
1416
1487
impl < ' a , T > CursorMut < ' a , T > {
1488
+ /// Inserts the elements from the given `LinkedList` after the current one.
1489
+ ///
1490
+ /// If the cursor is pointing at the "ghost" non-element then the new elements are
1491
+ /// inserted at the start of the `LinkedList`.
1492
+ #[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1493
+ pub fn splice_after ( & mut self , list : LinkedList < T > ) {
1494
+ unsafe {
1495
+ let ( splice_head, splice_tail, splice_len) = match list. detach_all_nodes ( ) {
1496
+ Some ( parts) => parts,
1497
+ _ => return ,
1498
+ } ;
1499
+ let node_next = match self . current {
1500
+ None => self . list . head ,
1501
+ Some ( node) => node. as_ref ( ) . next ,
1502
+ } ;
1503
+ self . list . splice_nodes ( self . current , node_next, splice_head, splice_tail, splice_len) ;
1504
+ if self . current . is_none ( ) {
1505
+ // The "ghost" non-element's index has changed.
1506
+ self . index = self . list . len ;
1507
+ }
1508
+ }
1509
+ }
1510
+
1511
+ /// Inserts the elements from the given `LinkedList` before the current one.
1512
+ ///
1513
+ /// If the cursor is pointing at the "ghost" non-element then the new elements are
1514
+ /// inserted at the end of the `LinkedList`.
1515
+ #[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1516
+ pub fn splice_before ( & mut self , list : LinkedList < T > ) {
1517
+ unsafe {
1518
+ let ( splice_head, splice_tail, splice_len) = match list. detach_all_nodes ( ) {
1519
+ Some ( parts) => parts,
1520
+ _ => return ,
1521
+ } ;
1522
+ let node_prev = match self . current {
1523
+ None => self . list . tail ,
1524
+ Some ( node) => node. as_ref ( ) . prev ,
1525
+ } ;
1526
+ self . list . splice_nodes ( node_prev, self . current , splice_head, splice_tail, splice_len) ;
1527
+ self . index += splice_len;
1528
+ }
1529
+ }
1530
+ }
1531
+
1532
+ impl < ' a , T , A : Allocator > CursorMut < ' a , T , A > {
1417
1533
/// Inserts a new element into the `LinkedList` after the current one.
1418
1534
///
1419
1535
/// If the cursor is pointing at the "ghost" non-element then the new element is
1420
1536
/// inserted at the front of the `LinkedList`.
1421
1537
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1422
1538
pub fn insert_after ( & mut self , item : T ) {
1423
1539
unsafe {
1424
- let spliced_node = Box :: leak ( Box :: new ( Node :: new ( item) ) ) . into ( ) ;
1540
+ let spliced_node = Box :: leak ( Box :: new_in ( Node :: new ( item) , & self . list . alloc ) ) . into ( ) ;
1425
1541
let node_next = match self . current {
1426
1542
None => self . list . head ,
1427
1543
Some ( node) => node. as_ref ( ) . next ,
@@ -1441,7 +1557,7 @@ impl<'a, T> CursorMut<'a, T> {
1441
1557
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1442
1558
pub fn insert_before ( & mut self , item : T ) {
1443
1559
unsafe {
1444
- let spliced_node = Box :: leak ( Box :: new ( Node :: new ( item) ) ) . into ( ) ;
1560
+ let spliced_node = Box :: leak ( Box :: new_in ( Node :: new ( item) , & self . list . alloc ) ) . into ( ) ;
1445
1561
let node_prev = match self . current {
1446
1562
None => self . list . tail ,
1447
1563
Some ( node) => node. as_ref ( ) . prev ,
@@ -1477,7 +1593,10 @@ impl<'a, T> CursorMut<'a, T> {
1477
1593
/// If the cursor is currently pointing to the "ghost" non-element then no element
1478
1594
/// is removed and `None` is returned.
1479
1595
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1480
- pub fn remove_current_as_list ( & mut self ) -> Option < LinkedList < T > > {
1596
+ pub fn remove_current_as_list ( & mut self ) -> Option < LinkedList < T , A > >
1597
+ where
1598
+ A : Clone ,
1599
+ {
1481
1600
let mut unlinked_node = self . current ?;
1482
1601
unsafe {
1483
1602
self . current = unlinked_node. as_ref ( ) . next ;
@@ -1489,62 +1608,23 @@ impl<'a, T> CursorMut<'a, T> {
1489
1608
head : Some ( unlinked_node) ,
1490
1609
tail : Some ( unlinked_node) ,
1491
1610
len : 1 ,
1611
+ alloc : self . list . alloc . clone ( ) ,
1492
1612
marker : PhantomData ,
1493
1613
} )
1494
1614
}
1495
1615
}
1496
1616
1497
- /// Inserts the elements from the given `LinkedList` after the current one.
1498
- ///
1499
- /// If the cursor is pointing at the "ghost" non-element then the new elements are
1500
- /// inserted at the start of the `LinkedList`.
1501
- #[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1502
- pub fn splice_after ( & mut self , list : LinkedList < T > ) {
1503
- unsafe {
1504
- let ( splice_head, splice_tail, splice_len) = match list. detach_all_nodes ( ) {
1505
- Some ( parts) => parts,
1506
- _ => return ,
1507
- } ;
1508
- let node_next = match self . current {
1509
- None => self . list . head ,
1510
- Some ( node) => node. as_ref ( ) . next ,
1511
- } ;
1512
- self . list . splice_nodes ( self . current , node_next, splice_head, splice_tail, splice_len) ;
1513
- if self . current . is_none ( ) {
1514
- // The "ghost" non-element's index has changed.
1515
- self . index = self . list . len ;
1516
- }
1517
- }
1518
- }
1519
-
1520
- /// Inserts the elements from the given `LinkedList` before the current one.
1521
- ///
1522
- /// If the cursor is pointing at the "ghost" non-element then the new elements are
1523
- /// inserted at the end of the `LinkedList`.
1524
- #[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1525
- pub fn splice_before ( & mut self , list : LinkedList < T > ) {
1526
- unsafe {
1527
- let ( splice_head, splice_tail, splice_len) = match list. detach_all_nodes ( ) {
1528
- Some ( parts) => parts,
1529
- _ => return ,
1530
- } ;
1531
- let node_prev = match self . current {
1532
- None => self . list . tail ,
1533
- Some ( node) => node. as_ref ( ) . prev ,
1534
- } ;
1535
- self . list . splice_nodes ( node_prev, self . current , splice_head, splice_tail, splice_len) ;
1536
- self . index += splice_len;
1537
- }
1538
- }
1539
-
1540
1617
/// Splits the list into two after the current element. This will return a
1541
1618
/// new list consisting of everything after the cursor, with the original
1542
1619
/// list retaining everything before.
1543
1620
///
1544
1621
/// If the cursor is pointing at the "ghost" non-element then the entire contents
1545
1622
/// of the `LinkedList` are moved.
1546
1623
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1547
- pub fn split_after ( & mut self ) -> LinkedList < T > {
1624
+ pub fn split_after ( & mut self ) -> LinkedList < T , A >
1625
+ where
1626
+ A : Clone ,
1627
+ {
1548
1628
let split_off_idx = if self . index == self . list . len { 0 } else { self . index + 1 } ;
1549
1629
if self . index == self . list . len {
1550
1630
// The "ghost" non-element's index has changed to 0.
@@ -1560,7 +1640,10 @@ impl<'a, T> CursorMut<'a, T> {
1560
1640
/// If the cursor is pointing at the "ghost" non-element then the entire contents
1561
1641
/// of the `LinkedList` are moved.
1562
1642
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1563
- pub fn split_before ( & mut self ) -> LinkedList < T > {
1643
+ pub fn split_before ( & mut self ) -> LinkedList < T , A >
1644
+ where
1645
+ A : Clone ,
1646
+ {
1564
1647
let split_off_idx = self . index ;
1565
1648
self . index = 0 ;
1566
1649
unsafe { self . list . split_off_before_node ( self . current , split_off_idx) }
@@ -1702,19 +1785,23 @@ impl<'a, T> CursorMut<'a, T> {
1702
1785
1703
1786
/// An iterator produced by calling `drain_filter` on LinkedList.
1704
1787
#[ unstable( feature = "drain_filter" , reason = "recently added" , issue = "43244" ) ]
1705
- pub struct DrainFilter < ' a , T : ' a , F : ' a >
1706
- where
1788
+ pub struct DrainFilter <
1789
+ ' a ,
1790
+ T : ' a ,
1791
+ F : ' a ,
1792
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
1793
+ > where
1707
1794
F : FnMut ( & mut T ) -> bool ,
1708
1795
{
1709
- list : & ' a mut LinkedList < T > ,
1796
+ list : & ' a mut LinkedList < T , A > ,
1710
1797
it : Option < NonNull < Node < T > > > ,
1711
1798
pred : F ,
1712
1799
idx : usize ,
1713
1800
old_len : usize ,
1714
1801
}
1715
1802
1716
1803
#[ unstable( feature = "drain_filter" , reason = "recently added" , issue = "43244" ) ]
1717
- impl < T , F > Iterator for DrainFilter < ' _ , T , F >
1804
+ impl < T , F , A : Allocator > Iterator for DrainFilter < ' _ , T , F , A >
1718
1805
where
1719
1806
F : FnMut ( & mut T ) -> bool ,
1720
1807
{
@@ -1743,16 +1830,16 @@ where
1743
1830
}
1744
1831
1745
1832
#[ unstable( feature = "drain_filter" , reason = "recently added" , issue = "43244" ) ]
1746
- impl < T , F > Drop for DrainFilter < ' _ , T , F >
1833
+ impl < T , F , A : Allocator > Drop for DrainFilter < ' _ , T , F , A >
1747
1834
where
1748
1835
F : FnMut ( & mut T ) -> bool ,
1749
1836
{
1750
1837
fn drop ( & mut self ) {
1751
- struct DropGuard < ' r , ' a , T , F > ( & ' r mut DrainFilter < ' a , T , F > )
1838
+ struct DropGuard < ' r , ' a , T , F , A : Allocator > ( & ' r mut DrainFilter < ' a , T , F , A > )
1752
1839
where
1753
1840
F : FnMut ( & mut T ) -> bool ;
1754
1841
1755
- impl < ' r , ' a , T , F > Drop for DropGuard < ' r , ' a , T , F >
1842
+ impl < ' r , ' a , T , F , A : Allocator > Drop for DropGuard < ' r , ' a , T , F , A >
1756
1843
where
1757
1844
F : FnMut ( & mut T ) -> bool ,
1758
1845
{
@@ -1780,7 +1867,7 @@ where
1780
1867
}
1781
1868
1782
1869
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1783
- impl < T > Iterator for IntoIter < T > {
1870
+ impl < T , A : Allocator > Iterator for IntoIter < T , A > {
1784
1871
type Item = T ;
1785
1872
1786
1873
#[ inline]
@@ -1795,18 +1882,18 @@ impl<T> Iterator for IntoIter<T> {
1795
1882
}
1796
1883
1797
1884
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1798
- impl < T > DoubleEndedIterator for IntoIter < T > {
1885
+ impl < T , A : Allocator > DoubleEndedIterator for IntoIter < T , A > {
1799
1886
#[ inline]
1800
1887
fn next_back ( & mut self ) -> Option < T > {
1801
1888
self . list . pop_back ( )
1802
1889
}
1803
1890
}
1804
1891
1805
1892
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1806
- impl < T > ExactSizeIterator for IntoIter < T > { }
1893
+ impl < T , A : Allocator > ExactSizeIterator for IntoIter < T , A > { }
1807
1894
1808
1895
#[ stable( feature = "fused" , since = "1.26.0" ) ]
1809
- impl < T > FusedIterator for IntoIter < T > { }
1896
+ impl < T , A : Allocator > FusedIterator for IntoIter < T , A > { }
1810
1897
1811
1898
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1812
1899
impl < T > FromIterator < T > for LinkedList < T > {
@@ -1818,19 +1905,19 @@ impl<T> FromIterator<T> for LinkedList<T> {
1818
1905
}
1819
1906
1820
1907
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1821
- impl < T > IntoIterator for LinkedList < T > {
1908
+ impl < T , A : Allocator > IntoIterator for LinkedList < T , A > {
1822
1909
type Item = T ;
1823
- type IntoIter = IntoIter < T > ;
1910
+ type IntoIter = IntoIter < T , A > ;
1824
1911
1825
1912
/// Consumes the list into an iterator yielding elements by value.
1826
1913
#[ inline]
1827
- fn into_iter ( self ) -> IntoIter < T > {
1914
+ fn into_iter ( self ) -> IntoIter < T , A > {
1828
1915
IntoIter { list : self }
1829
1916
}
1830
1917
}
1831
1918
1832
1919
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1833
- impl < ' a , T > IntoIterator for & ' a LinkedList < T > {
1920
+ impl < ' a , T , A : Allocator > IntoIterator for & ' a LinkedList < T , A > {
1834
1921
type Item = & ' a T ;
1835
1922
type IntoIter = Iter < ' a , T > ;
1836
1923
@@ -1840,7 +1927,7 @@ impl<'a, T> IntoIterator for &'a LinkedList<T> {
1840
1927
}
1841
1928
1842
1929
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1843
- impl < ' a , T > IntoIterator for & ' a mut LinkedList < T > {
1930
+ impl < ' a , T , A : Allocator > IntoIterator for & ' a mut LinkedList < T , A > {
1844
1931
type Item = & ' a mut T ;
1845
1932
type IntoIter = IterMut < ' a , T > ;
1846
1933
@@ -1850,7 +1937,7 @@ impl<'a, T> IntoIterator for &'a mut LinkedList<T> {
1850
1937
}
1851
1938
1852
1939
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1853
- impl < T > Extend < T > for LinkedList < T > {
1940
+ impl < T , A : Allocator > Extend < T > for LinkedList < T , A > {
1854
1941
fn extend < I : IntoIterator < Item = T > > ( & mut self , iter : I ) {
1855
1942
<Self as SpecExtend < I > >:: spec_extend ( self , iter) ;
1856
1943
}
@@ -1861,7 +1948,7 @@ impl<T> Extend<T> for LinkedList<T> {
1861
1948
}
1862
1949
}
1863
1950
1864
- impl < I : IntoIterator > SpecExtend < I > for LinkedList < I :: Item > {
1951
+ impl < I : IntoIterator , A : Allocator > SpecExtend < I > for LinkedList < I :: Item , A > {
1865
1952
default fn spec_extend ( & mut self , iter : I ) {
1866
1953
iter. into_iter ( ) . for_each ( move |elt| self . push_back ( elt) ) ;
1867
1954
}
@@ -1874,7 +1961,7 @@ impl<T> SpecExtend<LinkedList<T>> for LinkedList<T> {
1874
1961
}
1875
1962
1876
1963
#[ stable( feature = "extend_ref" , since = "1.2.0" ) ]
1877
- impl < ' a , T : ' a + Copy > Extend < & ' a T > for LinkedList < T > {
1964
+ impl < ' a , T : ' a + Copy , A : Allocator > Extend < & ' a T > for LinkedList < T , A > {
1878
1965
fn extend < I : IntoIterator < Item = & ' a T > > ( & mut self , iter : I ) {
1879
1966
self . extend ( iter. into_iter ( ) . cloned ( ) ) ;
1880
1967
}
@@ -1886,7 +1973,7 @@ impl<'a, T: 'a + Copy> Extend<&'a T> for LinkedList<T> {
1886
1973
}
1887
1974
1888
1975
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1889
- impl < T : PartialEq > PartialEq for LinkedList < T > {
1976
+ impl < T : PartialEq , A : Allocator > PartialEq for LinkedList < T , A > {
1890
1977
fn eq ( & self , other : & Self ) -> bool {
1891
1978
self . len ( ) == other. len ( ) && self . iter ( ) . eq ( other)
1892
1979
}
@@ -1897,27 +1984,29 @@ impl<T: PartialEq> PartialEq for LinkedList<T> {
1897
1984
}
1898
1985
1899
1986
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1900
- impl < T : Eq > Eq for LinkedList < T > { }
1987
+ impl < T : Eq , A : Allocator > Eq for LinkedList < T , A > { }
1901
1988
1902
1989
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1903
- impl < T : PartialOrd > PartialOrd for LinkedList < T > {
1990
+ impl < T : PartialOrd , A : Allocator > PartialOrd for LinkedList < T , A > {
1904
1991
fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
1905
1992
self . iter ( ) . partial_cmp ( other)
1906
1993
}
1907
1994
}
1908
1995
1909
1996
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1910
- impl < T : Ord > Ord for LinkedList < T > {
1997
+ impl < T : Ord , A : Allocator > Ord for LinkedList < T , A > {
1911
1998
#[ inline]
1912
1999
fn cmp ( & self , other : & Self ) -> Ordering {
1913
2000
self . iter ( ) . cmp ( other)
1914
2001
}
1915
2002
}
1916
2003
1917
2004
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1918
- impl < T : Clone > Clone for LinkedList < T > {
2005
+ impl < T : Clone , A : Allocator + Clone > Clone for LinkedList < T , A > {
1919
2006
fn clone ( & self ) -> Self {
1920
- self . iter ( ) . cloned ( ) . collect ( )
2007
+ let mut list = Self :: new_in ( self . alloc . clone ( ) ) ;
2008
+ list. extend ( self . iter ( ) . cloned ( ) ) ;
2009
+ list
1921
2010
}
1922
2011
1923
2012
fn clone_from ( & mut self , other : & Self ) {
@@ -1935,14 +2024,14 @@ impl<T: Clone> Clone for LinkedList<T> {
1935
2024
}
1936
2025
1937
2026
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1938
- impl < T : fmt:: Debug > fmt:: Debug for LinkedList < T > {
2027
+ impl < T : fmt:: Debug , A : Allocator > fmt:: Debug for LinkedList < T , A > {
1939
2028
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1940
2029
f. debug_list ( ) . entries ( self ) . finish ( )
1941
2030
}
1942
2031
}
1943
2032
1944
2033
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1945
- impl < T : Hash > Hash for LinkedList < T > {
2034
+ impl < T : Hash , A : Allocator > Hash for LinkedList < T , A > {
1946
2035
fn hash < H : Hasher > ( & self , state : & mut H ) {
1947
2036
state. write_length_prefix ( self . len ( ) ) ;
1948
2037
for elt in self {
@@ -1982,10 +2071,10 @@ fn assert_covariance() {
1982
2071
}
1983
2072
1984
2073
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1985
- unsafe impl < T : Send > Send for LinkedList < T > { }
2074
+ unsafe impl < T : Send , A : Allocator + Send > Send for LinkedList < T , A > { }
1986
2075
1987
2076
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1988
- unsafe impl < T : Sync > Sync for LinkedList < T > { }
2077
+ unsafe impl < T : Sync , A : Allocator + Sync > Sync for LinkedList < T , A > { }
1989
2078
1990
2079
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1991
2080
unsafe impl < T : Sync > Send for Iter < ' _ , T > { }
@@ -2000,13 +2089,13 @@ unsafe impl<T: Send> Send for IterMut<'_, T> {}
2000
2089
unsafe impl < T : Sync > Sync for IterMut < ' _ , T > { }
2001
2090
2002
2091
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
2003
- unsafe impl < T : Sync > Send for Cursor < ' _ , T > { }
2092
+ unsafe impl < T : Sync , A : Allocator + Sync > Send for Cursor < ' _ , T , A > { }
2004
2093
2005
2094
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
2006
- unsafe impl < T : Sync > Sync for Cursor < ' _ , T > { }
2095
+ unsafe impl < T : Sync , A : Allocator + Sync > Sync for Cursor < ' _ , T , A > { }
2007
2096
2008
2097
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
2009
- unsafe impl < T : Send > Send for CursorMut < ' _ , T > { }
2098
+ unsafe impl < T : Send , A : Allocator + Send > Send for CursorMut < ' _ , T , A > { }
2010
2099
2011
2100
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
2012
- unsafe impl < T : Sync > Sync for CursorMut < ' _ , T > { }
2101
+ unsafe impl < T : Sync , A : Allocator + Sync > Sync for CursorMut < ' _ , T , A > { }
0 commit comments