Skip to content

Commit fe134b9

Browse files
author
blake2-ppc
committed
dlist: Implement Clone for immutable iterators
1 parent 24b6901 commit fe134b9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/libextra/dlist.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct Node<T> {
4747
}
4848

4949
/// Double-ended DList iterator
50+
#[deriving(Clone)]
5051
pub struct DListIterator<'self, T> {
5152
priv head: &'self Link<T>,
5253
priv tail: Rawlink<Node<T>>,
@@ -62,6 +63,7 @@ pub struct MutDListIterator<'self, T> {
6263
}
6364

6465
/// DList consuming iterator
66+
#[deriving(Clone)]
6567
pub struct ConsumeIterator<T> {
6668
priv list: DList<T>
6769
}
@@ -93,6 +95,13 @@ impl<T> Rawlink<T> {
9395
}
9496
}
9597

98+
impl<T> Clone for Rawlink<T> {
99+
#[inline]
100+
fn clone(&self) -> Rawlink<T> {
101+
Rawlink{p: self.p}
102+
}
103+
}
104+
96105
/// Set the .prev field on `next`, then return `Some(next)`
97106
fn link_with_prev<T>(mut next: ~Node<T>, prev: Rawlink<Node<T>>) -> Link<T> {
98107
next.prev = prev;
@@ -686,6 +695,20 @@ mod tests {
686695
assert_eq!(it.next(), None);
687696
}
688697

698+
#[test]
699+
fn test_iterator_clone() {
700+
let mut n = DList::new();
701+
n.push_back(2);
702+
n.push_back(3);
703+
n.push_back(4);
704+
let mut it = n.iter();
705+
it.next();
706+
let mut jt = it.clone();
707+
assert_eq!(it.next(), jt.next());
708+
assert_eq!(it.next_back(), jt.next_back());
709+
assert_eq!(it.next(), jt.next());
710+
}
711+
689712
#[test]
690713
fn test_iterator_double_end() {
691714
let mut n = DList::new();

0 commit comments

Comments
 (0)