Skip to content

Commit f6226e7

Browse files
bors[bot]phimuemue
andauthored
Merge #513
513: Simplifications for tuple combinations r=jswrenn a=phimuemue This tries to simplify some things around tuple combinations: * The first 2 commits reuse our `ignore_ident` to avoid redundant macro parameters. * The remaining commits try to reduce the usage of macro arguments, which possibly helps us in the future to simplify the macros even further. One question I have: Why do we `Fuse` tuple-combinations for arity >=2, but not for arity 1? BTW congrats to the 0.10.0 release! Co-authored-by: philipp <[email protected]>
2 parents 4d317f7 + 077040f commit f6226e7

File tree

3 files changed

+26
-33
lines changed

3 files changed

+26
-33
lines changed

src/adaptors/mod.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,6 @@ pub struct TupleCombinations<I, T>
682682
{
683683
iter: T::Combination,
684684
_mi: PhantomData<I>,
685-
_mt: PhantomData<T>
686685
}
687686

688687
pub trait HasCombination<I>: Sized {
@@ -698,7 +697,6 @@ pub fn tuple_combinations<T, I>(iter: I) -> TupleCombinations<I, T>
698697
TupleCombinations {
699698
iter: T::Combination::from(iter),
700699
_mi: PhantomData,
701-
_mt: PhantomData,
702700
}
703701
}
704702

@@ -737,7 +735,7 @@ impl<I: Iterator> HasCombination<I> for (I::Item,) {
737735
}
738736

739737
macro_rules! impl_tuple_combination {
740-
($C:ident $P:ident ; $A:ident, $($I:ident),* ; $($X:ident)*) => (
738+
($C:ident $P:ident ; $($X:ident)*) => (
741739
#[derive(Clone, Debug)]
742740
pub struct $C<I: Iterator> {
743741
item: Option<I::Item>,
@@ -747,30 +745,25 @@ macro_rules! impl_tuple_combination {
747745

748746
impl<I: Iterator + Clone> From<I> for $C<I> {
749747
fn from(mut iter: I) -> Self {
750-
$C {
748+
Self {
751749
item: iter.next(),
752750
iter: iter.clone(),
753-
c: $P::from(iter),
751+
c: iter.into(),
754752
}
755753
}
756754
}
757755

758756
impl<I: Iterator + Clone> From<I> for $C<Fuse<I>> {
759757
fn from(iter: I) -> Self {
760-
let mut iter = iter.fuse();
761-
$C {
762-
item: iter.next(),
763-
iter: iter.clone(),
764-
c: $P::from(iter),
765-
}
758+
Self::from(iter.fuse())
766759
}
767760
}
768761

769-
impl<I, $A> Iterator for $C<I>
770-
where I: Iterator<Item = $A> + Clone,
762+
impl<I, A> Iterator for $C<I>
763+
where I: Iterator<Item = A> + Clone,
771764
I::Item: Clone
772765
{
773-
type Item = ($($I),*);
766+
type Item = (A, $(ignore_ident!($X, A)),*);
774767

775768
fn next(&mut self) -> Option<Self::Item> {
776769
if let Some(($($X),*,)) = self.c.next() {
@@ -779,15 +772,15 @@ macro_rules! impl_tuple_combination {
779772
} else {
780773
self.item = self.iter.next();
781774
self.item.clone().and_then(|z| {
782-
self.c = $P::from(self.iter.clone());
775+
self.c = self.iter.clone().into();
783776
self.c.next().map(|($($X),*,)| (z, $($X),*))
784777
})
785778
}
786779
}
787780
}
788781

789-
impl<I, $A> HasCombination<I> for ($($I),*)
790-
where I: Iterator<Item = $A> + Clone,
782+
impl<I, A> HasCombination<I> for (A, $(ignore_ident!($X, A)),*)
783+
where I: Iterator<Item = A> + Clone,
791784
I::Item: Clone
792785
{
793786
type Combination = $C<Fuse<I>>;
@@ -800,25 +793,24 @@ macro_rules! impl_tuple_combination {
800793
// use itertools::Itertools;
801794
//
802795
// for i in 2..=12 {
803-
// println!("impl_tuple_combination!(Tuple{arity}Combination Tuple{prev}Combination; {tys}; {idents});",
796+
// println!("impl_tuple_combination!(Tuple{arity}Combination Tuple{prev}Combination; {idents});",
804797
// arity = i,
805798
// prev = i - 1,
806-
// tys = iter::repeat("A").take(i + 1).join(", "),
807799
// idents = ('a'..'z').take(i - 1).join(" "),
808800
// );
809801
// }
810802
// It could probably be replaced by a bit more macro cleverness.
811-
impl_tuple_combination!(Tuple2Combination Tuple1Combination; A, A, A; a);
812-
impl_tuple_combination!(Tuple3Combination Tuple2Combination; A, A, A, A; a b);
813-
impl_tuple_combination!(Tuple4Combination Tuple3Combination; A, A, A, A, A; a b c);
814-
impl_tuple_combination!(Tuple5Combination Tuple4Combination; A, A, A, A, A, A; a b c d);
815-
impl_tuple_combination!(Tuple6Combination Tuple5Combination; A, A, A, A, A, A, A; a b c d e);
816-
impl_tuple_combination!(Tuple7Combination Tuple6Combination; A, A, A, A, A, A, A, A; a b c d e f);
817-
impl_tuple_combination!(Tuple8Combination Tuple7Combination; A, A, A, A, A, A, A, A, A; a b c d e f g);
818-
impl_tuple_combination!(Tuple9Combination Tuple8Combination; A, A, A, A, A, A, A, A, A, A; a b c d e f g h);
819-
impl_tuple_combination!(Tuple10Combination Tuple9Combination; A, A, A, A, A, A, A, A, A, A, A; a b c d e f g h i);
820-
impl_tuple_combination!(Tuple11Combination Tuple10Combination; A, A, A, A, A, A, A, A, A, A, A, A; a b c d e f g h i j);
821-
impl_tuple_combination!(Tuple12Combination Tuple11Combination; A, A, A, A, A, A, A, A, A, A, A, A, A; a b c d e f g h i j k);
803+
impl_tuple_combination!(Tuple2Combination Tuple1Combination; a);
804+
impl_tuple_combination!(Tuple3Combination Tuple2Combination; a b);
805+
impl_tuple_combination!(Tuple4Combination Tuple3Combination; a b c);
806+
impl_tuple_combination!(Tuple5Combination Tuple4Combination; a b c d);
807+
impl_tuple_combination!(Tuple6Combination Tuple5Combination; a b c d e);
808+
impl_tuple_combination!(Tuple7Combination Tuple6Combination; a b c d e f);
809+
impl_tuple_combination!(Tuple8Combination Tuple7Combination; a b c d e f g);
810+
impl_tuple_combination!(Tuple9Combination Tuple8Combination; a b c d e f g h);
811+
impl_tuple_combination!(Tuple10Combination Tuple9Combination; a b c d e f g h i);
812+
impl_tuple_combination!(Tuple11Combination Tuple10Combination; a b c d e f g h i j);
813+
impl_tuple_combination!(Tuple12Combination Tuple11Combination; a b c d e f g h i j k);
822814

823815
/// An iterator adapter to filter values within a nested `Result::Ok`.
824816
///

src/impl_macros.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ macro_rules! clone_fields {
2222
}
2323
}
2424
}
25+
26+
macro_rules! ignore_ident{
27+
($id:ident, $($t:tt)*) => {$($t)*};
28+
}

src/tuple_impl.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,6 @@ macro_rules! count_ident{
248248
() => {0};
249249
($i0:ident, $($i:ident,)*) => {1 + count_ident!($($i,)*)};
250250
}
251-
macro_rules! ignore_ident{
252-
($id:ident, $($t:tt)*) => {$($t)*};
253-
}
254251
macro_rules! rev_for_each_ident{
255252
($m:ident, ) => {};
256253
($m:ident, $i0:ident, $($i:ident,)*) => {

0 commit comments

Comments
 (0)