Skip to content

Commit a8820f7

Browse files
committed
rollup merge of rust-lang#20328: huonw/attack-of-the-clones
It's useful to be able to save state.
2 parents 899eb65 + b7832ed commit a8820f7

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
@@ -1014,6 +1014,7 @@ impl cmp::PartialEq for Bitv {
10141014
impl cmp::Eq for Bitv {}
10151015

10161016
/// An iterator for `Bitv`.
1017+
#[deriving(Clone)]
10171018
pub struct Bits<'a> {
10181019
bitv: &'a Bitv,
10191020
next_idx: uint,
@@ -1747,12 +1748,14 @@ impl<S: hash::Writer> hash::Hash<S> for BitvSet {
17471748
}
17481749

17491750
/// An iterator for `BitvSet`.
1751+
#[deriving(Clone)]
17501752
pub struct BitPositions<'a> {
17511753
set: &'a BitvSet,
17521754
next_idx: uint
17531755
}
17541756

17551757
/// An iterator combining two `BitvSet` iterators.
1758+
#[deriving(Clone)]
17561759
pub struct TwoBitPositions<'a> {
17571760
set: &'a BitvSet,
17581761
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
@@ -1135,6 +1135,17 @@ pub struct Iter<'a, T:'a> {
11351135
head: uint
11361136
}
11371137

1138+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
1139+
impl<'a, T> Clone for Iter<'a, T> {
1140+
fn clone(&self) -> Iter<'a, T> {
1141+
Iter {
1142+
ring: self.ring,
1143+
tail: self.tail,
1144+
head: self.head
1145+
}
1146+
}
1147+
}
1148+
11381149
impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
11391150
#[inline]
11401151
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
@@ -672,6 +672,17 @@ pub struct Iter<'a, V:'a> {
672672
iter: slice::Iter<'a, Option<V>>
673673
}
674674

675+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
676+
impl<'a, V> Clone for Iter<'a, V> {
677+
fn clone(&self) -> Iter<'a, V> {
678+
Iter {
679+
front: self.front,
680+
back: self.back,
681+
iter: self.iter.clone()
682+
}
683+
}
684+
}
685+
675686
iterator! { impl Iter -> (uint, &'a V), as_ref }
676687
double_ended_iterator! { impl Iter -> (uint, &'a V), as_ref }
677688

@@ -691,11 +702,29 @@ pub struct Keys<'a, V: 'a> {
691702
iter: Map<(uint, &'a V), uint, Iter<'a, V>, fn((uint, &'a V)) -> uint>
692703
}
693704

705+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
706+
impl<'a, V> Clone for Keys<'a, V> {
707+
fn clone(&self) -> Keys<'a, V> {
708+
Keys {
709+
iter: self.iter.clone()
710+
}
711+
}
712+
}
713+
694714
/// An iterator over the values of a map.
695715
pub struct Values<'a, V: 'a> {
696716
iter: Map<(uint, &'a V), &'a V, Iter<'a, V>, fn((uint, &'a V)) -> &'a V>
697717
}
698718

719+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
720+
impl<'a, V> Clone for Values<'a, V> {
721+
fn clone(&self) -> Values<'a, V> {
722+
Values {
723+
iter: self.iter.clone()
724+
}
725+
}
726+
}
727+
699728
/// A consuming iterator over the key-value pairs of a map.
700729
pub struct IntoIter<V> {
701730
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
@@ -1319,6 +1319,15 @@ pub struct Iter<'a, K: 'a, V: 'a> {
13191319
inner: table::Iter<'a, K, V>
13201320
}
13211321

1322+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
1323+
impl<'a, K, V> Clone for Entries<'a, K, V> {
1324+
fn clone(&self) -> Entries<'a, K, V> {
1325+
Entries {
1326+
inner: self.inner.clone()
1327+
}
1328+
}
1329+
}
1330+
13221331
/// HashMap mutable values iterator
13231332
pub struct IterMut<'a, K: 'a, V: 'a> {
13241333
inner: table::IterMut<'a, K, V>
@@ -1339,11 +1348,29 @@ pub struct Keys<'a, K: 'a, V: 'a> {
13391348
inner: Map<(&'a K, &'a V), &'a K, Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
13401349
}
13411350

1351+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
1352+
impl<'a, K, V> Clone for Keys<'a, K, V> {
1353+
fn clone(&self) -> Keys<'a, K, V> {
1354+
Keys {
1355+
inner: self.inner.clone()
1356+
}
1357+
}
1358+
}
1359+
13421360
/// HashMap values iterator
13431361
pub struct Values<'a, K: 'a, V: 'a> {
13441362
inner: Map<(&'a K, &'a V), &'a V, Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
13451363
}
13461364

1365+
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
1366+
impl<'a, K, V> Clone for Values<'a, K, V> {
1367+
fn clone(&self) -> Values<'a, K, V> {
1368+
Values {
1369+
inner: self.inner.clone()
1370+
}
1371+
}
1372+
}
1373+
13471374
/// HashMap drain iterator
13481375
pub struct Drain<'a, K: 'a, V: 'a> {
13491376
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 Iter<'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)