dlist: Implement Clone for immutable iterators

This commit is contained in:
blake2-ppc 2013-07-18 18:46:37 +02:00
parent 24b6901b26
commit fe134b9509

View 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 @@ 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;
@ -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();