Skip to content

Commit

Permalink
Merge #391 #397
Browse files Browse the repository at this point in the history
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 <descpl@yahoo.de>
Co-authored-by: κeen <3han5chou7@gmail.com>
  • Loading branch information
3 people committed Jan 14, 2020
3 parents bfbe01d + 27021b0 + 31c3544 commit 43bbade
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 72 deletions.
23 changes: 3 additions & 20 deletions src/adaptors/mod.rs
Expand Up @@ -14,16 +14,6 @@ use std::iter::{Fuse, Peekable, FromIterator};
use std::marker::PhantomData;
use size_hint;

macro_rules! clone_fields {
($name:ident, $base:expr, $($field:ident),+) => (
$name {
$(
$field : $base . $field .clone()
),*
}
);
}

/// An iterator adaptor that alternates elements from two iterators until both
/// run out.
///
Expand Down Expand Up @@ -555,9 +545,7 @@ impl<I, J, F> Clone for MergeBy<I, J, F>
Peekable<J>: Clone,
F: Clone
{
fn clone(&self) -> Self {
clone_fields!(MergeBy, self, a, b, fused, cmp)
}
clone_fields!(a, b, fused, cmp);
}

impl<I, J, F> Iterator for MergeBy<I, J, F>
Expand Down Expand Up @@ -650,9 +638,7 @@ impl<I: Clone, F: Clone> Clone for Coalesce<I, F>
where I: Iterator,
I::Item: Clone
{
fn clone(&self) -> Self {
clone_fields!(Coalesce, self, iter, f)
}
clone_fields!(iter, f);
}

impl<I, F> fmt::Debug for Coalesce<I, F>
Expand Down Expand Up @@ -729,9 +715,7 @@ impl<I: Clone, Pred: Clone> Clone for DedupBy<I, Pred>
where I: Iterator,
I::Item: Clone,
{
fn clone(&self) -> Self {
clone_fields!(DedupBy, self, iter, dedup_pred)
}
clone_fields!(iter, dedup_pred);
}

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

impl<I, Pred> Iterator for DedupBy<I, Pred>
where I: Iterator,
I::Item: PartialEq,
Pred: DedupPredicate<I::Item>,
{
type Item = I::Item;
Expand Down
9 changes: 1 addition & 8 deletions src/combinations.rs
Expand Up @@ -17,14 +17,7 @@ impl<I> Clone for Combinations<I>
where I: Clone + Iterator,
I::Item: Clone,
{
fn clone(&self) -> Self {
Combinations {
k: self.k,
indices: self.indices.clone(),
pool: self.pool.clone(),
first: self.first,
}
}
clone_fields!(k, indices, pool, first);
}

impl<I> fmt::Debug for Combinations<I>
Expand Down
6 changes: 1 addition & 5 deletions src/cons_tuples_impl.rs
Expand Up @@ -52,11 +52,7 @@ pub struct ConsTuples<I, J>
impl<I, J> Clone for ConsTuples<I, J>
where I: Clone + Iterator<Item=J>,
{
fn clone(&self) -> Self {
ConsTuples {
iter: self.iter.clone(),
}
}
clone_fields!(iter);
}

/// Create an iterator that maps for example iterators of
Expand Down
10 changes: 10 additions & 0 deletions src/impl_macros.rs
Expand Up @@ -12,3 +12,13 @@ macro_rules! debug_fmt_fields {
}
}
}

macro_rules! clone_fields {
($($field:ident),*) => {
fn clone(&self) -> Self {
Self {
$($field: self.$field.clone(),)*
}
}
}
}
18 changes: 2 additions & 16 deletions src/kmerge_impl.rs
Expand Up @@ -5,16 +5,6 @@ use Itertools;
use std::mem::replace;
use std::fmt;

macro_rules! clone_fields {
($name:ident, $base:expr, $($field:ident),+) => (
$name {
$(
$field : $base . $field .clone()
),*
}
);
}

/// Head element and Tail iterator pair
///
/// `PartialEq`, `Eq`, `PartialOrd` and `Ord` are implemented by comparing sequences based on
Expand Down Expand Up @@ -65,9 +55,7 @@ impl<I> Clone for HeadTail<I>
where I: Iterator + Clone,
I::Item: Clone
{
fn clone(&self) -> Self {
clone_fields!(HeadTail, self, head, tail)
}
clone_fields!(head, tail);
}

/// Make `data` a heap (min-heap w.r.t the sorting).
Expand Down Expand Up @@ -197,9 +185,7 @@ impl<I, F> Clone for KMergeBy<I, F>
I::Item: Clone,
F: Clone,
{
fn clone(&self) -> KMergeBy<I, F> {
clone_fields!(KMergeBy, self, heap, less_than)
}
clone_fields!(heap, less_than);
}

impl<I, F> Iterator for KMergeBy<I, F>
Expand Down
8 changes: 1 addition & 7 deletions src/merge_join.rs
Expand Up @@ -38,13 +38,7 @@ impl<I, J, F> Clone for MergeJoinBy<I, J, F>
J::Item: Clone,
F: Clone,
{
fn clone(&self) -> Self {
MergeJoinBy {
left: self.left.clone(),
right: self.right.clone(),
cmp_fn: self.cmp_fn.clone(),
}
}
clone_fields!(left, right, cmp_fn);
}

impl<I, J, F> fmt::Debug for MergeJoinBy<I, J, F>
Expand Down
9 changes: 2 additions & 7 deletions src/permutations.rs
Expand Up @@ -18,12 +18,7 @@ impl<I> Clone for Permutations<I>
where I: Clone + Iterator,
I::Item: Clone,
{
fn clone(&self) -> Self {
Permutations {
vals: self.vals.clone(),
state: self.state.clone(),
}
}
clone_fields!(vals, state);
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -281,4 +276,4 @@ impl CompleteState {
}
}
}
}
}
4 changes: 1 addition & 3 deletions src/rciter_impl.rs
Expand Up @@ -52,9 +52,7 @@ pub fn rciter<I>(iterable: I) -> RcIter<I::IntoIter>

impl<I> Clone for RcIter<I> {
#[inline]
fn clone(&self) -> RcIter<I> {
RcIter { rciter: self.rciter.clone() }
}
clone_fields!(rciter);
}

impl<A, I> Iterator for RcIter<I>
Expand Down
7 changes: 1 addition & 6 deletions src/with_position.rs
Expand Up @@ -17,12 +17,7 @@ impl<I> Clone for WithPosition<I>
where I: Clone + Iterator,
I::Item: Clone,
{
fn clone(&self) -> Self {
WithPosition {
handled_first: self.handled_first,
peekable: self.peekable.clone(),
}
}
clone_fields!(handled_first, peekable);
}

/// Create a new `WithPosition` iterator.
Expand Down

0 comments on commit 43bbade

Please sign in to comment.