diff --git a/src/lib.rs b/src/lib.rs index 1f39379e2..5d49b7fc6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -249,13 +249,19 @@ macro_rules! iproduct { (@flatten $I:expr, $J:expr, $($K:expr,)*) => ( $crate::iproduct!(@flatten $crate::cons_tuples($crate::iproduct!($I, $J)), $($K,)*) ); - ($I:expr) => ( - $crate::__std_iter::IntoIterator::into_iter($I) + () => ( + $crate::__std_iter::once(()) ); - ($I:expr, $J:expr) => ( - $crate::Itertools::cartesian_product($crate::iproduct!($I), $crate::iproduct!($J)) + ($I:expr $(,)?) => ( + $crate::__std_iter::IntoIterator::into_iter($I).map(|elt| (elt,)) ); - ($I:expr, $J:expr, $($K:expr),+) => ( + ($I:expr, $J:expr $(,)?) => ( + $crate::Itertools::cartesian_product( + $crate::__std_iter::IntoIterator::into_iter($I), + $crate::__std_iter::IntoIterator::into_iter($J), + ) + ); + ($I:expr, $J:expr, $($K:expr),+ $(,)?) => ( $crate::iproduct!(@flatten $crate::iproduct!($I, $J), $($K,)+) ); } diff --git a/tests/macros_hygiene.rs b/tests/macros_hygiene.rs index d1111245d..20b59fba8 100644 --- a/tests/macros_hygiene.rs +++ b/tests/macros_hygiene.rs @@ -1,5 +1,6 @@ #[test] fn iproduct_hygiene() { + let _ = itertools::iproduct!(); let _ = itertools::iproduct!(0..6); let _ = itertools::iproduct!(0..6, 0..9); let _ = itertools::iproduct!(0..6, 0..9, 0..12); diff --git a/tests/test_core.rs b/tests/test_core.rs index 45a8df438..db77a1e3e 100644 --- a/tests/test_core.rs +++ b/tests/test_core.rs @@ -18,6 +18,23 @@ use crate::it::Itertools; use core::iter; use itertools as it; +#[test] +fn product0() { + let mut prod = iproduct!(); + assert_eq!(prod.next(), Some(())); + assert!(prod.next().is_none()); +} + +#[test] +fn iproduct1() { + let s = "αβ"; + + let mut prod = iproduct!(s.chars()); + assert_eq!(prod.next(), Some(('α',))); + assert_eq!(prod.next(), Some(('β',))); + assert!(prod.next().is_none()); +} + #[test] fn product2() { let s = "αβ";