Skip to content

Commit 43bbade

Browse files
bors[bot]phimuemueKeenS
authored
Merge #391 #397
391: Simplify `Clone` implementations r=jswrenn a=phimuemue Use a macro `clone_fields`, similar to `debug_fmt_fields`. I replaced existing implementations of `clone_fields` and simplified the macro's parameters. 397: don't require ParitalEq to the Item of DedupBy r=jswrenn a=KeenS `PartialEq` is't needed because `dedup_pred` take its place. Co-authored-by: philipp <[email protected]> Co-authored-by: κeen <[email protected]>
3 parents bfbe01d + 27021b0 + 31c3544 commit 43bbade

File tree

9 files changed

+22
-72
lines changed

9 files changed

+22
-72
lines changed

src/adaptors/mod.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@ use std::iter::{Fuse, Peekable, FromIterator};
1414
use std::marker::PhantomData;
1515
use size_hint;
1616

17-
macro_rules! clone_fields {
18-
($name:ident, $base:expr, $($field:ident),+) => (
19-
$name {
20-
$(
21-
$field : $base . $field .clone()
22-
),*
23-
}
24-
);
25-
}
26-
2717
/// An iterator adaptor that alternates elements from two iterators until both
2818
/// run out.
2919
///
@@ -555,9 +545,7 @@ impl<I, J, F> Clone for MergeBy<I, J, F>
555545
Peekable<J>: Clone,
556546
F: Clone
557547
{
558-
fn clone(&self) -> Self {
559-
clone_fields!(MergeBy, self, a, b, fused, cmp)
560-
}
548+
clone_fields!(a, b, fused, cmp);
561549
}
562550

563551
impl<I, J, F> Iterator for MergeBy<I, J, F>
@@ -650,9 +638,7 @@ impl<I: Clone, F: Clone> Clone for Coalesce<I, F>
650638
where I: Iterator,
651639
I::Item: Clone
652640
{
653-
fn clone(&self) -> Self {
654-
clone_fields!(Coalesce, self, iter, f)
655-
}
641+
clone_fields!(iter, f);
656642
}
657643

658644
impl<I, F> fmt::Debug for Coalesce<I, F>
@@ -729,9 +715,7 @@ impl<I: Clone, Pred: Clone> Clone for DedupBy<I, Pred>
729715
where I: Iterator,
730716
I::Item: Clone,
731717
{
732-
fn clone(&self) -> Self {
733-
clone_fields!(DedupBy, self, iter, dedup_pred)
734-
}
718+
clone_fields!(iter, dedup_pred);
735719
}
736720

737721
/// Create a new `DedupBy`.
@@ -763,7 +747,6 @@ impl<I, Pred> fmt::Debug for DedupBy<I, Pred>
763747

764748
impl<I, Pred> Iterator for DedupBy<I, Pred>
765749
where I: Iterator,
766-
I::Item: PartialEq,
767750
Pred: DedupPredicate<I::Item>,
768751
{
769752
type Item = I::Item;

src/combinations.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@ impl<I> Clone for Combinations<I>
1717
where I: Clone + Iterator,
1818
I::Item: Clone,
1919
{
20-
fn clone(&self) -> Self {
21-
Combinations {
22-
k: self.k,
23-
indices: self.indices.clone(),
24-
pool: self.pool.clone(),
25-
first: self.first,
26-
}
27-
}
20+
clone_fields!(k, indices, pool, first);
2821
}
2922

3023
impl<I> fmt::Debug for Combinations<I>

src/cons_tuples_impl.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ pub struct ConsTuples<I, J>
5252
impl<I, J> Clone for ConsTuples<I, J>
5353
where I: Clone + Iterator<Item=J>,
5454
{
55-
fn clone(&self) -> Self {
56-
ConsTuples {
57-
iter: self.iter.clone(),
58-
}
59-
}
55+
clone_fields!(iter);
6056
}
6157

6258
/// Create an iterator that maps for example iterators of

src/impl_macros.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ macro_rules! debug_fmt_fields {
1212
}
1313
}
1414
}
15+
16+
macro_rules! clone_fields {
17+
($($field:ident),*) => {
18+
fn clone(&self) -> Self {
19+
Self {
20+
$($field: self.$field.clone(),)*
21+
}
22+
}
23+
}
24+
}

src/kmerge_impl.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@ use Itertools;
55
use std::mem::replace;
66
use std::fmt;
77

8-
macro_rules! clone_fields {
9-
($name:ident, $base:expr, $($field:ident),+) => (
10-
$name {
11-
$(
12-
$field : $base . $field .clone()
13-
),*
14-
}
15-
);
16-
}
17-
188
/// Head element and Tail iterator pair
199
///
2010
/// `PartialEq`, `Eq`, `PartialOrd` and `Ord` are implemented by comparing sequences based on
@@ -65,9 +55,7 @@ impl<I> Clone for HeadTail<I>
6555
where I: Iterator + Clone,
6656
I::Item: Clone
6757
{
68-
fn clone(&self) -> Self {
69-
clone_fields!(HeadTail, self, head, tail)
70-
}
58+
clone_fields!(head, tail);
7159
}
7260

7361
/// Make `data` a heap (min-heap w.r.t the sorting).
@@ -197,9 +185,7 @@ impl<I, F> Clone for KMergeBy<I, F>
197185
I::Item: Clone,
198186
F: Clone,
199187
{
200-
fn clone(&self) -> KMergeBy<I, F> {
201-
clone_fields!(KMergeBy, self, heap, less_than)
202-
}
188+
clone_fields!(heap, less_than);
203189
}
204190

205191
impl<I, F> Iterator for KMergeBy<I, F>

src/merge_join.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,7 @@ impl<I, J, F> Clone for MergeJoinBy<I, J, F>
3838
J::Item: Clone,
3939
F: Clone,
4040
{
41-
fn clone(&self) -> Self {
42-
MergeJoinBy {
43-
left: self.left.clone(),
44-
right: self.right.clone(),
45-
cmp_fn: self.cmp_fn.clone(),
46-
}
47-
}
41+
clone_fields!(left, right, cmp_fn);
4842
}
4943

5044
impl<I, J, F> fmt::Debug for MergeJoinBy<I, J, F>

src/permutations.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ impl<I> Clone for Permutations<I>
1818
where I: Clone + Iterator,
1919
I::Item: Clone,
2020
{
21-
fn clone(&self) -> Self {
22-
Permutations {
23-
vals: self.vals.clone(),
24-
state: self.state.clone(),
25-
}
26-
}
21+
clone_fields!(vals, state);
2722
}
2823

2924
#[derive(Clone, Debug)]
@@ -281,4 +276,4 @@ impl CompleteState {
281276
}
282277
}
283278
}
284-
}
279+
}

src/rciter_impl.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ pub fn rciter<I>(iterable: I) -> RcIter<I::IntoIter>
5252

5353
impl<I> Clone for RcIter<I> {
5454
#[inline]
55-
fn clone(&self) -> RcIter<I> {
56-
RcIter { rciter: self.rciter.clone() }
57-
}
55+
clone_fields!(rciter);
5856
}
5957

6058
impl<A, I> Iterator for RcIter<I>

src/with_position.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ impl<I> Clone for WithPosition<I>
1717
where I: Clone + Iterator,
1818
I::Item: Clone,
1919
{
20-
fn clone(&self) -> Self {
21-
WithPosition {
22-
handled_first: self.handled_first,
23-
peekable: self.peekable.clone(),
24-
}
25-
}
20+
clone_fields!(handled_first, peekable);
2621
}
2722

2823
/// Create a new `WithPosition` iterator.

0 commit comments

Comments
 (0)