Skip to content

Commit 24b6901

Browse files
author
blake2-ppc
committed
std: Implement Clone for VecIterator and iterators using it
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.
1 parent ffe2623 commit 24b6901

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/libstd/hashmap.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ impl<K:Hash + Eq + Clone,V:Clone> Clone for HashMap<K,V> {
548548
}
549549

550550
/// HashMap iterator
551+
#[deriving(Clone)]
551552
pub struct HashMapIterator<'self, K, V> {
552553
priv iter: vec::VecIterator<'self, Option<Bucket<K, V>>>,
553554
}
@@ -563,6 +564,7 @@ pub struct HashMapConsumeIterator<K, V> {
563564
}
564565

565566
/// HashSet iterator
567+
#[deriving(Clone)]
566568
pub struct HashSetIterator<'self, K> {
567569
priv iter: vec::VecIterator<'self, Option<Bucket<K, ()>>>,
568570
}

src/libstd/str.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ impl<'self, C: CharEq> CharEq for &'self [C] {
288288

289289

290290
/// An iterator over the substrings of a string, separated by `sep`.
291+
#[deriving(Clone)]
291292
pub struct StrCharSplitIterator<'self,Sep> {
292293
priv string: &'self str,
293294
priv position: uint,
@@ -355,6 +356,7 @@ impl<'self, Sep: CharEq> Iterator<&'self str> for StrCharSplitIterator<'self, Se
355356

356357
/// An iterator over the start and end indicies of the matches of a
357358
/// substring within a larger string
359+
#[deriving(Clone)]
358360
pub struct StrMatchesIndexIterator<'self> {
359361
priv haystack: &'self str,
360362
priv needle: &'self str,
@@ -363,6 +365,7 @@ pub struct StrMatchesIndexIterator<'self> {
363365

364366
/// An iterator over the substrings of a string separated by a given
365367
/// search string
368+
#[deriving(Clone)]
366369
pub struct StrStrSplitIterator<'self> {
367370
priv it: StrMatchesIndexIterator<'self>,
368371
priv last_end: uint,
@@ -2269,6 +2272,7 @@ impl Clone for @str {
22692272
22702273
/// External iterator for a string's characters. Use with the `std::iterator`
22712274
/// module.
2275+
#[deriving(Clone)]
22722276
pub struct StrCharIterator<'self> {
22732277
priv index: uint,
22742278
priv string: &'self str,
@@ -2288,6 +2292,7 @@ impl<'self> Iterator<char> for StrCharIterator<'self> {
22882292
}
22892293
/// External iterator for a string's characters in reverse order. Use
22902294
/// with the `std::iterator` module.
2295+
#[deriving(Clone)]
22912296
pub struct StrCharRevIterator<'self> {
22922297
priv index: uint,
22932298
priv string: &'self str,
@@ -2308,6 +2313,7 @@ impl<'self> Iterator<char> for StrCharRevIterator<'self> {
23082313
23092314
/// External iterator for a string's bytes. Use with the `std::iterator`
23102315
/// module.
2316+
#[deriving(Clone)]
23112317
pub struct StrBytesIterator<'self> {
23122318
priv it: vec::VecIterator<'self, u8>
23132319
}
@@ -2321,6 +2327,7 @@ impl<'self> Iterator<u8> for StrBytesIterator<'self> {
23212327
23222328
/// External iterator for a string's bytes in reverse order. Use with
23232329
/// the `std::iterator` module.
2330+
#[deriving(Clone)]
23242331
pub struct StrBytesRevIterator<'self> {
23252332
priv it: vec::VecRevIterator<'self, u8>
23262333
}

src/libstd/vec.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,10 @@ iterator!{impl VecIterator -> &'self T}
22322232
double_ended_iterator!{impl VecIterator -> &'self T}
22332233
pub type VecRevIterator<'self, T> = InvertIterator<&'self T, VecIterator<'self, T>>;
22342234

2235+
impl<'self, T> Clone for VecIterator<'self, T> {
2236+
fn clone(&self) -> VecIterator<'self, T> { *self }
2237+
}
2238+
22352239
//iterator!{struct VecMutIterator -> *mut T, &'self mut T}
22362240
/// An iterator for mutating the elements of a vector.
22372241
pub struct VecMutIterator<'self, T> {
@@ -2244,6 +2248,7 @@ double_ended_iterator!{impl VecMutIterator -> &'self mut T}
22442248
pub type VecMutRevIterator<'self, T> = InvertIterator<&'self mut T, VecMutIterator<'self, T>>;
22452249

22462250
/// An iterator that moves out of a vector.
2251+
#[deriving(Clone)]
22472252
pub struct VecConsumeIterator<T> {
22482253
priv v: ~[T],
22492254
priv idx: uint,
@@ -2270,6 +2275,7 @@ impl<T> Iterator<T> for VecConsumeIterator<T> {
22702275
}
22712276

22722277
/// An iterator that moves out of a vector in reverse order.
2278+
#[deriving(Clone)]
22732279
pub struct VecConsumeRevIterator<T> {
22742280
priv v: ~[T]
22752281
}
@@ -3185,6 +3191,17 @@ mod tests {
31853191
assert_eq!(xs.mut_rev_iter().size_hint(), (5, Some(5)));
31863192
}
31873193

3194+
#[test]
3195+
fn test_iter_clone() {
3196+
let xs = [1, 2, 5];
3197+
let mut it = xs.iter();
3198+
it.next();
3199+
let mut jt = it.clone();
3200+
assert_eq!(it.next(), jt.next());
3201+
assert_eq!(it.next(), jt.next());
3202+
assert_eq!(it.next(), jt.next());
3203+
}
3204+
31883205
#[test]
31893206
fn test_mut_iterator() {
31903207
use iterator::*;

0 commit comments

Comments
 (0)