1 // Copyright 2012-2014 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 `DList` allows pushing and popping elements at either end and is thus
14 //! efficiently usable as a double-ended queue.
16 // DList 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 DList::prev are raw pointers that form a full chain in
20 // the reverse direction.
24 use alloc::boxed::Box;
25 use core::cmp::Ordering;
26 use core::default::Default;
28 use core::hash::{Writer, Hash};
29 use core::iter::{self, FromIterator};
33 /// A doubly-linked list.
38 list_tail: Rawlink<Node<T>>,
41 type Link<T> = Option<Box<Node<T>>>;
47 impl<T> Copy for Rawlink<T> {}
48 unsafe impl<T:'static+Send> Send for Rawlink<T> {}
49 unsafe impl<T:Send+Sync> Sync for Rawlink<T> {}
53 prev: Rawlink<Node<T>>,
57 /// An iterator over references to the items of a `DList`.
59 pub struct Iter<'a, T:'a> {
61 tail: Rawlink<Node<T>>,
65 // FIXME #19839: deriving is too aggressive on the bounds (T doesn't need to be Clone).
67 impl<'a, T> Clone for Iter<'a, T> {
68 fn clone(&self) -> Iter<'a, T> {
70 head: self.head.clone(),
77 /// An iterator over mutable references to the items of a `DList`.
79 pub struct IterMut<'a, T:'a> {
80 list: &'a mut DList<T>,
81 head: Rawlink<Node<T>>,
82 tail: Rawlink<Node<T>>,
86 /// An iterator over mutable references to the items of a `DList`.
89 pub struct IntoIter<T> {
93 /// Rawlink is a type like Option<T> but for holding a raw pointer
95 /// Like Option::None for Rawlink
96 fn none() -> Rawlink<T> {
97 Rawlink{p: ptr::null_mut()}
100 /// Like Option::Some for Rawlink
101 fn some(n: &mut T) -> Rawlink<T> {
105 /// Convert the `Rawlink` into an Option value
106 fn resolve_immut<'a>(&self) -> Option<&'a T> {
108 mem::transmute(self.p.as_ref())
112 /// Convert the `Rawlink` into an Option value
113 fn resolve<'a>(&mut self) -> Option<&'a mut T> {
114 if self.p.is_null() {
117 Some(unsafe { mem::transmute(self.p) })
121 /// Return the `Rawlink` and replace with `Rawlink::none()`
122 fn take(&mut self) -> Rawlink<T> {
123 mem::replace(self, Rawlink::none())
127 impl<T> Clone for Rawlink<T> {
129 fn clone(&self) -> Rawlink<T> {
135 fn new(v: T) -> Node<T> {
136 Node{value: v, next: None, prev: Rawlink::none()}
140 /// Set the .prev field on `next`, then return `Some(next)`
141 fn link_with_prev<T>(mut next: Box<Node<T>>, prev: Rawlink<Node<T>>)
149 /// Add a Node first in the list
151 fn push_front_node(&mut self, mut new_head: Box<Node<T>>) {
152 match self.list_head {
154 self.list_tail = Rawlink::some(&mut *new_head);
155 self.list_head = link_with_prev(new_head, Rawlink::none());
157 Some(ref mut head) => {
158 new_head.prev = Rawlink::none();
159 head.prev = Rawlink::some(&mut *new_head);
160 mem::swap(head, &mut new_head);
161 head.next = Some(new_head);
167 /// Remove the first Node and return it, or None if the list is empty
169 fn pop_front_node(&mut self) -> Option<Box<Node<T>>> {
170 self.list_head.take().map(|mut front_node| {
172 match front_node.next.take() {
173 Some(node) => self.list_head = link_with_prev(node, Rawlink::none()),
174 None => self.list_tail = Rawlink::none()
180 /// Add a Node last in the list
182 fn push_back_node(&mut self, mut new_tail: Box<Node<T>>) {
183 match self.list_tail.resolve() {
184 None => return self.push_front_node(new_tail),
186 self.list_tail = Rawlink::some(&mut *new_tail);
187 tail.next = link_with_prev(new_tail, Rawlink::some(tail));
193 /// Remove the last Node and return it, or None if the list is empty
195 fn pop_back_node(&mut self) -> Option<Box<Node<T>>> {
196 self.list_tail.resolve().map_or(None, |tail| {
198 self.list_tail = tail.prev;
199 match tail.prev.resolve() {
200 None => self.list_head.take(),
201 Some(tail_prev) => tail_prev.next.take()
208 impl<T> Default for DList<T> {
211 fn default() -> DList<T> { DList::new() }
215 /// Creates an empty `DList`.
218 pub fn new() -> DList<T> {
219 DList{list_head: None, list_tail: Rawlink::none(), length: 0}
222 /// Adds all elements from `other` to the end of the list.
224 /// This operation should compute in O(1) time.
229 /// use std::collections::DList;
231 /// let mut a = DList::new();
232 /// let mut b = DList::new();
240 /// for e in a.iter() {
241 /// println!("{}", e); // prints 1, then 2, then 3, then 4
244 #[unstable = "append should be by-mutable-reference"]
245 pub fn append(&mut self, mut other: DList<T>) {
246 match self.list_tail.resolve() {
247 None => *self = other,
249 // Carefully empty `other`.
250 let o_tail = other.list_tail.take();
251 let o_length = other.length;
252 match other.list_head.take() {
255 tail.next = link_with_prev(node, self.list_tail);
256 self.list_tail = o_tail;
257 self.length += o_length;
264 /// Provides a forward iterator.
267 pub fn iter(&self) -> Iter<T> {
268 Iter{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
271 /// Provides a forward iterator with mutable references.
274 pub fn iter_mut(&mut self) -> IterMut<T> {
275 let head_raw = match self.list_head {
276 Some(ref mut h) => Rawlink::some(&mut **h),
277 None => Rawlink::none(),
282 tail: self.list_tail,
287 /// Consumes the list into an iterator yielding elements by value.
290 pub fn into_iter(self) -> IntoIter<T> {
294 /// Returns `true` if the `DList` is empty.
296 /// This operation should compute in O(1) time.
299 pub fn is_empty(&self) -> bool {
300 self.list_head.is_none()
303 /// Returns the length of the `DList`.
305 /// This operation should compute in O(1) time.
308 pub fn len(&self) -> uint {
312 /// Removes all elements from the `DList`.
314 /// This operation should compute in O(n) time.
317 pub fn clear(&mut self) {
321 /// Provides a reference to the front element, or `None` if the list is
325 pub fn front(&self) -> Option<&T> {
326 self.list_head.as_ref().map(|head| &head.value)
329 /// Provides a mutable reference to the front element, or `None` if the list
333 pub fn front_mut(&mut self) -> Option<&mut T> {
334 self.list_head.as_mut().map(|head| &mut head.value)
337 /// Provides a reference to the back element, or `None` if the list is
341 pub fn back(&self) -> Option<&T> {
342 self.list_tail.resolve_immut().as_ref().map(|tail| &tail.value)
345 /// Provides a mutable reference to the back element, or `None` if the list
349 pub fn back_mut(&mut self) -> Option<&mut T> {
350 self.list_tail.resolve().map(|tail| &mut tail.value)
353 /// Adds an element first in the list.
355 /// This operation should compute in O(1) time.
357 pub fn push_front(&mut self, elt: T) {
358 self.push_front_node(box Node::new(elt))
361 /// Removes the first element and returns it, or `None` if the list is
364 /// This operation should compute in O(1) time.
366 pub fn pop_front(&mut self) -> Option<T> {
367 self.pop_front_node().map(|box Node{value, ..}| value)
370 /// Appends an element to the back of a list
375 /// use std::collections::DList;
377 /// let mut d = DList::new();
380 /// assert_eq!(3, *d.back().unwrap());
383 pub fn push_back(&mut self, elt: T) {
384 self.push_back_node(box Node::new(elt))
387 /// Removes the last element from a list and returns it, or `None` if
393 /// use std::collections::DList;
395 /// let mut d = DList::new();
396 /// assert_eq!(d.pop_back(), None);
399 /// assert_eq!(d.pop_back(), Some(3));
402 pub fn pop_back(&mut self) -> Option<T> {
403 self.pop_back_node().map(|box Node{value, ..}| value)
409 impl<T> Drop for DList<T> {
411 // Dissolve the dlist in backwards direction
412 // Just dropping the list_head can lead to stack exhaustion
413 // when length is >> 1_000_000
414 let mut tail = self.list_tail;
416 match tail.resolve() {
419 prev.next.take(); // release Box<Node<T>>
425 self.list_head = None;
426 self.list_tail = Rawlink::none();
431 impl<'a, A> Iterator for Iter<'a, A> {
435 fn next(&mut self) -> Option<&'a A> {
439 self.head.as_ref().map(|head| {
441 self.head = &head.next;
447 fn size_hint(&self) -> (uint, Option<uint>) {
448 (self.nelem, Some(self.nelem))
453 impl<'a, A> DoubleEndedIterator for Iter<'a, A> {
455 fn next_back(&mut self) -> Option<&'a A> {
459 self.tail.resolve_immut().as_ref().map(|prev| {
461 self.tail = prev.prev;
468 impl<'a, A> ExactSizeIterator for Iter<'a, A> {}
471 impl<'a, A> Iterator for IterMut<'a, A> {
472 type Item = &'a mut A;
474 fn next(&mut self) -> Option<&'a mut A> {
478 self.head.resolve().map(|next| {
480 self.head = match next.next {
481 Some(ref mut node) => Rawlink::some(&mut **node),
482 None => Rawlink::none(),
489 fn size_hint(&self) -> (uint, Option<uint>) {
490 (self.nelem, Some(self.nelem))
495 impl<'a, A> DoubleEndedIterator for IterMut<'a, A> {
497 fn next_back(&mut self) -> Option<&'a mut A> {
501 self.tail.resolve().map(|prev| {
503 self.tail = prev.prev;
510 impl<'a, A> ExactSizeIterator for IterMut<'a, A> {}
512 // private methods for IterMut
513 impl<'a, A> IterMut<'a, A> {
514 fn insert_next_node(&mut self, mut ins_node: Box<Node<A>>) {
515 // Insert before `self.head` so that it is between the
516 // previously yielded element and self.head.
518 // The inserted node will not appear in further iteration.
519 match self.head.resolve() {
520 None => { self.list.push_back_node(ins_node); }
522 let prev_node = match node.prev.resolve() {
523 None => return self.list.push_front_node(ins_node),
526 let node_own = prev_node.next.take().unwrap();
527 ins_node.next = link_with_prev(node_own, Rawlink::some(&mut *ins_node));
528 prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
529 self.list.length += 1;
535 impl<'a, A> IterMut<'a, A> {
536 /// Inserts `elt` just after the element most recently returned by `.next()`.
537 /// The inserted element does not appear in the iteration.
542 /// use std::collections::DList;
544 /// let mut list: DList<int> = vec![1, 3, 4].into_iter().collect();
547 /// let mut it = list.iter_mut();
548 /// assert_eq!(it.next().unwrap(), &1);
549 /// // insert `2` after `1`
550 /// it.insert_next(2);
553 /// let vec: Vec<int> = list.into_iter().collect();
554 /// assert_eq!(vec, vec![1i, 2, 3, 4]);
558 #[unstable = "this is probably better handled by a cursor type -- we'll see"]
559 pub fn insert_next(&mut self, elt: A) {
560 self.insert_next_node(box Node::new(elt))
563 /// Provides a reference to the next element, without changing the iterator.
568 /// use std::collections::DList;
570 /// let mut list: DList<int> = vec![1, 2, 3].into_iter().collect();
572 /// let mut it = list.iter_mut();
573 /// assert_eq!(it.next().unwrap(), &1);
574 /// assert_eq!(it.peek_next().unwrap(), &2);
575 /// // We just peeked at 2, so it was not consumed from the iterator.
576 /// assert_eq!(it.next().unwrap(), &2);
579 #[unstable = "this is probably better handled by a cursor type -- we'll see"]
580 pub fn peek_next(&mut self) -> Option<&mut A> {
584 self.head.resolve().map(|head| &mut head.value)
589 impl<A> Iterator for IntoIter<A> {
593 fn next(&mut self) -> Option<A> { self.list.pop_front() }
596 fn size_hint(&self) -> (uint, Option<uint>) {
597 (self.list.length, Some(self.list.length))
602 impl<A> DoubleEndedIterator for IntoIter<A> {
604 fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
608 impl<A> FromIterator<A> for DList<A> {
609 fn from_iter<T: Iterator<Item=A>>(iterator: T) -> DList<A> {
610 let mut ret = DList::new();
611 ret.extend(iterator);
617 impl<A> Extend<A> for DList<A> {
618 fn extend<T: Iterator<Item=A>>(&mut self, mut iterator: T) {
619 for elt in iterator { self.push_back(elt); }
624 impl<A: PartialEq> PartialEq for DList<A> {
625 fn eq(&self, other: &DList<A>) -> bool {
626 self.len() == other.len() &&
627 iter::order::eq(self.iter(), other.iter())
630 fn ne(&self, other: &DList<A>) -> bool {
631 self.len() != other.len() ||
632 iter::order::ne(self.iter(), other.iter())
637 impl<A: Eq> Eq for DList<A> {}
640 impl<A: PartialOrd> PartialOrd for DList<A> {
641 fn partial_cmp(&self, other: &DList<A>) -> Option<Ordering> {
642 iter::order::partial_cmp(self.iter(), other.iter())
647 impl<A: Ord> Ord for DList<A> {
649 fn cmp(&self, other: &DList<A>) -> Ordering {
650 iter::order::cmp(self.iter(), other.iter())
655 impl<A: Clone> Clone for DList<A> {
656 fn clone(&self) -> DList<A> {
657 self.iter().map(|x| x.clone()).collect()
662 impl<A: fmt::Show> fmt::Show for DList<A> {
663 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
664 try!(write!(f, "["));
666 for (i, e) in self.iter().enumerate() {
667 if i != 0 { try!(write!(f, ", ")); }
668 try!(write!(f, "{}", *e));
676 impl<S: Writer, A: Hash<S>> Hash<S> for DList<A> {
677 fn hash(&self, state: &mut S) {
678 self.len().hash(state);
679 for elt in self.iter() {
690 use std::thread::Thread;
694 use super::{DList, Node};
696 pub fn check_links<T>(list: &DList<T>) {
698 let mut last_ptr: Option<&Node<T>> = None;
699 let mut node_ptr: &Node<T>;
700 match list.list_head {
701 None => { assert_eq!(0u, list.length); return }
702 Some(ref node) => node_ptr = &**node,
705 match (last_ptr, node_ptr.prev.resolve_immut()) {
707 (None , _ ) => panic!("prev link for list_head"),
708 (Some(p), Some(pptr)) => {
709 assert_eq!(p as *const Node<T>, pptr as *const Node<T>);
711 _ => panic!("prev link is none, not good"),
713 match node_ptr.next {
715 last_ptr = Some(node_ptr);
725 assert_eq!(len, list.length);
730 let mut m: DList<Box<int>> = DList::new();
731 assert_eq!(m.pop_front(), None);
732 assert_eq!(m.pop_back(), None);
733 assert_eq!(m.pop_front(), None);
735 assert_eq!(m.pop_front(), Some(box 1));
738 assert_eq!(m.len(), 2);
739 assert_eq!(m.pop_front(), Some(box 2));
740 assert_eq!(m.pop_front(), Some(box 3));
741 assert_eq!(m.len(), 0);
742 assert_eq!(m.pop_front(), None);
747 assert_eq!(m.pop_front(), Some(box 1));
749 let mut n = DList::new();
753 assert_eq!(n.front().unwrap(), &3);
754 let x = n.front_mut().unwrap();
759 assert_eq!(n.back().unwrap(), &2);
760 let y = n.back_mut().unwrap();
764 assert_eq!(n.pop_front(), Some(0));
765 assert_eq!(n.pop_front(), Some(1));
769 fn generate_test() -> DList<int> {
770 list_from(&[0i,1,2,3,4,5,6])
774 fn list_from<T: Clone>(v: &[T]) -> DList<T> {
775 v.iter().map(|x| (*x).clone()).collect()
780 let m = generate_test();
781 for (i, elt) in m.iter().enumerate() {
782 assert_eq!(i as int, *elt);
784 let mut n = DList::new();
785 assert_eq!(n.iter().next(), None);
787 let mut it = n.iter();
788 assert_eq!(it.size_hint(), (1, Some(1)));
789 assert_eq!(it.next().unwrap(), &4);
790 assert_eq!(it.size_hint(), (0, Some(0)));
791 assert_eq!(it.next(), None);
795 fn test_iterator_clone() {
796 let mut n = DList::new();
800 let mut it = n.iter();
802 let mut jt = it.clone();
803 assert_eq!(it.next(), jt.next());
804 assert_eq!(it.next_back(), jt.next_back());
805 assert_eq!(it.next(), jt.next());
809 fn test_iterator_double_end() {
810 let mut n = DList::new();
811 assert_eq!(n.iter().next(), None);
815 let mut it = n.iter();
816 assert_eq!(it.size_hint(), (3, Some(3)));
817 assert_eq!(it.next().unwrap(), &6);
818 assert_eq!(it.size_hint(), (2, Some(2)));
819 assert_eq!(it.next_back().unwrap(), &4);
820 assert_eq!(it.size_hint(), (1, Some(1)));
821 assert_eq!(it.next_back().unwrap(), &5);
822 assert_eq!(it.next_back(), None);
823 assert_eq!(it.next(), None);
828 let m = generate_test();
829 for (i, elt) in m.iter().rev().enumerate() {
830 assert_eq!((6 - i) as int, *elt);
832 let mut n = DList::new();
833 assert_eq!(n.iter().rev().next(), None);
835 let mut it = n.iter().rev();
836 assert_eq!(it.size_hint(), (1, Some(1)));
837 assert_eq!(it.next().unwrap(), &4);
838 assert_eq!(it.size_hint(), (0, Some(0)));
839 assert_eq!(it.next(), None);
844 let mut m = generate_test();
845 let mut len = m.len();
846 for (i, elt) in m.iter_mut().enumerate() {
847 assert_eq!(i as int, *elt);
851 let mut n = DList::new();
852 assert!(n.iter_mut().next().is_none());
855 let mut it = n.iter_mut();
856 assert_eq!(it.size_hint(), (2, Some(2)));
857 assert!(it.next().is_some());
858 assert!(it.next().is_some());
859 assert_eq!(it.size_hint(), (0, Some(0)));
860 assert!(it.next().is_none());
864 fn test_iterator_mut_double_end() {
865 let mut n = DList::new();
866 assert!(n.iter_mut().next_back().is_none());
870 let mut it = n.iter_mut();
871 assert_eq!(it.size_hint(), (3, Some(3)));
872 assert_eq!(*it.next().unwrap(), 6);
873 assert_eq!(it.size_hint(), (2, Some(2)));
874 assert_eq!(*it.next_back().unwrap(), 4);
875 assert_eq!(it.size_hint(), (1, Some(1)));
876 assert_eq!(*it.next_back().unwrap(), 5);
877 assert!(it.next_back().is_none());
878 assert!(it.next().is_none());
882 fn test_insert_prev() {
883 let mut m = list_from(&[0i,2,4,6,8]);
886 let mut it = m.iter_mut();
892 it.insert_next(*elt + 1);
893 match it.peek_next() {
894 Some(x) => assert_eq!(*x, *elt + 2),
895 None => assert_eq!(8, *elt),
904 assert_eq!(m.len(), 3 + len * 2);
905 assert_eq!(m.into_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
909 fn test_mut_rev_iter() {
910 let mut m = generate_test();
911 for (i, elt) in m.iter_mut().rev().enumerate() {
912 assert_eq!((6-i) as int, *elt);
914 let mut n = DList::new();
915 assert!(n.iter_mut().rev().next().is_none());
917 let mut it = n.iter_mut().rev();
918 assert!(it.next().is_some());
919 assert!(it.next().is_none());
924 let n = list_from(&[1i,2,3]);
925 Thread::spawn(move || {
927 let a: &[_] = &[&1,&2,&3];
928 assert_eq!(a, n.iter().collect::<Vec<&int>>());
929 }).join().ok().unwrap();
934 let mut n: DList<u8> = list_from(&[]);
935 let mut m = list_from(&[]);
942 let n = list_from(&[2i,3,4]);
943 let m = list_from(&[1i,2,3]);
949 let mut x = DList::new();
950 let mut y = DList::new();
952 assert!(hash::hash(&x) == hash::hash(&y));
962 assert!(hash::hash(&x) == hash::hash(&y));
967 let n: DList<int> = list_from(&[]);
968 let m = list_from(&[1i,2,3]);
977 let nan = 0.0f64/0.0;
978 let n = list_from(&[nan]);
979 let m = list_from(&[nan]);
985 let n = list_from(&[nan]);
986 let one = list_from(&[1.0f64]);
989 assert!(!(n <= one));
990 assert!(!(n >= one));
992 let u = list_from(&[1.0f64,2.0,nan]);
993 let v = list_from(&[1.0f64,2.0,3.0]);
999 let s = list_from(&[1.0f64,2.0,4.0,2.0]);
1000 let t = list_from(&[1.0f64,2.0,3.0,2.0]);
1003 assert!(!(s <= one));
1009 for _ in range(0u, 25) {
1018 let list: DList<int> = range(0i, 10).collect();
1019 assert!(list.to_string() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1021 let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
1024 assert!(list.to_string() == "[just, one, test, more]");
1028 fn fuzz_test(sz: int) {
1029 let mut m: DList<int> = DList::new();
1031 for i in range(0, sz) {
1033 let r: u8 = rand::random();
1059 for (a, &b) in m.into_iter().zip(v.iter()) {
1063 assert_eq!(i, v.len());
1066 #[allow(deprecated)]
1069 let mut m = DList::new();
1070 let mut n = DList::new();
1073 assert_eq!(m.len(), 1);
1074 assert_eq!(m.pop_back(), Some(2));
1078 let mut m = DList::new();
1079 let n = DList::new();
1082 assert_eq!(m.len(), 1);
1083 assert_eq!(m.pop_back(), Some(2));
1087 let v = vec![1i,2,3,4,5];
1088 let u = vec![9i,8,1,2,3,4,5];
1089 let mut m = list_from(v.as_slice());
1090 m.append(list_from(u.as_slice()));
1093 sum.push_all(u.as_slice());
1094 assert_eq!(sum.len(), m.len());
1095 for elt in sum.into_iter() {
1096 assert_eq!(m.pop_front(), Some(elt))
1101 fn bench_collect_into(b: &mut test::Bencher) {
1104 let _: DList<int> = v.iter().map(|x| *x).collect();
1109 fn bench_push_front(b: &mut test::Bencher) {
1110 let mut m: DList<int> = DList::new();
1117 fn bench_push_back(b: &mut test::Bencher) {
1118 let mut m: DList<int> = DList::new();
1125 fn bench_push_back_pop_back(b: &mut test::Bencher) {
1126 let mut m: DList<int> = DList::new();
1134 fn bench_push_front_pop_front(b: &mut test::Bencher) {
1135 let mut m: DList<int> = DList::new();
1143 fn bench_iter(b: &mut test::Bencher) {
1145 let m: DList<int> = v.iter().map(|&x|x).collect();
1147 assert!(m.iter().count() == 128);
1151 fn bench_iter_mut(b: &mut test::Bencher) {
1153 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1155 assert!(m.iter_mut().count() == 128);
1159 fn bench_iter_rev(b: &mut test::Bencher) {
1161 let m: DList<int> = v.iter().map(|&x|x).collect();
1163 assert!(m.iter().rev().count() == 128);
1167 fn bench_iter_mut_rev(b: &mut test::Bencher) {
1169 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1171 assert!(m.iter_mut().rev().count() == 128);