From 2f7a5f49029279bd734ef87fd65ae992b2ad9f40 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 13 Dec 2014 22:04:23 -0500 Subject: [PATCH] libcore: make iterator adaptors `Clone`able --- src/libcore/iter.rs | 124 +++++++++++++++++++++++++++++++ src/libcore/slice.rs | 11 +++ src/test/run-pass/issue-12677.rs | 17 +++++ 3 files changed, 152 insertions(+) create mode 100644 src/test/run-pass/issue-12677.rs diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 8ee2a8874bb03..1eac8a39ebaec 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -1388,6 +1388,19 @@ pub struct Map, F: FnMut(A) -> B> { f: F, } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl Clone for Map where + I: Clone + Iterator, + F: Clone + FnMut(A) -> B, +{ + fn clone(&self) -> Map { + Map { + iter: self.iter.clone(), + f: self.f.clone(), + } + } +} + impl Map where I: Iterator, F: FnMut(A) -> B { #[inline] fn do_map(&mut self, elt: Option) -> Option { @@ -1449,6 +1462,19 @@ pub struct Filter where I: Iterator, P: FnMut(&A) -> bool { predicate: P, } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl Clone for Filter where + I: Clone + Iterator, + P: Clone + FnMut(&A) -> bool, +{ + fn clone(&self) -> Filter { + Filter { + iter: self.iter.clone(), + predicate: self.predicate.clone(), + } + } +} + #[unstable = "trait is unstable"] impl Iterator for Filter where I: Iterator, P: FnMut(&A) -> bool { #[inline] @@ -1494,6 +1520,19 @@ pub struct FilterMap where I: Iterator, F: FnMut(A) -> Option f: F, } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl Clone for FilterMap where + I: Clone + Iterator, + F: Clone + FnMut(A) -> Option, +{ + fn clone(&self) -> FilterMap { + FilterMap { + iter: self.iter.clone(), + f: self.f.clone(), + } + } +} + #[unstable = "trait is unstable"] impl Iterator for FilterMap where I: Iterator, @@ -1657,6 +1696,20 @@ pub struct SkipWhile where I: Iterator, P: FnMut(&A) -> bool { predicate: P, } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl Clone for SkipWhile where + I: Clone + Iterator, + P: Clone + FnMut(&A) -> bool, +{ + fn clone(&self) -> SkipWhile { + SkipWhile { + iter: self.iter.clone(), + flag: self.flag, + predicate: self.predicate.clone(), + } + } +} + #[unstable = "trait is unstable"] impl Iterator for SkipWhile where I: Iterator, P: FnMut(&A) -> bool { #[inline] @@ -1686,6 +1739,20 @@ pub struct TakeWhile where I: Iterator, P: FnMut(&A) -> bool { predicate: P, } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl Clone for TakeWhile where + I: Clone + Iterator, + P: Clone + FnMut(&A) -> bool, +{ + fn clone(&self) -> TakeWhile { + TakeWhile { + iter: self.iter.clone(), + flag: self.flag, + predicate: self.predicate.clone(), + } + } +} + #[unstable = "trait is unstable"] impl Iterator for TakeWhile where I: Iterator, P: FnMut(&A) -> bool { #[inline] @@ -1847,6 +1914,21 @@ pub struct Scan where I: Iterator, F: FnMut(&mut St, A) -> Op pub state: St, } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl Clone for Scan where + I: Clone + Iterator, + St: Clone, + F: Clone + FnMut(&mut St, A) -> Option, +{ + fn clone(&self) -> Scan { + Scan { + iter: self.iter.clone(), + f: self.f.clone(), + state: self.state.clone(), + } + } +} + #[unstable = "trait is unstable"] impl Iterator for Scan where I: Iterator, @@ -1876,6 +1958,22 @@ pub struct FlatMap where I: Iterator, U: Iterator, F: FnMut backiter: Option, } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl Clone for FlatMap where + I: Clone + Iterator, + U: Clone + Iterator, + F: Clone + FnMut(A) -> U, +{ + fn clone(&self) -> FlatMap { + FlatMap { + iter: self.iter.clone(), + f: self.f.clone(), + frontiter: self.frontiter.clone(), + backiter: self.backiter.clone(), + } + } +} + #[unstable = "trait is unstable"] impl Iterator for FlatMap where I: Iterator, @@ -2020,6 +2118,19 @@ pub struct Inspect where I: Iterator, F: FnMut(&A) { f: F, } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl Clone for Inspect where + I: Clone + Iterator, + F: Clone + FnMut(&A), +{ + fn clone(&self) -> Inspect { + Inspect { + iter: self.iter.clone(), + f: self.f.clone(), + } + } +} + impl Inspect where I: Iterator, F: FnMut(&A) { #[inline] fn do_inspect(&mut self, elt: Option) -> Option { @@ -2114,6 +2225,19 @@ pub struct Unfold where F: FnMut(&mut St) -> Option { pub state: St, } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl Clone for Unfold where + F: Clone + FnMut(&mut St) -> Option, + St: Clone, +{ + fn clone(&self) -> Unfold { + Unfold { + f: self.f.clone(), + state: self.state.clone(), + } + } +} + #[experimental] impl Unfold where F: FnMut(&mut St) -> Option { /// Creates a new iterator with the specified closure as the "iterator diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 27a4328ba8017..059292c11f24f 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -1291,6 +1291,17 @@ pub struct Splits<'a, T:'a, P> where P: FnMut(&T) -> bool { finished: bool } +// FIXME(#19839) Remove in favor of `#[deriving(Clone)]` +impl<'a, T, P> Clone for Splits<'a, T, P> where P: Clone + FnMut(&T) -> bool { + fn clone(&self) -> Splits<'a, T, P> { + Splits { + v: self.v, + pred: self.pred.clone(), + finished: self.finished, + } + } +} + #[experimental = "needs review"] impl<'a, T, P> Iterator<&'a [T]> for Splits<'a, T, P> where P: FnMut(&T) -> bool { #[inline] diff --git a/src/test/run-pass/issue-12677.rs b/src/test/run-pass/issue-12677.rs new file mode 100644 index 0000000000000..ef68daa8ce592 --- /dev/null +++ b/src/test/run-pass/issue-12677.rs @@ -0,0 +1,17 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let s = "Hello"; + let first = s.bytes(); + let second = first.clone(); + + assert_eq!(first.collect::>(), second.collect::>()) +}