Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,72 @@ macro_rules! izip {
};
}

#[macro_export]
/// [Chain][`chain`] zero or more iterators together into one sequence.
///
/// The comma-separated arguments must implement [`IntoIterator`].
/// The final argument may be followed by a trailing comma.
///
/// [`chain`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.chain
/// [`IntoIterator`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html
///
/// # Examples
///
/// [`iter::empty`]: https://doc.rust-lang.org/std/iter/fn.empty.html
///
/// Empty invocations of `chain!` expand to an invocation of [`iter::empty`]:
/// ```
/// # use std::iter;
/// use itertools::chain;
///
/// let _: iter::Empty<()> = chain!();
/// let _: iter::Empty<i8> = chain!();
/// ```
///
/// Invocations of `chain!` with one argument expand to [`arg.into_iter()`][`IntoIterator`]:
/// ```
/// use std::{ops::Range, slice};
/// use itertools::chain;
/// let _: <Range<_> as IntoIterator>::IntoIter = chain!((2..6),); // trailing comma optional!
/// let _: <&[_] as IntoIterator>::IntoIter = chain!(&[2, 3, 4]);
/// ```
///
/// Invocations of `chain!` with multiple arguments [`.into_iter()`][`IntoIterator`] each
/// argument, and then [`chain`] them together:
/// ```
/// use std::{iter::*, ops::Range, slice};
/// use itertools::{assert_equal, chain};
///
/// // e.g., this:
/// let with_macro: Chain<Chain<Once<_>, Take<Repeat<_>>>, slice::Iter<_>> =
/// chain![once(&0), repeat(&1).take(2), &[2, 3, 5],];
///
/// // ...is equivalant to this:
/// let with_method: Chain<Chain<Once<_>, Take<Repeat<_>>>, slice::Iter<_>> =
/// once(&0)
/// .chain(repeat(&1).take(2))
/// .chain(&[2, 3, 5]);
///
/// assert_equal(with_macro, with_method);
/// ```
macro_rules! chain {
() => {
core::iter::empty()
};
($first:expr $(, $rest:expr )* $(,)?) => {
{
let iter = core::iter::IntoIterator::into_iter($first);
$(
let iter =
core::iter::Iterator::chain(
iter,
core::iter::IntoIterator::into_iter($rest));
)*
iter
}
};
}

/// An [`Iterator`] blanket implementation that provides extra adaptors and
/// methods.
///
Expand Down
23 changes: 23 additions & 0 deletions tests/test_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::it::multizip;
use crate::it::free::put_back;
use crate::it::iproduct;
use crate::it::izip;
use crate::it::chain;

#[test]
fn product2() {
Expand Down Expand Up @@ -87,6 +88,28 @@ fn multizip3() {
}
}

#[test]
fn chain_macro() {
let mut chain = chain!(2..3);
assert!(chain.next() == Some(2));
assert!(chain.next().is_none());

let mut chain = chain!(0..2, 2..3, 3..5i8);
for i in 0..5i8 {
assert_eq!(Some(i), chain.next());
}
assert!(chain.next().is_none());

let mut chain = chain!();
assert_eq!(chain.next(), Option::<()>::None);
}

#[test]
fn chain2() {
let _ = chain!(1.., 2..);
let _ = chain!(1.., 2.., );
}

#[test]
fn write_to() {
let xs = [7, 9, 8];
Expand Down