about summary refs log tree commit diff
diff options
context:
space:
mode:
authorblake2-ppc <blake2-ppc>2013-07-18 18:46:37 +0200
committerblake2-ppc <blake2-ppc>2013-07-20 20:30:58 +0200
commitfe134b9509821e5e2fad5545cdd23c5325dfd583 (patch)
treea02b9ea2cbab8a014ad5234f8d4cdef1fe0fbd38
parent24b6901b26f0bde00706a5cbc16ffc29296ea40d (diff)
downloadrust-fe134b9509821e5e2fad5545cdd23c5325dfd583.tar.gz
rust-fe134b9509821e5e2fad5545cdd23c5325dfd583.zip
dlist: Implement Clone for immutable iterators
-rw-r--r--src/libextra/dlist.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/libextra/dlist.rs b/src/libextra/dlist.rs
index c42eba1ffa2..fe05b48988e 100644
--- a/src/libextra/dlist.rs
+++ b/src/libextra/dlist.rs
@@ -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 @@ impl<T> Rawlink<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;
@@ -687,6 +696,20 @@ mod tests {
     }
 
     #[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();
         assert_eq!(n.iter().next(), None);