1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A doubly-linked list with owned nodes.
13 //! The `LinkedList` allows pushing and popping elements at either end and is thus
14 //! efficiently usable as a double-ended queue.
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.
19 // Backlinks over LinkedList::prev are raw pointers that form a full chain in
20 // the reverse direction.
22 #![stable(feature = "rust1", since = "1.0.0")]
24 use alloc::boxed::Box;
25 use core::cmp::Ordering;
27 use core::hash::{Hasher, Hash};
28 use core::iter::FromIterator;
30 use core::ptr::Shared;
32 /// A doubly-linked list.
33 #[stable(feature = "rust1", since = "1.0.0")]
34 pub struct LinkedList<T> {
37 list_tail: Rawlink<Node<T>>,
40 type Link<T> = Option<Box<Node<T>>>;
46 impl<T> Copy for Rawlink<T> {}
47 unsafe impl<T: Send> Send for Rawlink<T> {}
48 unsafe impl<T: Sync> Sync for Rawlink<T> {}
52 prev: Rawlink<Node<T>>,
56 /// An iterator over references to the items of a `LinkedList`.
57 #[stable(feature = "rust1", since = "1.0.0")]
58 pub struct Iter<'a, T: 'a> {
60 tail: Rawlink<Node<T>>,
64 // FIXME #19839: deriving is too aggressive on the bounds (T doesn't need to be Clone).
65 #[stable(feature = "rust1", since = "1.0.0")]
66 impl<'a, T> Clone for Iter<'a, T> {
67 fn clone(&self) -> Iter<'a, T> {
69 head: self.head.clone(),
76 /// An iterator over mutable references to the items of a `LinkedList`.
77 #[stable(feature = "rust1", since = "1.0.0")]
78 pub struct IterMut<'a, T: 'a> {
79 list: &'a mut LinkedList<T>,
80 head: Rawlink<Node<T>>,
81 tail: Rawlink<Node<T>>,
85 /// An iterator over mutable references to the items of a `LinkedList`.
87 #[stable(feature = "rust1", since = "1.0.0")]
88 pub struct IntoIter<T> {
92 /// Rawlink is a type like Option<T> but for holding a raw pointer
94 /// Like Option::None for Rawlink
95 fn none() -> Rawlink<T> {
99 /// Like Option::Some for Rawlink
100 fn some(n: &mut T) -> Rawlink<T> {
101 unsafe { Rawlink { p: Some(Shared::new(n)) } }
104 /// Convert the `Rawlink` into an Option value
106 /// **unsafe** because:
108 /// - Dereference of raw pointer.
109 /// - Returns reference of arbitrary lifetime.
110 unsafe fn resolve<'a>(&self) -> Option<&'a T> {
114 /// Convert the `Rawlink` into an Option value
116 /// **unsafe** because:
118 /// - Dereference of raw pointer.
119 /// - Returns reference of arbitrary lifetime.
120 unsafe fn resolve_mut<'a>(&mut self) -> Option<&'a mut T> {
121 self.p.map(|p| &mut **p)
124 /// Return the `Rawlink` and replace with `Rawlink::none()`
125 fn take(&mut self) -> Rawlink<T> {
126 mem::replace(self, Rawlink::none())
130 impl<'a, T> From<&'a mut Link<T>> for Rawlink<Node<T>> {
131 fn from(node: &'a mut Link<T>) -> Self {
132 match node.as_mut() {
133 None => Rawlink::none(),
134 Some(ptr) => Rawlink::some(ptr),
139 impl<T> Clone for Rawlink<T> {
141 fn clone(&self) -> Rawlink<T> {
142 Rawlink { p: self.p }
147 fn new(v: T) -> Node<T> {
151 prev: Rawlink::none(),
155 /// Update the `prev` link on `next`, then set self's next pointer.
157 /// `self.next` should be `None` when you call this
158 /// (otherwise a Node is probably being dropped by mistake).
159 fn set_next(&mut self, mut next: Box<Node<T>>) {
160 debug_assert!(self.next.is_none());
161 next.prev = Rawlink::some(self);
162 self.next = Some(next);
166 /// Clear the .prev field on `next`, then return `Some(next)`
167 fn link_no_prev<T>(mut next: Box<Node<T>>) -> Link<T> {
168 next.prev = Rawlink::none();
173 impl<T> LinkedList<T> {
174 /// Add a Node first in the list
176 fn push_front_node(&mut self, mut new_head: Box<Node<T>>) {
177 match self.list_head {
179 self.list_head = link_no_prev(new_head);
180 self.list_tail = Rawlink::from(&mut self.list_head);
182 Some(ref mut head) => {
183 new_head.prev = Rawlink::none();
184 head.prev = Rawlink::some(&mut *new_head);
185 mem::swap(head, &mut new_head);
186 head.next = Some(new_head);
192 /// Remove the first Node and return it, or None if the list is empty
194 fn pop_front_node(&mut self) -> Option<Box<Node<T>>> {
195 self.list_head.take().map(|mut front_node| {
197 match front_node.next.take() {
198 Some(node) => self.list_head = link_no_prev(node),
199 None => self.list_tail = Rawlink::none(),
205 /// Add a Node last in the list
207 fn push_back_node(&mut self, new_tail: Box<Node<T>>) {
208 match unsafe { self.list_tail.resolve_mut() } {
209 None => return self.push_front_node(new_tail),
211 tail.set_next(new_tail);
212 self.list_tail = Rawlink::from(&mut tail.next);
218 /// Remove the last Node and return it, or None if the list is empty
220 fn pop_back_node(&mut self) -> Option<Box<Node<T>>> {
222 self.list_tail.resolve_mut().and_then(|tail| {
224 self.list_tail = tail.prev;
225 match tail.prev.resolve_mut() {
226 None => self.list_head.take(),
227 Some(tail_prev) => tail_prev.next.take(),
234 #[stable(feature = "rust1", since = "1.0.0")]
235 impl<T> Default for LinkedList<T> {
237 fn default() -> LinkedList<T> {
242 impl<T> LinkedList<T> {
243 /// Creates an empty `LinkedList`.
245 #[stable(feature = "rust1", since = "1.0.0")]
246 pub fn new() -> LinkedList<T> {
249 list_tail: Rawlink::none(),
254 /// Moves all elements from `other` to the end of the list.
256 /// This reuses all the nodes from `other` and moves them into `self`. After
257 /// this operation, `other` becomes empty.
259 /// This operation should compute in O(1) time and O(1) memory.
264 /// use std::collections::LinkedList;
266 /// let mut a = LinkedList::new();
267 /// let mut b = LinkedList::new();
273 /// a.append(&mut b);
276 /// println!("{}", e); // prints 1, then 2, then 3, then 4
278 /// println!("{}", b.len()); // prints 0
280 #[stable(feature = "rust1", since = "1.0.0")]
281 pub fn append(&mut self, other: &mut LinkedList<T>) {
282 match unsafe { self.list_tail.resolve_mut() } {
284 self.length = other.length;
285 self.list_head = other.list_head.take();
286 self.list_tail = other.list_tail.take();
289 // Carefully empty `other`.
290 let o_tail = other.list_tail.take();
291 let o_length = other.length;
292 match other.list_head.take() {
296 self.list_tail = o_tail;
297 self.length += o_length;
305 /// Provides a forward iterator.
307 #[stable(feature = "rust1", since = "1.0.0")]
308 pub fn iter(&self) -> Iter<T> {
311 head: &self.list_head,
312 tail: self.list_tail,
316 /// Provides a forward iterator with mutable references.
318 #[stable(feature = "rust1", since = "1.0.0")]
319 pub fn iter_mut(&mut self) -> IterMut<T> {
322 head: Rawlink::from(&mut self.list_head),
323 tail: self.list_tail,
328 /// Returns `true` if the `LinkedList` is empty.
330 /// This operation should compute in O(1) time.
335 /// use std::collections::LinkedList;
337 /// let mut dl = LinkedList::new();
338 /// assert!(dl.is_empty());
340 /// dl.push_front("foo");
341 /// assert!(!dl.is_empty());
344 #[stable(feature = "rust1", since = "1.0.0")]
345 pub fn is_empty(&self) -> bool {
346 self.list_head.is_none()
349 /// Returns the length of the `LinkedList`.
351 /// This operation should compute in O(1) time.
356 /// use std::collections::LinkedList;
358 /// let mut dl = LinkedList::new();
360 /// dl.push_front(2);
361 /// assert_eq!(dl.len(), 1);
363 /// dl.push_front(1);
364 /// assert_eq!(dl.len(), 2);
367 /// assert_eq!(dl.len(), 3);
371 #[stable(feature = "rust1", since = "1.0.0")]
372 pub fn len(&self) -> usize {
376 /// Removes all elements from the `LinkedList`.
378 /// This operation should compute in O(n) time.
383 /// use std::collections::LinkedList;
385 /// let mut dl = LinkedList::new();
387 /// dl.push_front(2);
388 /// dl.push_front(1);
389 /// assert_eq!(dl.len(), 2);
390 /// assert_eq!(dl.front(), Some(&1));
393 /// assert_eq!(dl.len(), 0);
394 /// assert_eq!(dl.front(), None);
398 #[stable(feature = "rust1", since = "1.0.0")]
399 pub fn clear(&mut self) {
400 *self = LinkedList::new()
403 /// Provides a reference to the front element, or `None` if the list is
409 /// use std::collections::LinkedList;
411 /// let mut dl = LinkedList::new();
412 /// assert_eq!(dl.front(), None);
414 /// dl.push_front(1);
415 /// assert_eq!(dl.front(), Some(&1));
419 #[stable(feature = "rust1", since = "1.0.0")]
420 pub fn front(&self) -> Option<&T> {
421 self.list_head.as_ref().map(|head| &head.value)
424 /// Provides a mutable reference to the front element, or `None` if the list
430 /// use std::collections::LinkedList;
432 /// let mut dl = LinkedList::new();
433 /// assert_eq!(dl.front(), None);
435 /// dl.push_front(1);
436 /// assert_eq!(dl.front(), Some(&1));
438 /// match dl.front_mut() {
440 /// Some(x) => *x = 5,
442 /// assert_eq!(dl.front(), Some(&5));
446 #[stable(feature = "rust1", since = "1.0.0")]
447 pub fn front_mut(&mut self) -> Option<&mut T> {
448 self.list_head.as_mut().map(|head| &mut head.value)
451 /// Provides a reference to the back element, or `None` if the list is
457 /// use std::collections::LinkedList;
459 /// let mut dl = LinkedList::new();
460 /// assert_eq!(dl.back(), None);
463 /// assert_eq!(dl.back(), Some(&1));
467 #[stable(feature = "rust1", since = "1.0.0")]
468 pub fn back(&self) -> Option<&T> {
469 unsafe { self.list_tail.resolve().map(|tail| &tail.value) }
472 /// Provides a mutable reference to the back element, or `None` if the list
478 /// use std::collections::LinkedList;
480 /// let mut dl = LinkedList::new();
481 /// assert_eq!(dl.back(), None);
484 /// assert_eq!(dl.back(), Some(&1));
486 /// match dl.back_mut() {
488 /// Some(x) => *x = 5,
490 /// assert_eq!(dl.back(), Some(&5));
494 #[stable(feature = "rust1", since = "1.0.0")]
495 pub fn back_mut(&mut self) -> Option<&mut T> {
496 unsafe { self.list_tail.resolve_mut().map(|tail| &mut tail.value) }
499 /// Adds an element first in the list.
501 /// This operation should compute in O(1) time.
506 /// use std::collections::LinkedList;
508 /// let mut dl = LinkedList::new();
510 /// dl.push_front(2);
511 /// assert_eq!(dl.front().unwrap(), &2);
513 /// dl.push_front(1);
514 /// assert_eq!(dl.front().unwrap(), &1);
517 #[stable(feature = "rust1", since = "1.0.0")]
518 pub fn push_front(&mut self, elt: T) {
519 self.push_front_node(box Node::new(elt))
522 /// Removes the first element and returns it, or `None` if the list is
525 /// This operation should compute in O(1) time.
530 /// use std::collections::LinkedList;
532 /// let mut d = LinkedList::new();
533 /// assert_eq!(d.pop_front(), None);
537 /// assert_eq!(d.pop_front(), Some(3));
538 /// assert_eq!(d.pop_front(), Some(1));
539 /// assert_eq!(d.pop_front(), None);
543 #[stable(feature = "rust1", since = "1.0.0")]
544 pub fn pop_front(&mut self) -> Option<T> {
545 self.pop_front_node().map(|box Node { value, .. }| value)
548 /// Appends an element to the back of a list
553 /// use std::collections::LinkedList;
555 /// let mut d = LinkedList::new();
558 /// assert_eq!(3, *d.back().unwrap());
560 #[stable(feature = "rust1", since = "1.0.0")]
561 pub fn push_back(&mut self, elt: T) {
562 self.push_back_node(box Node::new(elt))
565 /// Removes the last element from a list and returns it, or `None` if
571 /// use std::collections::LinkedList;
573 /// let mut d = LinkedList::new();
574 /// assert_eq!(d.pop_back(), None);
577 /// assert_eq!(d.pop_back(), Some(3));
579 #[stable(feature = "rust1", since = "1.0.0")]
580 pub fn pop_back(&mut self) -> Option<T> {
581 self.pop_back_node().map(|box Node { value, .. }| value)
584 /// Splits the list into two at the given index. Returns everything after the given index,
585 /// including the index.
589 /// Panics if `at > len`.
591 /// This operation should compute in O(n) time.
596 /// use std::collections::LinkedList;
598 /// let mut d = LinkedList::new();
604 /// let mut splitted = d.split_off(2);
606 /// assert_eq!(splitted.pop_front(), Some(1));
607 /// assert_eq!(splitted.pop_front(), None);
609 #[stable(feature = "rust1", since = "1.0.0")]
610 pub fn split_off(&mut self, at: usize) -> LinkedList<T> {
611 let len = self.len();
612 assert!(at <= len, "Cannot split off at a nonexistent index");
614 return mem::replace(self, LinkedList::new());
615 } else if at == len {
616 return LinkedList::new();
619 // Below, we iterate towards the `i-1`th node, either from the start or the end,
620 // depending on which would be faster.
621 let mut split_node = if at - 1 <= len - 1 - (at - 1) {
622 let mut iter = self.iter_mut();
623 // instead of skipping using .skip() (which creates a new struct),
624 // we skip manually so we can access the head field without
625 // depending on implementation details of Skip
631 // better off starting from the end
632 let mut iter = self.iter_mut();
633 for _ in 0..len - 1 - (at - 1) {
639 // The split node is the new tail node of the first part and owns
640 // the head of the second part.
641 let mut second_part_head;
644 second_part_head = split_node.resolve_mut().unwrap().next.take();
645 match second_part_head {
647 Some(ref mut head) => head.prev = Rawlink::none(),
651 let second_part = LinkedList {
652 list_head: second_part_head,
653 list_tail: self.list_tail,
657 // Fix the tail ptr of the first part
658 self.list_tail = split_node;
665 #[stable(feature = "rust1", since = "1.0.0")]
666 impl<T> Drop for LinkedList<T> {
667 #[unsafe_destructor_blind_to_params]
669 // Dissolve the linked_list in a loop.
670 // Just dropping the list_head can lead to stack exhaustion
671 // when length is >> 1_000_000
672 while let Some(mut head_) = self.list_head.take() {
673 self.list_head = head_.next.take();
676 self.list_tail = Rawlink::none();
680 #[stable(feature = "rust1", since = "1.0.0")]
681 impl<'a, A> Iterator for Iter<'a, A> {
685 fn next(&mut self) -> Option<&'a A> {
689 self.head.as_ref().map(|head| {
691 self.head = &head.next;
697 fn size_hint(&self) -> (usize, Option<usize>) {
698 (self.nelem, Some(self.nelem))
702 #[stable(feature = "rust1", since = "1.0.0")]
703 impl<'a, A> DoubleEndedIterator for Iter<'a, A> {
705 fn next_back(&mut self) -> Option<&'a A> {
710 self.tail.resolve().map(|prev| {
712 self.tail = prev.prev;
719 #[stable(feature = "rust1", since = "1.0.0")]
720 impl<'a, A> ExactSizeIterator for Iter<'a, A> {}
722 #[stable(feature = "rust1", since = "1.0.0")]
723 impl<'a, A> Iterator for IterMut<'a, A> {
724 type Item = &'a mut A;
726 fn next(&mut self) -> Option<&'a mut A> {
731 self.head.resolve_mut().map(|next| {
733 self.head = Rawlink::from(&mut next.next);
740 fn size_hint(&self) -> (usize, Option<usize>) {
741 (self.nelem, Some(self.nelem))
745 #[stable(feature = "rust1", since = "1.0.0")]
746 impl<'a, A> DoubleEndedIterator for IterMut<'a, A> {
748 fn next_back(&mut self) -> Option<&'a mut A> {
753 self.tail.resolve_mut().map(|prev| {
755 self.tail = prev.prev;
762 #[stable(feature = "rust1", since = "1.0.0")]
763 impl<'a, A> ExactSizeIterator for IterMut<'a, A> {}
765 // private methods for IterMut
766 impl<'a, A> IterMut<'a, A> {
767 fn insert_next_node(&mut self, mut ins_node: Box<Node<A>>) {
768 // Insert before `self.head` so that it is between the
769 // previously yielded element and self.head.
771 // The inserted node will not appear in further iteration.
772 match unsafe { self.head.resolve_mut() } {
774 self.list.push_back_node(ins_node);
777 let prev_node = match unsafe { node.prev.resolve_mut() } {
778 None => return self.list.push_front_node(ins_node),
781 let node_own = prev_node.next.take().unwrap();
782 ins_node.set_next(node_own);
783 prev_node.set_next(ins_node);
784 self.list.length += 1;
790 impl<'a, A> IterMut<'a, A> {
791 /// Inserts `elt` just after the element most recently returned by `.next()`.
792 /// The inserted element does not appear in the iteration.
797 /// #![feature(linked_list_extras)]
799 /// use std::collections::LinkedList;
801 /// let mut list: LinkedList<_> = vec![1, 3, 4].into_iter().collect();
804 /// let mut it = list.iter_mut();
805 /// assert_eq!(it.next().unwrap(), &1);
806 /// // insert `2` after `1`
807 /// it.insert_next(2);
810 /// let vec: Vec<_> = list.into_iter().collect();
811 /// assert_eq!(vec, [1, 2, 3, 4]);
815 #[unstable(feature = "linked_list_extras",
816 reason = "this is probably better handled by a cursor type -- we'll see",
818 pub fn insert_next(&mut self, elt: A) {
819 self.insert_next_node(box Node::new(elt))
822 /// Provides a reference to the next element, without changing the iterator.
827 /// #![feature(linked_list_extras)]
829 /// use std::collections::LinkedList;
831 /// let mut list: LinkedList<_> = vec![1, 2, 3].into_iter().collect();
833 /// let mut it = list.iter_mut();
834 /// assert_eq!(it.next().unwrap(), &1);
835 /// assert_eq!(it.peek_next().unwrap(), &2);
836 /// // We just peeked at 2, so it was not consumed from the iterator.
837 /// assert_eq!(it.next().unwrap(), &2);
840 #[unstable(feature = "linked_list_extras",
841 reason = "this is probably better handled by a cursor type -- we'll see",
843 pub fn peek_next(&mut self) -> Option<&mut A> {
847 unsafe { self.head.resolve_mut().map(|head| &mut head.value) }
851 #[stable(feature = "rust1", since = "1.0.0")]
852 impl<A> Iterator for IntoIter<A> {
856 fn next(&mut self) -> Option<A> {
857 self.list.pop_front()
861 fn size_hint(&self) -> (usize, Option<usize>) {
862 (self.list.length, Some(self.list.length))
866 #[stable(feature = "rust1", since = "1.0.0")]
867 impl<A> DoubleEndedIterator for IntoIter<A> {
869 fn next_back(&mut self) -> Option<A> {
874 #[stable(feature = "rust1", since = "1.0.0")]
875 impl<A> ExactSizeIterator for IntoIter<A> {}
877 #[stable(feature = "rust1", since = "1.0.0")]
878 impl<A> FromIterator<A> for LinkedList<A> {
879 fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> LinkedList<A> {
880 let mut ret = LinkedList::new();
886 #[stable(feature = "rust1", since = "1.0.0")]
887 impl<T> IntoIterator for LinkedList<T> {
889 type IntoIter = IntoIter<T>;
891 /// Consumes the list into an iterator yielding elements by value.
893 fn into_iter(self) -> IntoIter<T> {
894 IntoIter { list: self }
898 #[stable(feature = "rust1", since = "1.0.0")]
899 impl<'a, T> IntoIterator for &'a LinkedList<T> {
901 type IntoIter = Iter<'a, T>;
903 fn into_iter(self) -> Iter<'a, T> {
908 #[stable(feature = "rust1", since = "1.0.0")]
909 impl<'a, T> IntoIterator for &'a mut LinkedList<T> {
910 type Item = &'a mut T;
911 type IntoIter = IterMut<'a, T>;
913 fn into_iter(mut self) -> IterMut<'a, T> {
918 #[stable(feature = "rust1", since = "1.0.0")]
919 impl<A> Extend<A> for LinkedList<A> {
920 fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
927 #[stable(feature = "extend_ref", since = "1.2.0")]
928 impl<'a, T: 'a + Copy> Extend<&'a T> for LinkedList<T> {
929 fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
930 self.extend(iter.into_iter().cloned());
934 #[stable(feature = "rust1", since = "1.0.0")]
935 impl<A: PartialEq> PartialEq for LinkedList<A> {
936 fn eq(&self, other: &LinkedList<A>) -> bool {
937 self.len() == other.len() && self.iter().eq(other.iter())
940 fn ne(&self, other: &LinkedList<A>) -> bool {
941 self.len() != other.len() || self.iter().ne(other.iter())
945 #[stable(feature = "rust1", since = "1.0.0")]
946 impl<A: Eq> Eq for LinkedList<A> {}
948 #[stable(feature = "rust1", since = "1.0.0")]
949 impl<A: PartialOrd> PartialOrd for LinkedList<A> {
950 fn partial_cmp(&self, other: &LinkedList<A>) -> Option<Ordering> {
951 self.iter().partial_cmp(other.iter())
955 #[stable(feature = "rust1", since = "1.0.0")]
956 impl<A: Ord> Ord for LinkedList<A> {
958 fn cmp(&self, other: &LinkedList<A>) -> Ordering {
959 self.iter().cmp(other.iter())
963 #[stable(feature = "rust1", since = "1.0.0")]
964 impl<A: Clone> Clone for LinkedList<A> {
965 fn clone(&self) -> LinkedList<A> {
966 self.iter().cloned().collect()
970 #[stable(feature = "rust1", since = "1.0.0")]
971 impl<A: fmt::Debug> fmt::Debug for LinkedList<A> {
972 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
973 f.debug_list().entries(self.iter()).finish()
977 #[stable(feature = "rust1", since = "1.0.0")]
978 impl<A: Hash> Hash for LinkedList<A> {
979 fn hash<H: Hasher>(&self, state: &mut H) {
980 self.len().hash(state);
987 // Ensure that `LinkedList` and its read-only iterators are covariant in their type parameters.
989 fn assert_covariance() {
990 fn a<'a>(x: LinkedList<&'static str>) -> LinkedList<&'a str> { x }
991 fn b<'i, 'a>(x: Iter<'i, &'static str>) -> Iter<'i, &'a str> { x }
992 fn c<'a>(x: IntoIter<&'static str>) -> IntoIter<&'a str> { x }
997 use std::clone::Clone;
998 use std::iter::{Iterator, IntoIterator, Extend};
999 use std::option::Option::{self, Some, None};
1000 use std::__rand::{thread_rng, Rng};
1004 use super::{LinkedList, Node};
1007 fn list_from<T: Clone>(v: &[T]) -> LinkedList<T> {
1008 v.iter().cloned().collect()
1011 pub fn check_links<T>(list: &LinkedList<T>) {
1013 let mut last_ptr: Option<&Node<T>> = None;
1014 let mut node_ptr: &Node<T>;
1015 match list.list_head {
1017 assert_eq!(0, list.length);
1020 Some(ref node) => node_ptr = &**node,
1023 match unsafe { (last_ptr, node_ptr.prev.resolve()) } {
1025 (None, _) => panic!("prev link for list_head"),
1026 (Some(p), Some(pptr)) => {
1027 assert_eq!(p as *const Node<T>, pptr as *const Node<T>);
1029 _ => panic!("prev link is none, not good"),
1031 match node_ptr.next {
1033 last_ptr = Some(node_ptr);
1043 assert_eq!(len, list.length);
1050 let mut m = LinkedList::<i32>::new();
1051 let mut n = LinkedList::new();
1054 assert_eq!(m.len(), 0);
1055 assert_eq!(n.len(), 0);
1057 // Non-empty to empty
1059 let mut m = LinkedList::new();
1060 let mut n = LinkedList::new();
1064 assert_eq!(m.len(), 1);
1065 assert_eq!(m.pop_back(), Some(2));
1066 assert_eq!(n.len(), 0);
1069 // Empty to non-empty
1071 let mut m = LinkedList::new();
1072 let mut n = LinkedList::new();
1076 assert_eq!(m.len(), 1);
1077 assert_eq!(m.pop_back(), Some(2));
1081 // Non-empty to non-empty
1082 let v = vec![1, 2, 3, 4, 5];
1083 let u = vec![9, 8, 1, 2, 3, 4, 5];
1084 let mut m = list_from(&v);
1085 let mut n = list_from(&u);
1090 assert_eq!(sum.len(), m.len());
1092 assert_eq!(m.pop_front(), Some(elt))
1094 assert_eq!(n.len(), 0);
1095 // let's make sure it's working properly, since we
1096 // did some direct changes to private members
1098 assert_eq!(n.len(), 1);
1099 assert_eq!(n.pop_front(), Some(3));
1104 fn test_insert_prev() {
1105 let mut m = list_from(&[0, 2, 4, 6, 8]);
1108 let mut it = m.iter_mut();
1114 it.insert_next(*elt + 1);
1115 match it.peek_next() {
1116 Some(x) => assert_eq!(*x, *elt + 2),
1117 None => assert_eq!(8, *elt),
1126 assert_eq!(m.len(), 3 + len * 2);
1127 assert_eq!(m.into_iter().collect::<Vec<_>>(),
1128 [-2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1]);
1133 let n = list_from(&[1, 2, 3]);
1134 thread::spawn(move || {
1136 let a: &[_] = &[&1, &2, &3];
1137 assert_eq!(a, &n.iter().collect::<Vec<_>>()[..]);
1155 use std::iter::ExactSizeIterator;
1156 // There was a bug in split_off that failed to null out the RHS's head's prev ptr.
1157 // This caused the RHS's dtor to walk up into the LHS at drop and delete all of
1160 // https://github.com/rust-lang/rust/issues/26021
1161 let mut v1 = LinkedList::new();
1166 let _ = v1.split_off(3); // Dropping this now should not cause laundry consumption
1167 assert_eq!(v1.len(), 3);
1169 assert_eq!(v1.iter().len(), 3);
1170 assert_eq!(v1.iter().collect::<Vec<_>>().len(), 3);
1174 fn test_split_off() {
1175 let mut v1 = LinkedList::new();
1182 for ix in 0..1 + v1.len() {
1183 let mut a = v1.clone();
1184 let b = a.split_off(ix);
1194 fn fuzz_test(sz: i32) {
1195 let mut m: LinkedList<_> = LinkedList::new();
1199 let r: u8 = thread_rng().next_u32() as u8;
1225 for (a, &b) in m.into_iter().zip(&v) {
1229 assert_eq!(i, v.len());