From fe134b9509821e5e2fad5545cdd23c5325dfd583 Mon Sep 17 00:00:00 2001 From: blake2-ppc Date: Thu, 18 Jul 2013 18:46:37 +0200 Subject: [PATCH] dlist: Implement Clone for immutable iterators --- src/libextra/dlist.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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 { } /// Double-ended DList iterator +#[deriving(Clone)] pub struct DListIterator<'self, T> { priv head: &'self Link, priv tail: Rawlink>, @@ -62,6 +63,7 @@ pub struct MutDListIterator<'self, T> { } /// DList consuming iterator +#[deriving(Clone)] pub struct ConsumeIterator { priv list: DList } @@ -93,6 +95,13 @@ impl Rawlink { } } +impl Clone for Rawlink { + #[inline] + fn clone(&self) -> Rawlink { + Rawlink{p: self.p} + } +} + /// Set the .prev field on `next`, then return `Some(next)` fn link_with_prev(mut next: ~Node, prev: Rawlink>) -> Link { next.prev = prev; @@ -686,6 +695,20 @@ mod tests { 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();