7 releases

0.2.2 Dec 7, 2023
0.2.1 Nov 29, 2023
0.1.3 Jan 22, 2022

#251 in Rust patterns

Download history 2710/week @ 2024-01-04 3247/week @ 2024-01-11 4249/week @ 2024-01-18 2554/week @ 2024-01-25 2901/week @ 2024-02-01 3101/week @ 2024-02-08 3140/week @ 2024-02-15 4019/week @ 2024-02-22 3169/week @ 2024-02-29 2796/week @ 2024-03-07 3132/week @ 2024-03-14 2362/week @ 2024-03-21 2519/week @ 2024-03-28 3484/week @ 2024-04-04 1875/week @ 2024-04-11 2818/week @ 2024-04-18

10,954 downloads per month
Used in risinglight

MIT/Apache

12KB
186 lines

iter-chunks

Yet another crate to provides chunks method to rust Iterator.

Please read the API documentation here.

Usage

Add this to your Cargo.toml:

[dependencies]
iter-chunks = "0.2"

Examples

Currently, only while loop over Chunks is supported.

use iter_chunks::IterChunks;

let arr = [1, 1, 2, 2, 3];
let expected = [vec![1, 1], vec![2, 2], vec![3]];
let mut chunks = arr.into_iter().chunks(2);
let mut i = 0;
while let Some(chunk) = chunks.next() {
    assert_eq!(chunk.collect::<Vec<_>>(), expected[i]);
    i += 1;
}

Why create this crate?

itertools provides many awesome extensions, including chunks. It's really useful, but it use RefCell internally, causing it's not Send.

It's a very common usecase in async context, which requires Chunks to be Send:

async fn do_some_work(input: impl Iterator<Item = i32>) {
    for chunk in input.chunks(1024) {
        for v in chunk {
            handle(v).await
        }
        do_some_flush().await
    }
}

This crate implements chunks without RefCell, so Chunks is both Send and Sync. As a price, Chunks cannot implement Iterator (which can be resolved later by GAT and LendingIterator).

Future works

The lack of the Iterator implementation is very difficult to use, and the best solution is to wait for GAT and a suitable LendingIterator crate. But in the short term, we can consider providing some common methods such as nth, for_each, try_for_each, and so on.

Contributions are welcome.

License

Licensed under either of

at your option.

No runtime deps