13
13
//! The `LinkedList` allows pushing and popping elements at either end and is thus
14
14
//! efficiently usable as a double-ended queue.
15
15
16
- // LinkedList is constructed like a singly-linked list over the field `next`.
17
- // including the last link being None; each Node owns its `next` field.
18
- //
19
- // Backlinks over LinkedList::prev are raw pointers that form a full chain in
20
- // the reverse direction.
21
-
22
16
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
23
17
24
18
use alloc:: boxed:: { Box , IntermediateBox } ;
25
19
use core:: cmp:: Ordering ;
26
20
use core:: fmt;
27
21
use core:: hash:: { Hasher , Hash } ;
28
22
use core:: iter:: FromIterator ;
23
+ use core:: marker:: PhantomData ;
29
24
use core:: mem;
30
25
use core:: ops:: { BoxPlace , InPlace , Place , Placer } ;
31
26
use core:: ptr:: { self , Shared } ;
@@ -35,222 +30,156 @@ use super::SpecExtend;
35
30
/// A doubly-linked list.
36
31
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
37
32
pub struct LinkedList < T > {
38
- length : usize ,
39
- list_head : Link < T > ,
40
- list_tail : Rawlink < Node < T > > ,
41
- }
42
-
43
- type Link < T > = Option < Box < Node < T > > > ;
44
-
45
- struct Rawlink < T > {
46
- p : Option < Shared < T > > ,
33
+ head : Option < Shared < Node < T > > > ,
34
+ tail : Option < Shared < Node < T > > > ,
35
+ len : usize ,
36
+ marker : PhantomData < Box < Node < T > > > ,
47
37
}
48
38
49
- impl < T > Copy for Rawlink < T > { }
50
- unsafe impl < T : Send > Send for Rawlink < T > { }
51
- unsafe impl < T : Sync > Sync for Rawlink < T > { }
52
-
53
39
struct Node < T > {
54
- next : Link < T > ,
55
- prev : Rawlink < Node < T > > ,
56
- value : T ,
40
+ next : Option < Shared < Node < T > > > ,
41
+ prev : Option < Shared < Node < T > > > ,
42
+ element : T ,
57
43
}
58
44
59
- /// An iterator over references to the items of a `LinkedList`.
45
+ /// An iterator over references to the elements of a `LinkedList`.
60
46
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
61
47
pub struct Iter < ' a , T : ' a > {
62
- head : & ' a Link < T > ,
63
- tail : Rawlink < Node < T > > ,
64
- nelem : usize ,
48
+ head : Option < Shared < Node < T > > > ,
49
+ tail : Option < Shared < Node < T > > > ,
50
+ len : usize ,
51
+ marker : PhantomData < & ' a Node < T > > ,
65
52
}
66
53
67
54
// FIXME #19839: deriving is too aggressive on the bounds (T doesn't need to be Clone).
68
55
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
69
56
impl < ' a , T > Clone for Iter < ' a , T > {
70
- fn clone ( & self ) -> Iter < ' a , T > {
71
- Iter {
72
- head : self . head . clone ( ) ,
73
- tail : self . tail ,
74
- nelem : self . nelem ,
75
- }
57
+ fn clone ( & self ) -> Self {
58
+ Iter { ..* self }
76
59
}
77
60
}
78
61
79
- /// An iterator over mutable references to the items of a `LinkedList`.
62
+ /// An iterator over mutable references to the elements of a `LinkedList`.
80
63
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
81
64
pub struct IterMut < ' a , T : ' a > {
82
65
list : & ' a mut LinkedList < T > ,
83
- head : Rawlink < Node < T > > ,
84
- tail : Rawlink < Node < T > > ,
85
- nelem : usize ,
66
+ head : Option < Shared < Node < T > > > ,
67
+ tail : Option < Shared < Node < T > > > ,
68
+ len : usize ,
86
69
}
87
70
88
- /// An iterator over the items of a `LinkedList`.
71
+ /// An iterator over the elements of a `LinkedList`.
89
72
#[ derive( Clone ) ]
90
73
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
91
74
pub struct IntoIter < T > {
92
75
list : LinkedList < T > ,
93
76
}
94
77
95
- /// Rawlink is a type like Option<T> but for holding a raw pointer
96
- impl < T > Rawlink < T > {
97
- /// Like Option::None for Rawlink
98
- fn none ( ) -> Rawlink < T > {
99
- Rawlink { p : None }
100
- }
101
-
102
- /// Like Option::Some for Rawlink
103
- fn some ( n : & mut T ) -> Rawlink < T > {
104
- unsafe { Rawlink { p : Some ( Shared :: new ( n) ) } }
105
- }
106
-
107
- /// Convert the `Rawlink` into an Option value
108
- ///
109
- /// **unsafe** because:
110
- ///
111
- /// - Dereference of raw pointer.
112
- /// - Returns reference of arbitrary lifetime.
113
- unsafe fn resolve < ' a > ( & self ) -> Option < & ' a T > {
114
- self . p . map ( |p| & * * p)
115
- }
116
-
117
- /// Convert the `Rawlink` into an Option value
118
- ///
119
- /// **unsafe** because:
120
- ///
121
- /// - Dereference of raw pointer.
122
- /// - Returns reference of arbitrary lifetime.
123
- unsafe fn resolve_mut < ' a > ( & mut self ) -> Option < & ' a mut T > {
124
- self . p . map ( |p| & mut * * p)
125
- }
126
-
127
- /// Return the `Rawlink` and replace with `Rawlink::none()`
128
- fn take ( & mut self ) -> Rawlink < T > {
129
- mem:: replace ( self , Rawlink :: none ( ) )
130
- }
131
- }
132
-
133
- impl < ' a , T > From < & ' a mut Link < T > > for Rawlink < Node < T > > {
134
- fn from ( node : & ' a mut Link < T > ) -> Self {
135
- match node. as_mut ( ) {
136
- None => Rawlink :: none ( ) ,
137
- Some ( ptr) => Rawlink :: some ( ptr) ,
138
- }
139
- }
140
- }
141
-
142
- impl < T > Clone for Rawlink < T > {
143
- #[ inline]
144
- fn clone ( & self ) -> Rawlink < T > {
145
- Rawlink { p : self . p }
146
- }
147
- }
148
-
149
78
impl < T > Node < T > {
150
- fn new ( v : T ) -> Node < T > {
79
+ fn new ( element : T ) -> Self {
151
80
Node {
152
- value : v,
153
81
next : None ,
154
- prev : Rawlink :: none ( ) ,
82
+ prev : None ,
83
+ element : element,
155
84
}
156
85
}
157
86
158
- /// Update the `prev` link on `next`, then set self's next pointer.
159
- ///
160
- /// `self.next` should be `None` when you call this
161
- /// (otherwise a Node is probably being dropped by mistake).
162
- fn set_next ( & mut self , mut next : Box < Node < T > > ) {
163
- debug_assert ! ( self . next. is_none( ) ) ;
164
- next. prev = Rawlink :: some ( self ) ;
165
- self . next = Some ( next) ;
87
+ fn into_element ( self : Box < Self > ) -> T {
88
+ self . element
166
89
}
167
90
}
168
91
169
- /// Clear the .prev field on `next`, then return `Some(next)`
170
- fn link_no_prev < T > ( mut next : Box < Node < T > > ) -> Link < T > {
171
- next. prev = Rawlink :: none ( ) ;
172
- Some ( next)
173
- }
174
-
175
92
// private methods
176
93
impl < T > LinkedList < T > {
177
- /// Add a Node first in the list
94
+ /// Adds the given node to the front of the list.
178
95
#[ inline]
179
- fn push_front_node ( & mut self , mut new_head : Box < Node < T > > ) {
180
- match self . list_head {
181
- None => {
182
- self . list_head = link_no_prev ( new_head) ;
183
- self . list_tail = Rawlink :: from ( & mut self . list_head ) ;
184
- }
185
- Some ( ref mut head) => {
186
- new_head. prev = Rawlink :: none ( ) ;
187
- head. prev = Rawlink :: some ( & mut * new_head) ;
188
- mem:: swap ( head, & mut new_head) ;
189
- head. next = Some ( new_head) ;
96
+ fn push_front_node ( & mut self , mut node : Box < Node < T > > ) {
97
+ unsafe {
98
+ node. next = self . head ;
99
+ node. prev = None ;
100
+ let node = Some ( Shared :: new ( Box :: into_raw ( node) ) ) ;
101
+
102
+ match self . head {
103
+ None => self . tail = node,
104
+ Some ( head) => ( * * head) . prev = node,
190
105
}
106
+
107
+ self . head = node;
108
+ self . len += 1 ;
191
109
}
192
- self . length += 1 ;
193
110
}
194
111
195
- /// Remove the first Node and return it, or None if the list is empty
112
+ /// Removes and returns the node at the front of the list.
196
113
#[ inline]
197
114
fn pop_front_node ( & mut self ) -> Option < Box < Node < T > > > {
198
- self . list_head . take ( ) . map ( |mut front_node| {
199
- self . length -= 1 ;
200
- match front_node. next . take ( ) {
201
- Some ( node) => self . list_head = link_no_prev ( node) ,
202
- None => self . list_tail = Rawlink :: none ( ) ,
115
+ self . head . map ( |node| unsafe {
116
+ let node = Box :: from_raw ( * node) ;
117
+ self . head = node. next ;
118
+
119
+ match self . head {
120
+ None => self . tail = None ,
121
+ Some ( head) => ( * * head) . prev = None ,
203
122
}
204
- front_node
123
+
124
+ self . len -= 1 ;
125
+ node
205
126
} )
206
127
}
207
128
208
- /// Add a Node last in the list
129
+ /// Adds the given node to the back of the list.
209
130
#[ inline]
210
- fn push_back_node ( & mut self , new_tail : Box < Node < T > > ) {
211
- match unsafe { self . list_tail . resolve_mut ( ) } {
212
- None => return self . push_front_node ( new_tail) ,
213
- Some ( tail) => {
214
- tail. set_next ( new_tail) ;
215
- self . list_tail = Rawlink :: from ( & mut tail. next ) ;
131
+ fn push_back_node ( & mut self , mut node : Box < Node < T > > ) {
132
+ unsafe {
133
+ node. next = None ;
134
+ node. prev = self . tail ;
135
+ let node = Some ( Shared :: new ( Box :: into_raw ( node) ) ) ;
136
+
137
+ match self . tail {
138
+ None => self . head = node,
139
+ Some ( tail) => ( * * tail) . next = node,
216
140
}
141
+
142
+ self . tail = node;
143
+ self . len += 1 ;
217
144
}
218
- self . length += 1 ;
219
145
}
220
146
221
- /// Remove the last Node and return it, or None if the list is empty
147
+ /// Removes and returns the node at the back of the list.
222
148
#[ inline]
223
149
fn pop_back_node ( & mut self ) -> Option < Box < Node < T > > > {
224
- unsafe {
225
- self . list_tail . resolve_mut ( ) . and_then ( |tail| {
226
- self . length -= 1 ;
227
- self . list_tail = tail. prev ;
228
- match tail. prev . resolve_mut ( ) {
229
- None => self . list_head . take ( ) ,
230
- Some ( tail_prev) => tail_prev. next . take ( ) ,
231
- }
232
- } )
233
- }
150
+ self . tail . map ( |node| unsafe {
151
+ let node = Box :: from_raw ( * node) ;
152
+ self . tail = node. prev ;
153
+
154
+ match self . tail {
155
+ None => self . head = None ,
156
+ Some ( tail) => ( * * tail) . next = None ,
157
+ }
158
+
159
+ self . len -= 1 ;
160
+ node
161
+ } )
234
162
}
235
163
}
236
164
237
165
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
238
166
impl < T > Default for LinkedList < T > {
239
167
#[ inline]
240
- fn default ( ) -> LinkedList < T > {
241
- LinkedList :: new ( )
168
+ fn default ( ) -> Self {
169
+ Self :: new ( )
242
170
}
243
171
}
244
172
245
173
impl < T > LinkedList < T > {
246
174
/// Creates an empty `LinkedList`.
247
175
#[ inline]
248
176
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
249
- pub fn new ( ) -> LinkedList < T > {
177
+ pub fn new ( ) -> Self {
250
178
LinkedList {
251
- list_head : None ,
252
- list_tail : Rawlink :: none ( ) ,
253
- length : 0 ,
179
+ head : None ,
180
+ tail : None ,
181
+ len : 0 ,
182
+ marker : PhantomData ,
254
183
}
255
184
}
256
185
@@ -281,38 +210,30 @@ impl<T> LinkedList<T> {
281
210
/// println!("{}", b.len()); // prints 0
282
211
/// ```
283
212
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
284
- pub fn append ( & mut self , other : & mut LinkedList < T > ) {
285
- match unsafe { self . list_tail . resolve_mut ( ) } {
286
- None => {
287
- self . length = other. length ;
288
- self . list_head = other. list_head . take ( ) ;
289
- self . list_tail = other. list_tail . take ( ) ;
290
- }
291
- Some ( tail) => {
292
- // Carefully empty `other`.
293
- let o_tail = other. list_tail . take ( ) ;
294
- let o_length = other. length ;
295
- match other. list_head . take ( ) {
296
- None => return ,
297
- Some ( node) => {
298
- tail. set_next ( node) ;
299
- self . list_tail = o_tail;
300
- self . length += o_length;
301
- }
213
+ pub fn append ( & mut self , other : & mut Self ) {
214
+ match self . tail {
215
+ None => mem:: swap ( self , other) ,
216
+ Some ( tail) => if let Some ( other_head) = other. head . take ( ) {
217
+ unsafe {
218
+ ( * * tail) . next = Some ( other_head) ;
219
+ ( * * other_head) . prev = Some ( tail) ;
302
220
}
303
- }
221
+
222
+ self . tail = other. tail . take ( ) ;
223
+ self . len += mem:: replace ( & mut other. len , 0 ) ;
224
+ } ,
304
225
}
305
- other. length = 0 ;
306
226
}
307
227
308
228
/// Provides a forward iterator.
309
229
#[ inline]
310
230
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
311
231
pub fn iter ( & self ) -> Iter < T > {
312
232
Iter {
313
- nelem : self . len ( ) ,
314
- head : & self . list_head ,
315
- tail : self . list_tail ,
233
+ head : self . head ,
234
+ tail : self . tail ,
235
+ len : self . len ,
236
+ marker : PhantomData ,
316
237
}
317
238
}
318
239
@@ -321,9 +242,9 @@ impl<T> LinkedList<T> {
321
242
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
322
243
pub fn iter_mut ( & mut self ) -> IterMut < T > {
323
244
IterMut {
324
- nelem : self . len ( ) ,
325
- head : Rawlink :: from ( & mut self . list_head ) ,
326
- tail : self . list_tail ,
245
+ head : self . head ,
246
+ tail : self . tail ,
247
+ len : self . len ,
327
248
list : self ,
328
249
}
329
250
}
@@ -346,7 +267,7 @@ impl<T> LinkedList<T> {
346
267
#[ inline]
347
268
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
348
269
pub fn is_empty ( & self ) -> bool {
349
- self . list_head . is_none ( )
270
+ self . head . is_none ( )
350
271
}
351
272
352
273
/// Returns the length of the `LinkedList`.
@@ -373,7 +294,7 @@ impl<T> LinkedList<T> {
373
294
#[ inline]
374
295
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
375
296
pub fn len ( & self ) -> usize {
376
- self . length
297
+ self . len
377
298
}
378
299
379
300
/// Removes all elements from the `LinkedList`.
@@ -400,7 +321,7 @@ impl<T> LinkedList<T> {
400
321
#[ inline]
401
322
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
402
323
pub fn clear ( & mut self ) {
403
- * self = LinkedList :: new ( )
324
+ * self = Self :: new ( ) ;
404
325
}
405
326
406
327
/// Returns `true` if the `LinkedList` contains an element equal to the
@@ -431,7 +352,7 @@ impl<T> LinkedList<T> {
431
352
#[ inline]
432
353
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
433
354
pub fn front ( & self ) -> Option < & T > {
434
- self . list_head . as_ref ( ) . map ( |head| & head . value )
355
+ self . head . map ( |node| unsafe { & ( * * node ) . element } )
435
356
}
436
357
437
358
/// Provides a mutable reference to the front element, or `None` if the list
@@ -458,7 +379,7 @@ impl<T> LinkedList<T> {
458
379
#[ inline]
459
380
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
460
381
pub fn front_mut ( & mut self ) -> Option < & mut T > {
461
- self . list_head . as_mut ( ) . map ( |head| & mut head . value )
382
+ self . head . map ( |node| unsafe { & mut ( * * node ) . element } )
462
383
}
463
384
464
385
/// Provides a reference to the back element, or `None` if the list is
@@ -479,7 +400,7 @@ impl<T> LinkedList<T> {
479
400
#[ inline]
480
401
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
481
402
pub fn back ( & self ) -> Option < & T > {
482
- unsafe { self . list_tail . resolve ( ) . map ( |tail| & tail . value ) }
403
+ self . tail . map ( |node| unsafe { & ( * * node ) . element } )
483
404
}
484
405
485
406
/// Provides a mutable reference to the back element, or `None` if the list
@@ -506,7 +427,7 @@ impl<T> LinkedList<T> {
506
427
#[ inline]
507
428
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
508
429
pub fn back_mut ( & mut self ) -> Option < & mut T > {
509
- unsafe { self . list_tail . resolve_mut ( ) . map ( |tail| & mut tail . value ) }
430
+ self . tail . map ( |node| unsafe { & mut ( * * node ) . element } )
510
431
}
511
432
512
433
/// Adds an element first in the list.
@@ -529,7 +450,7 @@ impl<T> LinkedList<T> {
529
450
/// ```
530
451
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
531
452
pub fn push_front ( & mut self , elt : T ) {
532
- self . push_front_node ( box Node :: new ( elt) )
453
+ self . push_front_node ( box Node :: new ( elt) ) ;
533
454
}
534
455
535
456
/// Removes the first element and returns it, or `None` if the list is
@@ -555,7 +476,7 @@ impl<T> LinkedList<T> {
555
476
///
556
477
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
557
478
pub fn pop_front ( & mut self ) -> Option < T > {
558
- self . pop_front_node ( ) . map ( |box Node { value , .. } | value )
479
+ self . pop_front_node ( ) . map ( Node :: into_element )
559
480
}
560
481
561
482
/// Appends an element to the back of a list
@@ -572,7 +493,7 @@ impl<T> LinkedList<T> {
572
493
/// ```
573
494
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
574
495
pub fn push_back ( & mut self , elt : T ) {
575
- self . push_back_node ( box Node :: new ( elt) )
496
+ self . push_back_node ( box Node :: new ( elt) ) ;
576
497
}
577
498
578
499
/// Removes the last element from a list and returns it, or `None` if
@@ -591,7 +512,7 @@ impl<T> LinkedList<T> {
591
512
/// ```
592
513
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
593
514
pub fn pop_back ( & mut self ) -> Option < T > {
594
- self . pop_back_node ( ) . map ( |box Node { value , .. } | value )
515
+ self . pop_back_node ( ) . map ( Node :: into_element )
595
516
}
596
517
597
518
/// Splits the list into two at the given index. Returns everything after the given index,
@@ -624,14 +545,14 @@ impl<T> LinkedList<T> {
624
545
let len = self . len ( ) ;
625
546
assert ! ( at <= len, "Cannot split off at a nonexistent index" ) ;
626
547
if at == 0 {
627
- return mem:: replace ( self , LinkedList :: new ( ) ) ;
548
+ return mem:: replace ( self , Self :: new ( ) ) ;
628
549
} else if at == len {
629
- return LinkedList :: new ( ) ;
550
+ return Self :: new ( ) ;
630
551
}
631
552
632
553
// Below, we iterate towards the `i-1`th node, either from the start or the end,
633
554
// depending on which would be faster.
634
- let mut split_node = if at - 1 <= len - 1 - ( at - 1 ) {
555
+ let split_node = if at - 1 <= len - 1 - ( at - 1 ) {
635
556
let mut iter = self . iter_mut ( ) ;
636
557
// instead of skipping using .skip() (which creates a new struct),
637
558
// we skip manually so we can access the head field without
@@ -651,25 +572,25 @@ impl<T> LinkedList<T> {
651
572
652
573
// The split node is the new tail node of the first part and owns
653
574
// the head of the second part.
654
- let mut second_part_head;
575
+ let second_part_head;
655
576
656
577
unsafe {
657
- second_part_head = split_node. resolve_mut ( ) . unwrap ( ) . next . take ( ) ;
658
- match second_part_head {
659
- None => { }
660
- Some ( ref mut head) => head. prev = Rawlink :: none ( ) ,
578
+ second_part_head = ( * * split_node. unwrap ( ) ) . next . take ( ) ;
579
+ if let Some ( head) = second_part_head {
580
+ ( * * head) . prev = None ;
661
581
}
662
582
}
663
583
664
584
let second_part = LinkedList {
665
- list_head : second_part_head,
666
- list_tail : self . list_tail ,
667
- length : len - at,
585
+ head : second_part_head,
586
+ tail : self . tail ,
587
+ len : len - at,
588
+ marker : PhantomData ,
668
589
} ;
669
590
670
591
// Fix the tail ptr of the first part
671
- self . list_tail = split_node;
672
- self . length = at;
592
+ self . tail = split_node;
593
+ self . len = at;
673
594
674
595
second_part
675
596
}
@@ -729,129 +650,100 @@ impl<T> LinkedList<T> {
729
650
impl < T > Drop for LinkedList < T > {
730
651
#[ unsafe_destructor_blind_to_params]
731
652
fn drop ( & mut self ) {
732
- // Dissolve the linked_list in a loop.
733
- // Just dropping the list_head can lead to stack exhaustion
734
- // when length is >> 1_000_000
735
- while let Some ( mut head_) = self . list_head . take ( ) {
736
- self . list_head = head_. next . take ( ) ;
737
- }
738
- self . length = 0 ;
739
- self . list_tail = Rawlink :: none ( ) ;
653
+ while let Some ( _) = self . pop_front_node ( ) { }
740
654
}
741
655
}
742
656
743
657
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
744
- impl < ' a , A > Iterator for Iter < ' a , A > {
745
- type Item = & ' a A ;
658
+ impl < ' a , T > Iterator for Iter < ' a , T > {
659
+ type Item = & ' a T ;
746
660
747
661
#[ inline]
748
- fn next ( & mut self ) -> Option < & ' a A > {
749
- if self . nelem == 0 {
750
- return None ;
662
+ fn next ( & mut self ) -> Option < & ' a T > {
663
+ if self . len == 0 {
664
+ None
665
+ } else {
666
+ self . head . map ( |node| unsafe {
667
+ let node = & * * node;
668
+ self . len -= 1 ;
669
+ self . head = node. next ;
670
+ & node. element
671
+ } )
751
672
}
752
- self . head . as_ref ( ) . map ( |head| {
753
- self . nelem -= 1 ;
754
- self . head = & head. next ;
755
- & head. value
756
- } )
757
673
}
758
674
759
675
#[ inline]
760
676
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
761
- ( self . nelem , Some ( self . nelem ) )
677
+ ( self . len , Some ( self . len ) )
762
678
}
763
679
}
764
680
765
681
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
766
- impl < ' a , A > DoubleEndedIterator for Iter < ' a , A > {
682
+ impl < ' a , T > DoubleEndedIterator for Iter < ' a , T > {
767
683
#[ inline]
768
- fn next_back ( & mut self ) -> Option < & ' a A > {
769
- if self . nelem == 0 {
770
- return None ;
771
- }
772
- unsafe {
773
- self . tail . resolve ( ) . map ( |prev| {
774
- self . nelem -= 1 ;
775
- self . tail = prev . prev ;
776
- & prev . value
684
+ fn next_back ( & mut self ) -> Option < & ' a T > {
685
+ if self . len == 0 {
686
+ None
687
+ } else {
688
+ self . tail . map ( |node| unsafe {
689
+ let node = & * * node ;
690
+ self . len -= 1 ;
691
+ self . tail = node . prev ;
692
+ & node . element
777
693
} )
778
694
}
779
695
}
780
696
}
781
697
782
698
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
783
- impl < ' a , A > ExactSizeIterator for Iter < ' a , A > { }
699
+ impl < ' a , T > ExactSizeIterator for Iter < ' a , T > { }
784
700
785
701
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
786
- impl < ' a , A > Iterator for IterMut < ' a , A > {
787
- type Item = & ' a mut A ;
702
+ impl < ' a , T > Iterator for IterMut < ' a , T > {
703
+ type Item = & ' a mut T ;
704
+
788
705
#[ inline]
789
- fn next ( & mut self ) -> Option < & ' a mut A > {
790
- if self . nelem == 0 {
791
- return None ;
792
- }
793
- unsafe {
794
- self . head . resolve_mut ( ) . map ( |next| {
795
- self . nelem -= 1 ;
796
- self . head = Rawlink :: from ( & mut next . next ) ;
797
- & mut next . value
706
+ fn next ( & mut self ) -> Option < & ' a mut T > {
707
+ if self . len == 0 {
708
+ None
709
+ } else {
710
+ self . head . map ( |node| unsafe {
711
+ let node = & mut * * node ;
712
+ self . len -= 1 ;
713
+ self . head = node . next ;
714
+ & mut node . element
798
715
} )
799
716
}
800
717
}
801
718
802
719
#[ inline]
803
720
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
804
- ( self . nelem , Some ( self . nelem ) )
721
+ ( self . len , Some ( self . len ) )
805
722
}
806
723
}
807
724
808
725
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
809
- impl < ' a , A > DoubleEndedIterator for IterMut < ' a , A > {
726
+ impl < ' a , T > DoubleEndedIterator for IterMut < ' a , T > {
810
727
#[ inline]
811
- fn next_back ( & mut self ) -> Option < & ' a mut A > {
812
- if self . nelem == 0 {
813
- return None ;
814
- }
815
- unsafe {
816
- self . tail . resolve_mut ( ) . map ( |prev| {
817
- self . nelem -= 1 ;
818
- self . tail = prev . prev ;
819
- & mut prev . value
728
+ fn next_back ( & mut self ) -> Option < & ' a mut T > {
729
+ if self . len == 0 {
730
+ None
731
+ } else {
732
+ self . tail . map ( |node| unsafe {
733
+ let node = & mut * * node ;
734
+ self . len -= 1 ;
735
+ self . tail = node . prev ;
736
+ & mut node . element
820
737
} )
821
738
}
822
739
}
823
740
}
824
741
825
742
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
826
- impl < ' a , A > ExactSizeIterator for IterMut < ' a , A > { }
827
-
828
- // private methods for IterMut
829
- impl < ' a , A > IterMut < ' a , A > {
830
- fn insert_next_node ( & mut self , mut ins_node : Box < Node < A > > ) {
831
- // Insert before `self.head` so that it is between the
832
- // previously yielded element and self.head.
833
- //
834
- // The inserted node will not appear in further iteration.
835
- match unsafe { self . head . resolve_mut ( ) } {
836
- None => {
837
- self . list . push_back_node ( ins_node) ;
838
- }
839
- Some ( node) => {
840
- let prev_node = match unsafe { node. prev . resolve_mut ( ) } {
841
- None => return self . list . push_front_node ( ins_node) ,
842
- Some ( prev) => prev,
843
- } ;
844
- let node_own = prev_node. next . take ( ) . unwrap ( ) ;
845
- ins_node. set_next ( node_own) ;
846
- prev_node. set_next ( ins_node) ;
847
- self . list . length += 1 ;
848
- }
849
- }
850
- }
851
- }
743
+ impl < ' a , T > ExactSizeIterator for IterMut < ' a , T > { }
852
744
853
- impl < ' a , A > IterMut < ' a , A > {
854
- /// Inserts `elt` just after the element most recently returned by `.next()`.
745
+ impl < ' a , T > IterMut < ' a , T > {
746
+ /// Inserts the given element just after the element most recently returned by `.next()`.
855
747
/// The inserted element does not appear in the iteration.
856
748
///
857
749
/// # Examples
@@ -878,8 +770,27 @@ impl<'a, A> IterMut<'a, A> {
878
770
#[ unstable( feature = "linked_list_extras" ,
879
771
reason = "this is probably better handled by a cursor type -- we'll see" ,
880
772
issue = "27794" ) ]
881
- pub fn insert_next ( & mut self , elt : A ) {
882
- self . insert_next_node ( box Node :: new ( elt) )
773
+ pub fn insert_next ( & mut self , element : T ) {
774
+ match self . head {
775
+ None => self . list . push_back ( element) ,
776
+ Some ( head) => unsafe {
777
+ let prev = match ( * * head) . prev {
778
+ None => return self . list . push_front ( element) ,
779
+ Some ( prev) => prev,
780
+ } ;
781
+
782
+ let node = Some ( Shared :: new ( Box :: into_raw ( box Node {
783
+ next : Some ( head) ,
784
+ prev : Some ( prev) ,
785
+ element : element,
786
+ } ) ) ) ;
787
+
788
+ ( * * prev) . next = node;
789
+ ( * * head) . prev = node;
790
+
791
+ self . list . len += 1 ;
792
+ }
793
+ }
883
794
}
884
795
885
796
/// Provides a reference to the next element, without changing the iterator.
@@ -903,46 +814,47 @@ impl<'a, A> IterMut<'a, A> {
903
814
#[ unstable( feature = "linked_list_extras" ,
904
815
reason = "this is probably better handled by a cursor type -- we'll see" ,
905
816
issue = "27794" ) ]
906
- pub fn peek_next ( & mut self ) -> Option < & mut A > {
907
- if self . nelem == 0 {
908
- return None ;
817
+ pub fn peek_next ( & mut self ) -> Option < & mut T > {
818
+ if self . len == 0 {
819
+ None
820
+ } else {
821
+ self . head . map ( |node| unsafe { & mut ( * * node) . element } )
909
822
}
910
- unsafe { self . head . resolve_mut ( ) . map ( |head| & mut head. value ) }
911
823
}
912
824
}
913
825
914
826
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
915
- impl < A > Iterator for IntoIter < A > {
916
- type Item = A ;
827
+ impl < T > Iterator for IntoIter < T > {
828
+ type Item = T ;
917
829
918
830
#[ inline]
919
- fn next ( & mut self ) -> Option < A > {
831
+ fn next ( & mut self ) -> Option < T > {
920
832
self . list . pop_front ( )
921
833
}
922
834
923
835
#[ inline]
924
836
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
925
- ( self . list . length , Some ( self . list . length ) )
837
+ ( self . list . len , Some ( self . list . len ) )
926
838
}
927
839
}
928
840
929
841
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
930
- impl < A > DoubleEndedIterator for IntoIter < A > {
842
+ impl < T > DoubleEndedIterator for IntoIter < T > {
931
843
#[ inline]
932
- fn next_back ( & mut self ) -> Option < A > {
844
+ fn next_back ( & mut self ) -> Option < T > {
933
845
self . list . pop_back ( )
934
846
}
935
847
}
936
848
937
849
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
938
- impl < A > ExactSizeIterator for IntoIter < A > { }
850
+ impl < T > ExactSizeIterator for IntoIter < T > { }
939
851
940
852
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
941
- impl < A > FromIterator < A > for LinkedList < A > {
942
- fn from_iter < T : IntoIterator < Item = A > > ( iter : T ) -> LinkedList < A > {
943
- let mut ret = LinkedList :: new ( ) ;
944
- ret . extend ( iter) ;
945
- ret
853
+ impl < T > FromIterator < T > for LinkedList < T > {
854
+ fn from_iter < I : IntoIterator < Item = T > > ( iter : I ) -> Self {
855
+ let mut list = Self :: new ( ) ;
856
+ list . extend ( iter) ;
857
+ list
946
858
}
947
859
}
948
860
@@ -973,15 +885,15 @@ impl<'a, T> IntoIterator for &'a mut LinkedList<T> {
973
885
type Item = & ' a mut T ;
974
886
type IntoIter = IterMut < ' a , T > ;
975
887
976
- fn into_iter ( mut self ) -> IterMut < ' a , T > {
888
+ fn into_iter ( self ) -> IterMut < ' a , T > {
977
889
self . iter_mut ( )
978
890
}
979
891
}
980
892
981
893
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
982
- impl < A > Extend < A > for LinkedList < A > {
983
- fn extend < T : IntoIterator < Item = A > > ( & mut self , iter : T ) {
984
- <Self as SpecExtend < T > >:: spec_extend ( self , iter) ;
894
+ impl < T > Extend < T > for LinkedList < T > {
895
+ fn extend < I : IntoIterator < Item = T > > ( & mut self , iter : I ) {
896
+ <Self as SpecExtend < I > >:: spec_extend ( self , iter) ;
985
897
}
986
898
}
987
899
@@ -1007,50 +919,50 @@ impl<'a, T: 'a + Copy> Extend<&'a T> for LinkedList<T> {
1007
919
}
1008
920
1009
921
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1010
- impl < A : PartialEq > PartialEq for LinkedList < A > {
1011
- fn eq ( & self , other : & LinkedList < A > ) -> bool {
1012
- self . len ( ) == other. len ( ) && self . iter ( ) . eq ( other. iter ( ) )
922
+ impl < T : PartialEq > PartialEq for LinkedList < T > {
923
+ fn eq ( & self , other : & Self ) -> bool {
924
+ self . len ( ) == other. len ( ) && self . iter ( ) . eq ( other)
1013
925
}
1014
926
1015
- fn ne ( & self , other : & LinkedList < A > ) -> bool {
1016
- self . len ( ) != other. len ( ) || self . iter ( ) . ne ( other. iter ( ) )
927
+ fn ne ( & self , other : & Self ) -> bool {
928
+ self . len ( ) != other. len ( ) || self . iter ( ) . ne ( other)
1017
929
}
1018
930
}
1019
931
1020
932
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1021
- impl < A : Eq > Eq for LinkedList < A > { }
933
+ impl < T : Eq > Eq for LinkedList < T > { }
1022
934
1023
935
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1024
- impl < A : PartialOrd > PartialOrd for LinkedList < A > {
1025
- fn partial_cmp ( & self , other : & LinkedList < A > ) -> Option < Ordering > {
1026
- self . iter ( ) . partial_cmp ( other. iter ( ) )
936
+ impl < T : PartialOrd > PartialOrd for LinkedList < T > {
937
+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
938
+ self . iter ( ) . partial_cmp ( other)
1027
939
}
1028
940
}
1029
941
1030
942
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1031
- impl < A : Ord > Ord for LinkedList < A > {
943
+ impl < T : Ord > Ord for LinkedList < T > {
1032
944
#[ inline]
1033
- fn cmp ( & self , other : & LinkedList < A > ) -> Ordering {
1034
- self . iter ( ) . cmp ( other. iter ( ) )
945
+ fn cmp ( & self , other : & Self ) -> Ordering {
946
+ self . iter ( ) . cmp ( other)
1035
947
}
1036
948
}
1037
949
1038
950
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1039
- impl < A : Clone > Clone for LinkedList < A > {
1040
- fn clone ( & self ) -> LinkedList < A > {
951
+ impl < T : Clone > Clone for LinkedList < T > {
952
+ fn clone ( & self ) -> Self {
1041
953
self . iter ( ) . cloned ( ) . collect ( )
1042
954
}
1043
955
}
1044
956
1045
957
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1046
- impl < A : fmt:: Debug > fmt:: Debug for LinkedList < A > {
958
+ impl < T : fmt:: Debug > fmt:: Debug for LinkedList < T > {
1047
959
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1048
- f. debug_list ( ) . entries ( self . iter ( ) ) . finish ( )
960
+ f. debug_list ( ) . entries ( self ) . finish ( )
1049
961
}
1050
962
}
1051
963
1052
964
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1053
- impl < A : Hash > Hash for LinkedList < A > {
965
+ impl < T : Hash > Hash for LinkedList < T > {
1054
966
fn hash < H : Hasher > ( & self , state : & mut H ) {
1055
967
self . len ( ) . hash ( state) ;
1056
968
for elt in self {
@@ -1062,7 +974,7 @@ impl<A: Hash> Hash for LinkedList<A> {
1062
974
unsafe fn finalize < T > ( node : IntermediateBox < Node < T > > ) -> Box < Node < T > > {
1063
975
let mut node = node. finalize ( ) ;
1064
976
ptr:: write ( & mut node. next , None ) ;
1065
- ptr:: write ( & mut node. prev , Rawlink :: none ( ) ) ;
977
+ ptr:: write ( & mut node. prev , None ) ;
1066
978
node
1067
979
}
1068
980
@@ -1094,7 +1006,7 @@ impl<'a, T> Placer<T> for FrontPlace<'a, T> {
1094
1006
issue = "30172" ) ]
1095
1007
impl < ' a , T > Place < T > for FrontPlace < ' a , T > {
1096
1008
fn pointer ( & mut self ) -> * mut T {
1097
- unsafe { & mut ( * self . node . pointer ( ) ) . value }
1009
+ unsafe { & mut ( * self . node . pointer ( ) ) . element }
1098
1010
}
1099
1011
}
1100
1012
@@ -1138,7 +1050,7 @@ impl<'a, T> Placer<T> for BackPlace<'a, T> {
1138
1050
issue = "30172" ) ]
1139
1051
impl < ' a , T > Place < T > for BackPlace < ' a , T > {
1140
1052
fn pointer ( & mut self ) -> * mut T {
1141
- unsafe { & mut ( * self . node . pointer ( ) ) . value }
1053
+ unsafe { & mut ( * self . node . pointer ( ) ) . element }
1142
1054
}
1143
1055
}
1144
1056
@@ -1162,6 +1074,24 @@ fn assert_covariance() {
1162
1074
fn c < ' a > ( x : IntoIter < & ' static str > ) -> IntoIter < & ' a str > { x }
1163
1075
}
1164
1076
1077
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1078
+ unsafe impl < T : Send > Send for LinkedList < T > { }
1079
+
1080
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1081
+ unsafe impl < T : Sync > Sync for LinkedList < T > { }
1082
+
1083
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1084
+ unsafe impl < ' a , T : Sync > Send for Iter < ' a , T > { }
1085
+
1086
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1087
+ unsafe impl < ' a , T : Sync > Sync for Iter < ' a , T > { }
1088
+
1089
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1090
+ unsafe impl < ' a , T : Send > Send for IterMut < ' a , T > { }
1091
+
1092
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1093
+ unsafe impl < ' a , T : Sync > Sync for IterMut < ' a , T > { }
1094
+
1165
1095
#[ cfg( test) ]
1166
1096
mod tests {
1167
1097
use std:: clone:: Clone ;
@@ -1179,38 +1109,40 @@ mod tests {
1179
1109
}
1180
1110
1181
1111
pub fn check_links < T > ( list : & LinkedList < T > ) {
1182
- let mut len = 0 ;
1183
- let mut last_ptr: Option < & Node < T > > = None ;
1184
- let mut node_ptr: & Node < T > ;
1185
- match list. list_head {
1186
- None => {
1187
- assert_eq ! ( 0 , list. length) ;
1188
- return ;
1189
- }
1190
- Some ( ref node) => node_ptr = & * * node,
1191
- }
1192
- loop {
1193
- match unsafe { ( last_ptr, node_ptr. prev . resolve ( ) ) } {
1194
- ( None , None ) => { }
1195
- ( None , _) => panic ! ( "prev link for list_head" ) ,
1196
- ( Some ( p) , Some ( pptr) ) => {
1197
- assert_eq ! ( p as * const Node <T >, pptr as * const Node <T >) ;
1112
+ unsafe {
1113
+ let mut len = 0 ;
1114
+ let mut last_ptr: Option < & Node < T > > = None ;
1115
+ let mut node_ptr: & Node < T > ;
1116
+ match list. head {
1117
+ None => {
1118
+ assert_eq ! ( 0 , list. len) ;
1119
+ return ;
1198
1120
}
1199
- _ => panic ! ( "prev link is none, not good" ) ,
1121
+ Some ( node ) => node_ptr = & * * node ,
1200
1122
}
1201
- match node_ptr. next {
1202
- Some ( ref next) => {
1203
- last_ptr = Some ( node_ptr) ;
1204
- node_ptr = & * * next;
1205
- len += 1 ;
1123
+ loop {
1124
+ match ( last_ptr, node_ptr. prev ) {
1125
+ ( None , None ) => { }
1126
+ ( None , _) => panic ! ( "prev link for head" ) ,
1127
+ ( Some ( p) , Some ( pptr) ) => {
1128
+ assert_eq ! ( p as * const Node <T >, * pptr as * const Node <T >) ;
1129
+ }
1130
+ _ => panic ! ( "prev link is none, not good" ) ,
1206
1131
}
1207
- None => {
1208
- len += 1 ;
1209
- break ;
1132
+ match node_ptr. next {
1133
+ Some ( next) => {
1134
+ last_ptr = Some ( node_ptr) ;
1135
+ node_ptr = & * * next;
1136
+ len += 1 ;
1137
+ }
1138
+ None => {
1139
+ len += 1 ;
1140
+ break ;
1141
+ }
1210
1142
}
1211
1143
}
1144
+ assert_eq ! ( len, list. len) ;
1212
1145
}
1213
- assert_eq ! ( len, list. length) ;
1214
1146
}
1215
1147
1216
1148
#[ test]
@@ -1359,7 +1291,6 @@ mod tests {
1359
1291
}
1360
1292
}
1361
1293
1362
-
1363
1294
#[ cfg( test) ]
1364
1295
fn fuzz_test ( sz : i32 ) {
1365
1296
let mut m: LinkedList < _ > = LinkedList :: new ( ) ;
0 commit comments