Skip to content

Commit

Permalink
Macro hygiene of chain!
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
Philippe-Cholet committed May 21, 2024
1 parent 4777762 commit aefe977
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,16 +393,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
}
Expand Down
13 changes: 13 additions & 0 deletions tests/macros_hygiene.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
mod alloc {}
mod core {}
mod either {}
mod std {}

#[test]
fn iproduct_hygiene() {
let _ = itertools::iproduct!();
Expand All @@ -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);
}

0 comments on commit aefe977

Please sign in to comment.