]> git.lizzy.rs Git - rust.git/commitdiff
std: Implement Clone for VecIterator and iterators using it
authorblake2-ppc <blake2-ppc>
Thu, 18 Jul 2013 15:38:17 +0000 (17:38 +0200)
committerblake2-ppc <blake2-ppc>
Sat, 20 Jul 2013 18:30:57 +0000 (20:30 +0200)
The theory is simple, the immutable iterators simply hold state
variables (indicies or pointers) into frozen containers. We can freely
clone these iterators, just like we can clone borrowed pointers.

VecIterator needs a manual impl to handle the lifetime struct member.

src/libstd/hashmap.rs
src/libstd/str.rs
src/libstd/vec.rs

index 182ee37202a658f48c56b8caf7f39f087bed33fc..56774560d1d113b6d607c14fea18e0f6ba56f443 100644 (file)
@@ -548,6 +548,7 @@ fn clone(&self) -> HashMap<K,V> {
 }
 
 /// HashMap iterator
+#[deriving(Clone)]
 pub struct HashMapIterator<'self, K, V> {
     priv iter: vec::VecIterator<'self, Option<Bucket<K, V>>>,
 }
@@ -563,6 +564,7 @@ pub struct HashMapConsumeIterator<K, V> {
 }
 
 /// HashSet iterator
+#[deriving(Clone)]
 pub struct HashSetIterator<'self, K> {
     priv iter: vec::VecIterator<'self, Option<Bucket<K, ()>>>,
 }
index 0811dab407ef5d2a2b47452cb60430e657464033..c74c1e18e6d2ee1bfd548494a8421f37c06adfee 100644 (file)
@@ -288,6 +288,7 @@ fn only_ascii(&self) -> bool {
 
 
 /// An iterator over the substrings of a string, separated by `sep`.
+#[deriving(Clone)]
 pub struct StrCharSplitIterator<'self,Sep> {
     priv string: &'self str,
     priv position: uint,
@@ -355,6 +356,7 @@ fn next(&mut self) -> Option<&'self str> {
 
 /// An iterator over the start and end indicies of the matches of a
 /// substring within a larger string
+#[deriving(Clone)]
 pub struct StrMatchesIndexIterator<'self> {
     priv haystack: &'self str,
     priv needle: &'self str,
@@ -363,6 +365,7 @@ pub struct StrMatchesIndexIterator<'self> {
 
 /// An iterator over the substrings of a string separated by a given
 /// search string
+#[deriving(Clone)]
 pub struct StrStrSplitIterator<'self> {
     priv it: StrMatchesIndexIterator<'self>,
     priv last_end: uint,
@@ -2269,6 +2272,7 @@ fn clone(&self) -> @str {
 
 /// External iterator for a string's characters. Use with the `std::iterator`
 /// module.
+#[deriving(Clone)]
 pub struct StrCharIterator<'self> {
     priv index: uint,
     priv string: &'self str,
@@ -2288,6 +2292,7 @@ fn next(&mut self) -> Option<char> {
 }
 /// External iterator for a string's characters in reverse order. Use
 /// with the `std::iterator` module.
+#[deriving(Clone)]
 pub struct StrCharRevIterator<'self> {
     priv index: uint,
     priv string: &'self str,
@@ -2308,6 +2313,7 @@ fn next(&mut self) -> Option<char> {
 
 /// External iterator for a string's bytes. Use with the `std::iterator`
 /// module.
+#[deriving(Clone)]
 pub struct StrBytesIterator<'self> {
     priv it: vec::VecIterator<'self, u8>
 }
@@ -2321,6 +2327,7 @@ fn next(&mut self) -> Option<u8> {
 
 /// External iterator for a string's bytes in reverse order. Use with
 /// the `std::iterator` module.
+#[deriving(Clone)]
 pub struct StrBytesRevIterator<'self> {
     priv it: vec::VecRevIterator<'self, u8>
 }
index 03e94a902c1a1b495b4237ab7dd46e634a389a9c..877ee65b4d67f88f893b7768728a75b91eb564a9 100644 (file)
@@ -2232,6 +2232,10 @@ pub struct VecIterator<'self, T> {
 double_ended_iterator!{impl VecIterator -> &'self T}
 pub type VecRevIterator<'self, T> = InvertIterator<&'self T, VecIterator<'self, T>>;
 
+impl<'self, T> Clone for VecIterator<'self, T> {
+    fn clone(&self) -> VecIterator<'self, T> { *self }
+}
+
 //iterator!{struct VecMutIterator -> *mut T, &'self mut T}
 /// An iterator for mutating the elements of a vector.
 pub struct VecMutIterator<'self, T> {
@@ -2244,6 +2248,7 @@ pub struct VecMutIterator<'self, T> {
 pub type VecMutRevIterator<'self, T> = InvertIterator<&'self mut T, VecMutIterator<'self, T>>;
 
 /// An iterator that moves out of a vector.
+#[deriving(Clone)]
 pub struct VecConsumeIterator<T> {
     priv v: ~[T],
     priv idx: uint,
@@ -2270,6 +2275,7 @@ fn next(&mut self) -> Option<T> {
 }
 
 /// An iterator that moves out of a vector in reverse order.
+#[deriving(Clone)]
 pub struct VecConsumeRevIterator<T> {
     priv v: ~[T]
 }
@@ -3185,6 +3191,17 @@ fn test_iter_size_hints() {
         assert_eq!(xs.mut_rev_iter().size_hint(), (5, Some(5)));
     }
 
+    #[test]
+    fn test_iter_clone() {
+        let xs = [1, 2, 5];
+        let mut it = xs.iter();
+        it.next();
+        let mut jt = it.clone();
+        assert_eq!(it.next(), jt.next());
+        assert_eq!(it.next(), jt.next());
+        assert_eq!(it.next(), jt.next());
+    }
+
     #[test]
     fn test_mut_iterator() {
         use iterator::*;