Repository: rust-itertools/itertools
Pull request #943 (2024-05-21 09:33:41)

Macro hygiene: `chain!` and qualified paths
#942 lead me to check our macro hygiene.

1. If someone has a module named `core` and uses `itertools::chain!` then there is a conflict!
  I improve our macro hygiene test by adding (empty) modules with all the names we might be tempted to use.
  Without changing `core` in `chain!`, the test failed!
2. Qualified paths only? Is conflict with `.map` and `.zip` absolutely impossible? Seems more bulletproof to me that way.
  EDIT: Found https://github.com/rust-itertools/itertools/pull/525#discussion_r584338246 which seems to indicate I'm right to use qualified paths.

**PS:** This is my 💯<sup>th</sup> merged PR. 🎉

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/rust-itertools/itertools/pull/943?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 94.35%. Comparing base [(`6814180`)](https://app.codecov.io/gh/rust-itertools/itertools/commit/68141809aabec2816d3250820c8d6608f3b2293f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) to head [(`4588c02`)](https://app.codecov.io/gh/rust-itertools/itertools/pull/943?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).
> Report is 94 commits behind head on master.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #943      +/-   ##
==========================================
- Coverage   94.38%   94.35%   -0.03%     
==========================================
  Files          48       49       +1     
  Lines        6665     7002     +337     
==========================================
+ Hits         6291     6607     +316     
- Misses        374      395      +21     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/rust-itertools/itertools/pull/943?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).


Patch:
diff --git a/src/lib.rs b/src/lib.rs
index f4de79c..018f877 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -262,7 +262,10 @@ macro_rules! iproduct {
         $crate::__std_iter::once(())
     );
     ($I:expr $(,)?) => (
-        $crate::__std_iter::IntoIterator::into_iter($I).map(|elt| (elt,))
+        $crate::__std_iter::Iterator::map(
+            $crate::__std_iter::IntoIterator::into_iter($I),
+            |elt| (elt,)
+        )
     );
     ($I:expr, $J:expr $(,)?) => (
         $crate::Itertools::cartesian_product(
@@ -330,19 +333,24 @@ macro_rules! izip {
 
     // binary
     ($first:expr, $second:expr $(,)*) => {
-        $crate::izip!($first)
-            .zip($second)
+        $crate::__std_iter::Iterator::zip(
+            $crate::__std_iter::IntoIterator::into_iter($first),
+            $second,
+        )
     };
 
     // n-ary where n > 2
     ( $first:expr $( , $rest:expr )* $(,)* ) => {
-        $crate::izip!($first)
+        {
+            let iter = $crate::__std_iter::IntoIterator::into_iter($first);
             $(
-                .zip($rest)
+                let iter = $crate::__std_iter::Iterator::zip(iter, $rest);
             )*
-            .map(
+            $crate::__std_iter::Iterator::map(
+                iter,
                 $crate::izip!(@closure a => (a) $( , $rest )*)
             )
+        }
     };
 }
 
@@ -393,16 +401,16 @@ macro_rules! izip {
 /// ```
 macro_rules! chain {
     () => {
-        core::iter::empty()
+        $crate::__std_iter::empty()
     };
     ($first:expr $(, $rest:expr )* $(,)?) => {
         {
-            let iter = core::iter::IntoIterator::into_iter($first);
+            let iter = $crate::__std_iter::IntoIterator::into_iter($first);
             $(
                 let iter =
-                    core::iter::Iterator::chain(
+                    $crate::__std_iter::Iterator::chain(
                         iter,
-                        core::iter::IntoIterator::into_iter($rest));
+                        $crate::__std_iter::IntoIterator::into_iter($rest));
             )*
             iter
         }


Test patch:
diff --git a/tests/macros_hygiene.rs b/tests/macros_hygiene.rs
index 20b59fb..e6e8955 100644
--- a/tests/macros_hygiene.rs
+++ b/tests/macros_hygiene.rs
@@ -1,3 +1,8 @@
+mod alloc {}
+mod core {}
+mod either {}
+mod std {}
+
 #[test]
 fn iproduct_hygiene() {
     let _ = itertools::iproduct!();
@@ -12,3 +17,11 @@ fn izip_hygiene() {
     let _ = itertools::izip!(0..6, 0..9);
     let _ = itertools::izip!(0..6, 0..9, 0..12);
 }
+
+#[test]
+fn chain_hygiene() {
+    let _: ::std::iter::Empty<i32> = itertools::chain!();
+    let _ = itertools::chain!(0..6);
+    let _ = itertools::chain!(0..6, 0..9);
+    let _ = itertools::chain!(0..6, 0..9, 0..12);
+}