Skip to content

Commit 362b665

Browse files
authoredJul 13, 2016
Auto merge of #34608 - apasel422:ll, r=bluss
Replace `LinkedList`'s use of `Box` with `Shared` Closes #34417
·
1.88.01.12.0
2 parents 2ab18ce + 6d3bf6e commit 362b665

File tree

1 file changed

+283
-352
lines changed

1 file changed

+283
-352
lines changed
 

‎src/libcollections/linked_list.rs

Lines changed: 283 additions & 352 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,14 @@
1313
//! The `LinkedList` allows pushing and popping elements at either end and is thus
1414
//! efficiently usable as a double-ended queue.
1515
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-
2216
#![stable(feature = "rust1", since = "1.0.0")]
2317

2418
use alloc::boxed::{Box, IntermediateBox};
2519
use core::cmp::Ordering;
2620
use core::fmt;
2721
use core::hash::{Hasher, Hash};
2822
use core::iter::FromIterator;
23+
use core::marker::PhantomData;
2924
use core::mem;
3025
use core::ops::{BoxPlace, InPlace, Place, Placer};
3126
use core::ptr::{self, Shared};
@@ -35,222 +30,156 @@ use super::SpecExtend;
3530
/// A doubly-linked list.
3631
#[stable(feature = "rust1", since = "1.0.0")]
3732
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>>>,
4737
}
4838

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-
5339
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,
5743
}
5844

59-
/// An iterator over references to the items of a `LinkedList`.
45+
/// An iterator over references to the elements of a `LinkedList`.
6046
#[stable(feature = "rust1", since = "1.0.0")]
6147
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>>,
6552
}
6653

6754
// FIXME #19839: deriving is too aggressive on the bounds (T doesn't need to be Clone).
6855
#[stable(feature = "rust1", since = "1.0.0")]
6956
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 }
7659
}
7760
}
7861

79-
/// An iterator over mutable references to the items of a `LinkedList`.
62+
/// An iterator over mutable references to the elements of a `LinkedList`.
8063
#[stable(feature = "rust1", since = "1.0.0")]
8164
pub struct IterMut<'a, T: 'a> {
8265
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,
8669
}
8770

88-
/// An iterator over the items of a `LinkedList`.
71+
/// An iterator over the elements of a `LinkedList`.
8972
#[derive(Clone)]
9073
#[stable(feature = "rust1", since = "1.0.0")]
9174
pub struct IntoIter<T> {
9275
list: LinkedList<T>,
9376
}
9477

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-
14978
impl<T> Node<T> {
150-
fn new(v: T) -> Node<T> {
79+
fn new(element: T) -> Self {
15180
Node {
152-
value: v,
15381
next: None,
154-
prev: Rawlink::none(),
82+
prev: None,
83+
element: element,
15584
}
15685
}
15786

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
16689
}
16790
}
16891

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-
17592
// private methods
17693
impl<T> LinkedList<T> {
177-
/// Add a Node first in the list
94+
/// Adds the given node to the front of the list.
17895
#[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,
190105
}
106+
107+
self.head = node;
108+
self.len += 1;
191109
}
192-
self.length += 1;
193110
}
194111

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.
196113
#[inline]
197114
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,
203122
}
204-
front_node
123+
124+
self.len -= 1;
125+
node
205126
})
206127
}
207128

208-
/// Add a Node last in the list
129+
/// Adds the given node to the back of the list.
209130
#[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,
216140
}
141+
142+
self.tail = node;
143+
self.len += 1;
217144
}
218-
self.length += 1;
219145
}
220146

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.
222148
#[inline]
223149
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+
})
234162
}
235163
}
236164

237165
#[stable(feature = "rust1", since = "1.0.0")]
238166
impl<T> Default for LinkedList<T> {
239167
#[inline]
240-
fn default() -> LinkedList<T> {
241-
LinkedList::new()
168+
fn default() -> Self {
169+
Self::new()
242170
}
243171
}
244172

245173
impl<T> LinkedList<T> {
246174
/// Creates an empty `LinkedList`.
247175
#[inline]
248176
#[stable(feature = "rust1", since = "1.0.0")]
249-
pub fn new() -> LinkedList<T> {
177+
pub fn new() -> Self {
250178
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,
254183
}
255184
}
256185

@@ -281,38 +210,30 @@ impl<T> LinkedList<T> {
281210
/// println!("{}", b.len()); // prints 0
282211
/// ```
283212
#[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);
302220
}
303-
}
221+
222+
self.tail = other.tail.take();
223+
self.len += mem::replace(&mut other.len, 0);
224+
},
304225
}
305-
other.length = 0;
306226
}
307227

308228
/// Provides a forward iterator.
309229
#[inline]
310230
#[stable(feature = "rust1", since = "1.0.0")]
311231
pub fn iter(&self) -> Iter<T> {
312232
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,
316237
}
317238
}
318239

@@ -321,9 +242,9 @@ impl<T> LinkedList<T> {
321242
#[stable(feature = "rust1", since = "1.0.0")]
322243
pub fn iter_mut(&mut self) -> IterMut<T> {
323244
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,
327248
list: self,
328249
}
329250
}
@@ -346,7 +267,7 @@ impl<T> LinkedList<T> {
346267
#[inline]
347268
#[stable(feature = "rust1", since = "1.0.0")]
348269
pub fn is_empty(&self) -> bool {
349-
self.list_head.is_none()
270+
self.head.is_none()
350271
}
351272

352273
/// Returns the length of the `LinkedList`.
@@ -373,7 +294,7 @@ impl<T> LinkedList<T> {
373294
#[inline]
374295
#[stable(feature = "rust1", since = "1.0.0")]
375296
pub fn len(&self) -> usize {
376-
self.length
297+
self.len
377298
}
378299

379300
/// Removes all elements from the `LinkedList`.
@@ -400,7 +321,7 @@ impl<T> LinkedList<T> {
400321
#[inline]
401322
#[stable(feature = "rust1", since = "1.0.0")]
402323
pub fn clear(&mut self) {
403-
*self = LinkedList::new()
324+
*self = Self::new();
404325
}
405326

406327
/// Returns `true` if the `LinkedList` contains an element equal to the
@@ -431,7 +352,7 @@ impl<T> LinkedList<T> {
431352
#[inline]
432353
#[stable(feature = "rust1", since = "1.0.0")]
433354
pub fn front(&self) -> Option<&T> {
434-
self.list_head.as_ref().map(|head| &head.value)
355+
self.head.map(|node| unsafe { &(**node).element })
435356
}
436357

437358
/// Provides a mutable reference to the front element, or `None` if the list
@@ -458,7 +379,7 @@ impl<T> LinkedList<T> {
458379
#[inline]
459380
#[stable(feature = "rust1", since = "1.0.0")]
460381
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 })
462383
}
463384

464385
/// Provides a reference to the back element, or `None` if the list is
@@ -479,7 +400,7 @@ impl<T> LinkedList<T> {
479400
#[inline]
480401
#[stable(feature = "rust1", since = "1.0.0")]
481402
pub fn back(&self) -> Option<&T> {
482-
unsafe { self.list_tail.resolve().map(|tail| &tail.value) }
403+
self.tail.map(|node| unsafe { &(**node).element })
483404
}
484405

485406
/// Provides a mutable reference to the back element, or `None` if the list
@@ -506,7 +427,7 @@ impl<T> LinkedList<T> {
506427
#[inline]
507428
#[stable(feature = "rust1", since = "1.0.0")]
508429
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 })
510431
}
511432

512433
/// Adds an element first in the list.
@@ -529,7 +450,7 @@ impl<T> LinkedList<T> {
529450
/// ```
530451
#[stable(feature = "rust1", since = "1.0.0")]
531452
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));
533454
}
534455

535456
/// Removes the first element and returns it, or `None` if the list is
@@ -555,7 +476,7 @@ impl<T> LinkedList<T> {
555476
///
556477
#[stable(feature = "rust1", since = "1.0.0")]
557478
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)
559480
}
560481

561482
/// Appends an element to the back of a list
@@ -572,7 +493,7 @@ impl<T> LinkedList<T> {
572493
/// ```
573494
#[stable(feature = "rust1", since = "1.0.0")]
574495
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));
576497
}
577498

578499
/// Removes the last element from a list and returns it, or `None` if
@@ -591,7 +512,7 @@ impl<T> LinkedList<T> {
591512
/// ```
592513
#[stable(feature = "rust1", since = "1.0.0")]
593514
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)
595516
}
596517

597518
/// Splits the list into two at the given index. Returns everything after the given index,
@@ -624,14 +545,14 @@ impl<T> LinkedList<T> {
624545
let len = self.len();
625546
assert!(at <= len, "Cannot split off at a nonexistent index");
626547
if at == 0 {
627-
return mem::replace(self, LinkedList::new());
548+
return mem::replace(self, Self::new());
628549
} else if at == len {
629-
return LinkedList::new();
550+
return Self::new();
630551
}
631552

632553
// Below, we iterate towards the `i-1`th node, either from the start or the end,
633554
// 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) {
635556
let mut iter = self.iter_mut();
636557
// instead of skipping using .skip() (which creates a new struct),
637558
// we skip manually so we can access the head field without
@@ -651,25 +572,25 @@ impl<T> LinkedList<T> {
651572

652573
// The split node is the new tail node of the first part and owns
653574
// the head of the second part.
654-
let mut second_part_head;
575+
let second_part_head;
655576

656577
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;
661581
}
662582
}
663583

664584
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,
668589
};
669590

670591
// 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;
673594

674595
second_part
675596
}
@@ -729,129 +650,100 @@ impl<T> LinkedList<T> {
729650
impl<T> Drop for LinkedList<T> {
730651
#[unsafe_destructor_blind_to_params]
731652
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() {}
740654
}
741655
}
742656

743657
#[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;
746660

747661
#[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+
})
751672
}
752-
self.head.as_ref().map(|head| {
753-
self.nelem -= 1;
754-
self.head = &head.next;
755-
&head.value
756-
})
757673
}
758674

759675
#[inline]
760676
fn size_hint(&self) -> (usize, Option<usize>) {
761-
(self.nelem, Some(self.nelem))
677+
(self.len, Some(self.len))
762678
}
763679
}
764680

765681
#[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> {
767683
#[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
777693
})
778694
}
779695
}
780696
}
781697

782698
#[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> {}
784700

785701
#[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+
788705
#[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
798715
})
799716
}
800717
}
801718

802719
#[inline]
803720
fn size_hint(&self) -> (usize, Option<usize>) {
804-
(self.nelem, Some(self.nelem))
721+
(self.len, Some(self.len))
805722
}
806723
}
807724

808725
#[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> {
810727
#[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
820737
})
821738
}
822739
}
823740
}
824741

825742
#[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> {}
852744

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()`.
855747
/// The inserted element does not appear in the iteration.
856748
///
857749
/// # Examples
@@ -878,8 +770,27 @@ impl<'a, A> IterMut<'a, A> {
878770
#[unstable(feature = "linked_list_extras",
879771
reason = "this is probably better handled by a cursor type -- we'll see",
880772
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+
}
883794
}
884795

885796
/// Provides a reference to the next element, without changing the iterator.
@@ -903,46 +814,47 @@ impl<'a, A> IterMut<'a, A> {
903814
#[unstable(feature = "linked_list_extras",
904815
reason = "this is probably better handled by a cursor type -- we'll see",
905816
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 })
909822
}
910-
unsafe { self.head.resolve_mut().map(|head| &mut head.value) }
911823
}
912824
}
913825

914826
#[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;
917829

918830
#[inline]
919-
fn next(&mut self) -> Option<A> {
831+
fn next(&mut self) -> Option<T> {
920832
self.list.pop_front()
921833
}
922834

923835
#[inline]
924836
fn size_hint(&self) -> (usize, Option<usize>) {
925-
(self.list.length, Some(self.list.length))
837+
(self.list.len, Some(self.list.len))
926838
}
927839
}
928840

929841
#[stable(feature = "rust1", since = "1.0.0")]
930-
impl<A> DoubleEndedIterator for IntoIter<A> {
842+
impl<T> DoubleEndedIterator for IntoIter<T> {
931843
#[inline]
932-
fn next_back(&mut self) -> Option<A> {
844+
fn next_back(&mut self) -> Option<T> {
933845
self.list.pop_back()
934846
}
935847
}
936848

937849
#[stable(feature = "rust1", since = "1.0.0")]
938-
impl<A> ExactSizeIterator for IntoIter<A> {}
850+
impl<T> ExactSizeIterator for IntoIter<T> {}
939851

940852
#[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
946858
}
947859
}
948860

@@ -973,15 +885,15 @@ impl<'a, T> IntoIterator for &'a mut LinkedList<T> {
973885
type Item = &'a mut T;
974886
type IntoIter = IterMut<'a, T>;
975887

976-
fn into_iter(mut self) -> IterMut<'a, T> {
888+
fn into_iter(self) -> IterMut<'a, T> {
977889
self.iter_mut()
978890
}
979891
}
980892

981893
#[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);
985897
}
986898
}
987899

@@ -1007,50 +919,50 @@ impl<'a, T: 'a + Copy> Extend<&'a T> for LinkedList<T> {
1007919
}
1008920

1009921
#[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)
1013925
}
1014926

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)
1017929
}
1018930
}
1019931

1020932
#[stable(feature = "rust1", since = "1.0.0")]
1021-
impl<A: Eq> Eq for LinkedList<A> {}
933+
impl<T: Eq> Eq for LinkedList<T> {}
1022934

1023935
#[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)
1027939
}
1028940
}
1029941

1030942
#[stable(feature = "rust1", since = "1.0.0")]
1031-
impl<A: Ord> Ord for LinkedList<A> {
943+
impl<T: Ord> Ord for LinkedList<T> {
1032944
#[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)
1035947
}
1036948
}
1037949

1038950
#[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 {
1041953
self.iter().cloned().collect()
1042954
}
1043955
}
1044956

1045957
#[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> {
1047959
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1048-
f.debug_list().entries(self.iter()).finish()
960+
f.debug_list().entries(self).finish()
1049961
}
1050962
}
1051963

1052964
#[stable(feature = "rust1", since = "1.0.0")]
1053-
impl<A: Hash> Hash for LinkedList<A> {
965+
impl<T: Hash> Hash for LinkedList<T> {
1054966
fn hash<H: Hasher>(&self, state: &mut H) {
1055967
self.len().hash(state);
1056968
for elt in self {
@@ -1062,7 +974,7 @@ impl<A: Hash> Hash for LinkedList<A> {
1062974
unsafe fn finalize<T>(node: IntermediateBox<Node<T>>) -> Box<Node<T>> {
1063975
let mut node = node.finalize();
1064976
ptr::write(&mut node.next, None);
1065-
ptr::write(&mut node.prev, Rawlink::none());
977+
ptr::write(&mut node.prev, None);
1066978
node
1067979
}
1068980

@@ -1094,7 +1006,7 @@ impl<'a, T> Placer<T> for FrontPlace<'a, T> {
10941006
issue = "30172")]
10951007
impl<'a, T> Place<T> for FrontPlace<'a, T> {
10961008
fn pointer(&mut self) -> *mut T {
1097-
unsafe { &mut (*self.node.pointer()).value }
1009+
unsafe { &mut (*self.node.pointer()).element }
10981010
}
10991011
}
11001012

@@ -1138,7 +1050,7 @@ impl<'a, T> Placer<T> for BackPlace<'a, T> {
11381050
issue = "30172")]
11391051
impl<'a, T> Place<T> for BackPlace<'a, T> {
11401052
fn pointer(&mut self) -> *mut T {
1141-
unsafe { &mut (*self.node.pointer()).value }
1053+
unsafe { &mut (*self.node.pointer()).element }
11421054
}
11431055
}
11441056

@@ -1162,6 +1074,24 @@ fn assert_covariance() {
11621074
fn c<'a>(x: IntoIter<&'static str>) -> IntoIter<&'a str> { x }
11631075
}
11641076

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+
11651095
#[cfg(test)]
11661096
mod tests {
11671097
use std::clone::Clone;
@@ -1179,38 +1109,40 @@ mod tests {
11791109
}
11801110

11811111
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;
11981120
}
1199-
_ => panic!("prev link is none, not good"),
1121+
Some(node) => node_ptr = &**node,
12001122
}
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"),
12061131
}
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+
}
12101142
}
12111143
}
1144+
assert_eq!(len, list.len);
12121145
}
1213-
assert_eq!(len, list.length);
12141146
}
12151147

12161148
#[test]
@@ -1359,7 +1291,6 @@ mod tests {
13591291
}
13601292
}
13611293

1362-
13631294
#[cfg(test)]
13641295
fn fuzz_test(sz: i32) {
13651296
let mut m: LinkedList<_> = LinkedList::new();

0 commit comments

Comments
 (0)
Please sign in to comment.