]> git.lizzy.rs Git - rust.git/commitdiff
dlist: Implement Clone for immutable iterators
authorblake2-ppc <blake2-ppc>
Thu, 18 Jul 2013 16:46:37 +0000 (18:46 +0200)
committerblake2-ppc <blake2-ppc>
Sat, 20 Jul 2013 18:30:58 +0000 (20:30 +0200)
src/libextra/dlist.rs

index c42eba1ffa29cd1aab52b5b47188150007c02605..fe05b48988eff956d261ecc32f554924745a77af 100644 (file)
@@ -47,6 +47,7 @@ struct Node<T> {
 }
 
 /// Double-ended DList iterator
+#[deriving(Clone)]
 pub struct DListIterator<'self, T> {
     priv head: &'self Link<T>,
     priv tail: Rawlink<Node<T>>,
@@ -62,6 +63,7 @@ pub struct MutDListIterator<'self, T> {
 }
 
 /// DList consuming iterator
+#[deriving(Clone)]
 pub struct ConsumeIterator<T> {
     priv list: DList<T>
 }
@@ -93,6 +95,13 @@ fn resolve(&mut self) -> Option<&mut T> {
     }
 }
 
+impl<T> Clone for Rawlink<T> {
+    #[inline]
+    fn clone(&self) -> Rawlink<T> {
+        Rawlink{p: self.p}
+    }
+}
+
 /// Set the .prev field on `next`, then return `Some(next)`
 fn link_with_prev<T>(mut next: ~Node<T>, prev: Rawlink<Node<T>>) -> Link<T> {
     next.prev = prev;
@@ -686,6 +695,20 @@ fn test_iterator() {
         assert_eq!(it.next(), None);
     }
 
+    #[test]
+    fn test_iterator_clone() {
+        let mut n = DList::new();
+        n.push_back(2);
+        n.push_back(3);
+        n.push_back(4);
+        let mut it = n.iter();
+        it.next();
+        let mut jt = it.clone();
+        assert_eq!(it.next(), jt.next());
+        assert_eq!(it.next_back(), jt.next_back());
+        assert_eq!(it.next(), jt.next());
+    }
+
     #[test]
     fn test_iterator_double_end() {
         let mut n = DList::new();