Crate anon_iter

Crate anon_iter 

Source
Expand description

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:

[dependencies]
anon_iter = "0.1"

Wrap each iterator in AnonIter2 to return 2 different iterators from the same function:

use anon_iter::AnonIter2;

fn foo(x: i32) -> impl Iterator<Item = i32> {
    match x {
        0 => AnonIter2::I1(1..10),
        _ => AnonIter2::I2(vec![5, 10].into_iter()),
    }
}

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.

Enums§

AnonIter2
Wraps 2 impl Iterators which may be of different types
AnonIter3
Wraps 3 impl Iterators which may be of different types
AnonIter4
Wraps 4 impl Iterators which may be of different types
AnonIter5
Wraps 5 impl Iterators which may be of different types
AnonIter6
Wraps 6 impl Iterators which may be of different types
AnonIter7
Wraps 7 impl Iterators which may be of different types
AnonIter8
Wraps 8 impl Iterators which may be of different types
AnonIter9
Wraps 9 impl Iterators which may be of different types
AnonIter10
Wraps 10 impl Iterators which may be of different types
AnonIter11
Wraps 11 impl Iterators which may be of different types
AnonIter12
Wraps 12 impl Iterators which may be of different types