Skip to content

Commit b7832ed

Browse files
committed
Implement Clone for a large number of iterators & other adaptors.
It's useful to be able to save state.
1 parent fea5aa6 commit b7832ed

File tree

13 files changed

+130
-3
lines changed

13 files changed

+130
-3
lines changed

src/libcollections/binary_heap.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,13 @@ pub struct Iter <'a, T: 'a> {
563563
iter: slice::Iter<'a, T>,
564564
}
565565

566+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
567+
impl<'a, T> Clone for Iter<'a, T> {
568+
fn clone(&self) -> Iter<'a, T> {
569+
Iter { iter: self.iter.clone() }
570+
}
571+
}
572+
566573
impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
567574
#[inline]
568575
fn next(&mut self) -> Option<&'a T> { self.iter.next() }

src/libcollections/bit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@ impl cmp::PartialEq for Bitv {
10101010
impl cmp::Eq for Bitv {}
10111011

10121012
/// An iterator for `Bitv`.
1013+
#[deriving(Clone)]
10131014
pub struct Bits<'a> {
10141015
bitv: &'a Bitv,
10151016
next_idx: uint,
@@ -1739,12 +1740,14 @@ impl<S: hash::Writer> hash::Hash<S> for BitvSet {
17391740
}
17401741

17411742
/// An iterator for `BitvSet`.
1743+
#[deriving(Clone)]
17421744
pub struct BitPositions<'a> {
17431745
set: &'a BitvSet,
17441746
next_idx: uint
17451747
}
17461748

17471749
/// An iterator combining two `BitvSet` iterators.
1750+
#[deriving(Clone)]
17481751
pub struct TwoBitPositions<'a> {
17491752
set: &'a BitvSet,
17501753
other: &'a BitvSet,

src/libcollections/enum_set.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ pub struct Iter<E> {
213213
bits: uint,
214214
}
215215

216+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
217+
impl<E> Clone for Iter<E> {
218+
fn clone(&self) -> Iter<E> {
219+
Iter {
220+
index: self.index,
221+
bits: self.bits,
222+
}
223+
}
224+
}
225+
216226
impl<E:CLike> Iter<E> {
217227
fn new(bits: uint) -> Iter<E> {
218228
Iter { index: 0, bits: bits }

src/libcollections/ring_buf.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,17 @@ pub struct Iter<'a, T:'a> {
11291129
head: uint
11301130
}
11311131

1132+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
1133+
impl<'a, T> Clone for Iter<'a, T> {
1134+
fn clone(&self) -> Iter<'a, T> {
1135+
Iter {
1136+
ring: self.ring,
1137+
tail: self.tail,
1138+
head: self.head
1139+
}
1140+
}
1141+
}
1142+
11321143
impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
11331144
#[inline]
11341145
fn next(&mut self) -> Option<&'a T> {

src/libcollections/slice.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ impl<'a, T: Clone, V: AsSlice<T>> VectorVector<T> for [V] {
155155
///
156156
/// The last generated swap is always (0, 1), and it returns the
157157
/// sequence to its initial order.
158+
#[deriving(Clone)]
158159
pub struct ElementSwaps {
159160
sdir: Vec<SizeDirection>,
160161
/// If `true`, emit the last swap that returns the sequence to initial
@@ -177,11 +178,11 @@ impl ElementSwaps {
177178
}
178179
}
179180

180-
#[deriving(Copy)]
181+
#[deriving(Copy, Clone)]
181182
enum Direction { Pos, Neg }
182183

183184
/// An `Index` and `Direction` together.
184-
#[deriving(Copy)]
185+
#[deriving(Copy, Clone)]
185186
struct SizeDirection {
186187
size: uint,
187188
dir: Direction,
@@ -247,6 +248,7 @@ impl Iterator<(uint, uint)> for ElementSwaps {
247248
/// swap applied.
248249
///
249250
/// Generates even and odd permutations alternately.
251+
#[deriving(Clone)]
250252
pub struct Permutations<T> {
251253
swaps: ElementSwaps,
252254
v: Vec<T>,

src/libcollections/vec_map.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,17 @@ pub struct Iter<'a, V:'a> {
667667
iter: slice::Iter<'a, Option<V>>
668668
}
669669

670+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
671+
impl<'a, V> Clone for Iter<'a, V> {
672+
fn clone(&self) -> Iter<'a, V> {
673+
Iter {
674+
front: self.front,
675+
back: self.back,
676+
iter: self.iter.clone()
677+
}
678+
}
679+
}
680+
670681
iterator! { impl Iter -> (uint, &'a V), as_ref }
671682
double_ended_iterator! { impl Iter -> (uint, &'a V), as_ref }
672683

@@ -686,11 +697,29 @@ pub struct Keys<'a, V: 'a> {
686697
iter: Map<(uint, &'a V), uint, Iter<'a, V>, fn((uint, &'a V)) -> uint>
687698
}
688699

700+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
701+
impl<'a, V> Clone for Keys<'a, V> {
702+
fn clone(&self) -> Keys<'a, V> {
703+
Keys {
704+
iter: self.iter.clone()
705+
}
706+
}
707+
}
708+
689709
/// An iterator over the values of a map.
690710
pub struct Values<'a, V: 'a> {
691711
iter: Map<(uint, &'a V), &'a V, Iter<'a, V>, fn((uint, &'a V)) -> &'a V>
692712
}
693713

714+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
715+
impl<'a, V> Clone for Values<'a, V> {
716+
fn clone(&self) -> Values<'a, V> {
717+
Values {
718+
iter: self.iter.clone()
719+
}
720+
}
721+
}
722+
694723
/// A consuming iterator over the key-value pairs of a map.
695724
pub struct IntoIter<V> {
696725
iter: FilterMap<

src/libcore/char.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,13 @@ impl Char for char {
430430

431431
/// An iterator over the characters that represent a `char`, as escaped by
432432
/// Rust's unicode escaping rules.
433+
#[deriving(Clone)]
433434
pub struct EscapeUnicode {
434435
c: char,
435436
state: EscapeUnicodeState
436437
}
437438

439+
#[deriving(Clone)]
438440
enum EscapeUnicodeState {
439441
Backslash,
440442
Type,
@@ -486,10 +488,12 @@ impl Iterator<char> for EscapeUnicode {
486488

487489
/// An iterator over the characters that represent a `char`, escaped
488490
/// for maximum portability.
491+
#[deriving(Clone)]
489492
pub struct EscapeDefault {
490493
state: EscapeDefaultState
491494
}
492495

496+
#[deriving(Clone)]
493497
enum EscapeDefaultState {
494498
Backslash(char),
495499
Char(char),
@@ -513,4 +517,3 @@ impl Iterator<char> for EscapeDefault {
513517
}
514518
}
515519
}
516-

src/libregex/re.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ impl Regex {
539539

540540
}
541541

542+
#[deriving(Clone)]
542543
pub enum NamesIter<'a> {
543544
NamesIterNative(::std::slice::Iter<'a, Option<&'static str>>),
544545
NamesIterDynamic(::std::slice::Iter<'a, Option<String>>)
@@ -595,6 +596,7 @@ impl<F> Replacer for F where F: FnMut(&Captures) -> String {
595596
///
596597
/// `'r` is the lifetime of the compiled expression and `'t` is the lifetime
597598
/// of the string being split.
599+
#[deriving(Clone)]
598600
pub struct RegexSplits<'r, 't> {
599601
finder: FindMatches<'r, 't>,
600602
last: uint,
@@ -628,6 +630,7 @@ impl<'r, 't> Iterator<&'t str> for RegexSplits<'r, 't> {
628630
///
629631
/// `'r` is the lifetime of the compiled expression and `'t` is the lifetime
630632
/// of the string being split.
633+
#[deriving(Clone)]
631634
pub struct RegexSplitsN<'r, 't> {
632635
splits: RegexSplits<'r, 't>,
633636
cur: uint,
@@ -791,6 +794,7 @@ impl<'t> Captures<'t> {
791794
/// expression.
792795
///
793796
/// `'t` is the lifetime of the matched text.
797+
#[deriving(Clone)]
794798
pub struct SubCaptures<'t> {
795799
idx: uint,
796800
caps: &'t Captures<'t>,
@@ -813,6 +817,7 @@ impl<'t> Iterator<&'t str> for SubCaptures<'t> {
813817
/// Positions are byte indices in terms of the original string matched.
814818
///
815819
/// `'t` is the lifetime of the matched text.
820+
#[deriving(Clone)]
816821
pub struct SubCapturesPos<'t> {
817822
idx: uint,
818823
caps: &'t Captures<'t>,
@@ -836,6 +841,7 @@ impl<'t> Iterator<Option<(uint, uint)>> for SubCapturesPos<'t> {
836841
///
837842
/// `'r` is the lifetime of the compiled expression and `'t` is the lifetime
838843
/// of the matched string.
844+
#[deriving(Clone)]
839845
pub struct FindCaptures<'r, 't> {
840846
re: &'r Regex,
841847
search: &'t str,
@@ -878,6 +884,7 @@ impl<'r, 't> Iterator<Captures<'t>> for FindCaptures<'r, 't> {
878884
///
879885
/// `'r` is the lifetime of the compiled expression and `'t` is the lifetime
880886
/// of the matched string.
887+
#[deriving(Clone)]
881888
pub struct FindMatches<'r, 't> {
882889
re: &'r Regex,
883890
search: &'t str,

src/libstd/c_str.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ fn check_for_null(v: &[u8], buf: *mut libc::c_char) {
486486
/// External iterator for a CString's bytes.
487487
///
488488
/// Use with the `std::iter` module.
489+
#[allow(raw_pointer_deriving)]
490+
#[deriving(Clone)]
489491
pub struct CChars<'a> {
490492
ptr: *const libc::c_char,
491493
marker: marker::ContravariantLifetime<'a>,

src/libstd/collections/hash/map.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,15 @@ pub struct Entries<'a, K: 'a, V: 'a> {
13091309
inner: table::Entries<'a, K, V>
13101310
}
13111311

1312+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
1313+
impl<'a, K, V> Clone for Entries<'a, K, V> {
1314+
fn clone(&self) -> Entries<'a, K, V> {
1315+
Entries {
1316+
inner: self.inner.clone()
1317+
}
1318+
}
1319+
}
1320+
13121321
/// HashMap mutable values iterator
13131322
pub struct IterMut<'a, K: 'a, V: 'a> {
13141323
inner: table::IterMut<'a, K, V>
@@ -1329,11 +1338,29 @@ pub struct Keys<'a, K: 'a, V: 'a> {
13291338
inner: Map<(&'a K, &'a V), &'a K, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
13301339
}
13311340

1341+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
1342+
impl<'a, K, V> Clone for Keys<'a, K, V> {
1343+
fn clone(&self) -> Keys<'a, K, V> {
1344+
Keys {
1345+
inner: self.inner.clone()
1346+
}
1347+
}
1348+
}
1349+
13321350
/// HashMap values iterator
13331351
pub struct Values<'a, K: 'a, V: 'a> {
13341352
inner: Map<(&'a K, &'a V), &'a V, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
13351353
}
13361354

1355+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
1356+
impl<'a, K, V> Clone for Values<'a, K, V> {
1357+
fn clone(&self) -> Values<'a, K, V> {
1358+
Values {
1359+
inner: self.inner.clone()
1360+
}
1361+
}
1362+
}
1363+
13371364
/// HashMap drain iterator
13381365
pub struct Drain<'a, K: 'a, V: 'a> {
13391366
inner: iter::Map<

src/libstd/collections/hash/table.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,18 @@ struct RawBuckets<'a, K, V> {
718718
marker: marker::ContravariantLifetime<'a>,
719719
}
720720

721+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
722+
impl<'a, K, V> Clone for RawBuckets<'a, K, V> {
723+
fn clone(&self) -> RawBuckets<'a, K, V> {
724+
RawBuckets {
725+
raw: self.raw,
726+
hashes_end: self.hashes_end,
727+
marker: marker::ContravariantLifetime,
728+
}
729+
}
730+
}
731+
732+
721733
impl<'a, K, V> Iterator<RawBucket<K, V>> for RawBuckets<'a, K, V> {
722734
fn next(&mut self) -> Option<RawBucket<K, V>> {
723735
while self.raw.hash != self.hashes_end {
@@ -775,6 +787,17 @@ pub struct Entries<'a, K: 'a, V: 'a> {
775787
elems_left: uint,
776788
}
777789

790+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
791+
impl<'a, K, V> Clone for Entries<'a, K, V> {
792+
fn clone(&self) -> Entries<'a, K, V> {
793+
Entries {
794+
iter: self.iter.clone(),
795+
elems_left: self.elems_left
796+
}
797+
}
798+
}
799+
800+
778801
/// Iterator over mutable references to entries in a table.
779802
pub struct IterMut<'a, K: 'a, V: 'a> {
780803
iter: RawBuckets<'a, K, V>,

src/libstd/io/fs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ pub fn walk_dir(path: &Path) -> IoResult<Directories> {
558558
}
559559

560560
/// An iterator that walks over a directory
561+
#[deriving(Clone)]
561562
pub struct Directories {
562563
stack: Vec<Path>,
563564
}

src/libstd/io/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ impl Writer for MultiWriter {
163163

164164
/// A `Reader` which chains input from multiple `Reader`s, reading each to
165165
/// completion before moving onto the next.
166+
#[deriving(Clone)]
166167
pub struct ChainedReader<I, R> {
167168
readers: I,
168169
cur_reader: Option<R>,
@@ -246,6 +247,7 @@ pub fn copy<R: Reader, W: Writer>(r: &mut R, w: &mut W) -> io::IoResult<()> {
246247
}
247248

248249
/// An adaptor converting an `Iterator<u8>` to a `Reader`.
250+
#[deriving(Clone)]
249251
pub struct IterReader<T> {
250252
iter: T,
251253
}

0 commit comments

Comments
 (0)