Skip to content

Implement Clone for a large number of iterators & other adaptors. #20328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/libcollections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,13 @@ pub struct Iter <'a, T: 'a> {
iter: slice::Iter<'a, T>,
}

// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
impl<'a, T> Clone for Iter<'a, T> {
fn clone(&self) -> Iter<'a, T> {
Iter { iter: self.iter.clone() }
}
}

impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
#[inline]
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
Expand Down
3 changes: 3 additions & 0 deletions src/libcollections/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ impl cmp::PartialEq for Bitv {
impl cmp::Eq for Bitv {}

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

/// An iterator for `BitvSet`.
#[deriving(Clone)]
pub struct BitPositions<'a> {
set: &'a BitvSet,
next_idx: uint
}

/// An iterator combining two `BitvSet` iterators.
#[deriving(Clone)]
pub struct TwoBitPositions<'a> {
set: &'a BitvSet,
other: &'a BitvSet,
Expand Down
10 changes: 10 additions & 0 deletions src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ pub struct Iter<E> {
bits: uint,
}

// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
impl<E> Clone for Iter<E> {
fn clone(&self) -> Iter<E> {
Iter {
index: self.index,
bits: self.bits,
}
}
}

impl<E:CLike> Iter<E> {
fn new(bits: uint) -> Iter<E> {
Iter { index: 0, bits: bits }
Expand Down
11 changes: 11 additions & 0 deletions src/libcollections/ring_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,17 @@ pub struct Iter<'a, T:'a> {
head: uint
}

// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
impl<'a, T> Clone for Iter<'a, T> {
fn clone(&self) -> Iter<'a, T> {
Iter {
ring: self.ring,
tail: self.tail,
head: self.head
}
}
}

impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
#[inline]
fn next(&mut self) -> Option<&'a T> {
Expand Down
6 changes: 4 additions & 2 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ impl<'a, T: Clone, V: AsSlice<T>> VectorVector<T> for [V] {
///
/// The last generated swap is always (0, 1), and it returns the
/// sequence to its initial order.
#[deriving(Clone)]
pub struct ElementSwaps {
sdir: Vec<SizeDirection>,
/// If `true`, emit the last swap that returns the sequence to initial
Expand All @@ -177,11 +178,11 @@ impl ElementSwaps {
}
}

#[deriving(Copy)]
#[deriving(Copy, Clone)]
enum Direction { Pos, Neg }

/// An `Index` and `Direction` together.
#[deriving(Copy)]
#[deriving(Copy, Clone)]
struct SizeDirection {
size: uint,
dir: Direction,
Expand Down Expand Up @@ -247,6 +248,7 @@ impl Iterator<(uint, uint)> for ElementSwaps {
/// swap applied.
///
/// Generates even and odd permutations alternately.
#[deriving(Clone)]
pub struct Permutations<T> {
swaps: ElementSwaps,
v: Vec<T>,
Expand Down
29 changes: 29 additions & 0 deletions src/libcollections/vec_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,17 @@ pub struct Iter<'a, V:'a> {
iter: slice::Iter<'a, Option<V>>
}

// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
impl<'a, V> Clone for Iter<'a, V> {
fn clone(&self) -> Iter<'a, V> {
Iter {
front: self.front,
back: self.back,
iter: self.iter.clone()
}
}
}

iterator! { impl Iter -> (uint, &'a V), as_ref }
double_ended_iterator! { impl Iter -> (uint, &'a V), as_ref }

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

// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
impl<'a, V> Clone for Keys<'a, V> {
fn clone(&self) -> Keys<'a, V> {
Keys {
iter: self.iter.clone()
}
}
}

/// An iterator over the values of a map.
pub struct Values<'a, V: 'a> {
iter: Map<(uint, &'a V), &'a V, Iter<'a, V>, fn((uint, &'a V)) -> &'a V>
}

// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
impl<'a, V> Clone for Values<'a, V> {
fn clone(&self) -> Values<'a, V> {
Values {
iter: self.iter.clone()
}
}
}

/// A consuming iterator over the key-value pairs of a map.
pub struct IntoIter<V> {
iter: FilterMap<
Expand Down
5 changes: 4 additions & 1 deletion src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,13 @@ impl Char for char {

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

#[deriving(Clone)]
enum EscapeUnicodeState {
Backslash,
Type,
Expand Down Expand Up @@ -486,10 +488,12 @@ impl Iterator<char> for EscapeUnicode {

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

#[deriving(Clone)]
enum EscapeDefaultState {
Backslash(char),
Char(char),
Expand All @@ -513,4 +517,3 @@ impl Iterator<char> for EscapeDefault {
}
}
}

7 changes: 7 additions & 0 deletions src/libregex/re.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ impl Regex {

}

#[deriving(Clone)]
pub enum NamesIter<'a> {
NamesIterNative(::std::slice::Iter<'a, Option<&'static str>>),
NamesIterDynamic(::std::slice::Iter<'a, Option<String>>)
Expand Down Expand Up @@ -595,6 +596,7 @@ impl<F> Replacer for F where F: FnMut(&Captures) -> String {
///
/// `'r` is the lifetime of the compiled expression and `'t` is the lifetime
/// of the string being split.
#[deriving(Clone)]
pub struct RegexSplits<'r, 't> {
finder: FindMatches<'r, 't>,
last: uint,
Expand Down Expand Up @@ -628,6 +630,7 @@ impl<'r, 't> Iterator<&'t str> for RegexSplits<'r, 't> {
///
/// `'r` is the lifetime of the compiled expression and `'t` is the lifetime
/// of the string being split.
#[deriving(Clone)]
pub struct RegexSplitsN<'r, 't> {
splits: RegexSplits<'r, 't>,
cur: uint,
Expand Down Expand Up @@ -791,6 +794,7 @@ impl<'t> Captures<'t> {
/// expression.
///
/// `'t` is the lifetime of the matched text.
#[deriving(Clone)]
pub struct SubCaptures<'t> {
idx: uint,
caps: &'t Captures<'t>,
Expand All @@ -813,6 +817,7 @@ impl<'t> Iterator<&'t str> for SubCaptures<'t> {
/// Positions are byte indices in terms of the original string matched.
///
/// `'t` is the lifetime of the matched text.
#[deriving(Clone)]
pub struct SubCapturesPos<'t> {
idx: uint,
caps: &'t Captures<'t>,
Expand All @@ -836,6 +841,7 @@ impl<'t> Iterator<Option<(uint, uint)>> for SubCapturesPos<'t> {
///
/// `'r` is the lifetime of the compiled expression and `'t` is the lifetime
/// of the matched string.
#[deriving(Clone)]
pub struct FindCaptures<'r, 't> {
re: &'r Regex,
search: &'t str,
Expand Down Expand Up @@ -878,6 +884,7 @@ impl<'r, 't> Iterator<Captures<'t>> for FindCaptures<'r, 't> {
///
/// `'r` is the lifetime of the compiled expression and `'t` is the lifetime
/// of the matched string.
#[deriving(Clone)]
pub struct FindMatches<'r, 't> {
re: &'r Regex,
search: &'t str,
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ fn check_for_null(v: &[u8], buf: *mut libc::c_char) {
/// External iterator for a CString's bytes.
///
/// Use with the `std::iter` module.
#[allow(raw_pointer_deriving)]
#[deriving(Clone)]
pub struct CChars<'a> {
ptr: *const libc::c_char,
marker: marker::ContravariantLifetime<'a>,
Expand Down
27 changes: 27 additions & 0 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,15 @@ pub struct Entries<'a, K: 'a, V: 'a> {
inner: table::Entries<'a, K, V>
}

// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
impl<'a, K, V> Clone for Entries<'a, K, V> {
fn clone(&self) -> Entries<'a, K, V> {
Entries {
inner: self.inner.clone()
}
}
}

/// HashMap mutable values iterator
pub struct IterMut<'a, K: 'a, V: 'a> {
inner: table::IterMut<'a, K, V>
Expand All @@ -1329,11 +1338,29 @@ pub struct Keys<'a, K: 'a, V: 'a> {
inner: Map<(&'a K, &'a V), &'a K, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
}

// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
impl<'a, K, V> Clone for Keys<'a, K, V> {
fn clone(&self) -> Keys<'a, K, V> {
Keys {
inner: self.inner.clone()
}
}
}

/// HashMap values iterator
pub struct Values<'a, K: 'a, V: 'a> {
inner: Map<(&'a K, &'a V), &'a V, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
}

// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
impl<'a, K, V> Clone for Values<'a, K, V> {
fn clone(&self) -> Values<'a, K, V> {
Values {
inner: self.inner.clone()
}
}
}

/// HashMap drain iterator
pub struct Drain<'a, K: 'a, V: 'a> {
inner: iter::Map<
Expand Down
23 changes: 23 additions & 0 deletions src/libstd/collections/hash/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,18 @@ struct RawBuckets<'a, K, V> {
marker: marker::ContravariantLifetime<'a>,
}

// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
impl<'a, K, V> Clone for RawBuckets<'a, K, V> {
fn clone(&self) -> RawBuckets<'a, K, V> {
RawBuckets {
raw: self.raw,
hashes_end: self.hashes_end,
marker: marker::ContravariantLifetime,
}
}
}


impl<'a, K, V> Iterator<RawBucket<K, V>> for RawBuckets<'a, K, V> {
fn next(&mut self) -> Option<RawBucket<K, V>> {
while self.raw.hash != self.hashes_end {
Expand Down Expand Up @@ -775,6 +787,17 @@ pub struct Entries<'a, K: 'a, V: 'a> {
elems_left: uint,
}

// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
impl<'a, K, V> Clone for Entries<'a, K, V> {
fn clone(&self) -> Entries<'a, K, V> {
Entries {
iter: self.iter.clone(),
elems_left: self.elems_left
}
}
}


/// Iterator over mutable references to entries in a table.
pub struct IterMut<'a, K: 'a, V: 'a> {
iter: RawBuckets<'a, K, V>,
Expand Down
1 change: 1 addition & 0 deletions src/libstd/io/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ pub fn walk_dir(path: &Path) -> IoResult<Directories> {
}

/// An iterator that walks over a directory
#[deriving(Clone)]
pub struct Directories {
stack: Vec<Path>,
}
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/io/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl Writer for MultiWriter {

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

/// An adaptor converting an `Iterator<u8>` to a `Reader`.
#[deriving(Clone)]
pub struct IterReader<T> {
iter: T,
}
Expand Down