iter-n 0.1.0

A utility for functions returning impl Iterator to return one of several distinct types.
Documentation
  • Coverage
  • 0.39%
    1 out of 259 items documented1 out of 1 items with examples
  • Size
  • Source code size: 312.83 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 55.26 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • frozenlib/iter-n
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • frozenlib

iter-n

Crates.io Docs.rs Actions Status

A utility for functions returning impl Iterator to return one of several distinct types.

Motivation

In functions that return impl Iterator, it is necessary to return an iterator of a specific type. Therefore, it is not possible to return an iterator of a different type, as shown below.

fn f(x: i32) -> impl Iterator<Item = i32> {
    if x % 2 == 0 {
        [0, 1].iter().map(|y| y + 1)
    } else {
        [0, 1].iter().map(|y| y + 2) // ERROR: `if` and `else` have incompatible types
    }
}

By using iter_n, you can return an iterator of a different type from a function.

Example

use iter_n::iter2::* must be placed in function scope, not in module scope.

Since iter_n::iter2, iter_n::iter3, etc. define methods of the same name, if multiple use iter_n::iter{N}::*; are placed in the module scope, there will be a conflict with the methods.

fn f(x: i32) -> impl Iterator<Item = i32> {
    use iter_n::iter2::*;
    if x % 2 == 0 {
        [0, 1].iter().map(|y| y + 1).into_iter0()
    } else {
        [0, 1].iter().map(|y| y + 2).into_iter1()
    }
}
fn g(x: i32) -> impl Iterator<Item = i32> {
    use iter_n::iter3::*;
    if x % 3 == 0 {
        [0, 1].iter().map(|y| y + 1).into_iter0()
    } else if x % 3 == 0 {
        [0, 1].iter().map(|y| y + 2).into_iter1()
    } else {
        [0, 1].iter().map(|y| y + 3).into_iter2()
    }
}

License

This project is dual licensed under Apache-2.0/MIT. See the two LICENSE-* files for details.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.