anon_iter
anon_iter is a much lighter alternative to the auto_enums crate,
being less general-purpose but solving the most common use-case of this pattern (impl Iterator),
without relying on macros - leading to much faster compile times and simpler syntax.
It does this by providing generic wrapper types like [AnonIter2]
to allow to return different types of iterators
from a function that returns -> impl Iterator.
Usage
Add this to your Cargo.toml:
[]
= "0.1"
Wrap each iterator in [AnonIter2] to return 2 different iterators from the same function:
use AnonIter2;
The crate Either allows similar functionality, as it too implements Iterator when
its type parameters are both Iterator.
But this breaks down when you want to return 3 or more Iterators because you now have to
do extra nesting (e.g. Either::Left(Either::Right(Either::Left(my_iter)))). With anon_iter, it's just AnonIter8::I3(my_iter).
Additionally, anon_iter makes code more readable because it may not be instantly obvious that we are using Either for this purpose, but with AnonEnum
the intent is apparent.
An even simpler approach
If you just want to do this once without depending on this crate, copy-paste this into your project:
/// Wraps 2 `impl Iterator` which may be of different types
///
/// Functions returning `-> impl Iterator` must have the same return type
/// from all branches, but this is overly restrictive.
///
/// We may want to return 2 or more different iterators from the same function,
/// and this type allows that by wrapping each unique iterator in a variant of
/// this enum.