#iterator #sequence #no-std #no-alloc

no-std iter_seq

Stateless, transformable, abstract sequence of values

3 releases

0.1.2 Feb 22, 2025
0.1.1 Feb 22, 2025
0.1.0 Feb 22, 2025

#1651 in Data structures

Download history 316/week @ 2025-02-19 68/week @ 2025-02-26

384 downloads per month

MIT/Apache

15KB
266 lines

iter_seq

Stateless, transformable, abstract sequence of values.

This crate provides a mechanism for working with abstract stateless sequences of arbitrary values. In contrast, standard iterators are stateful—that is, their state can be changed by calling next.

One significant limitation of the stateful model is its inability to encode compile-time invariants, which can lead to unnecessary overhead that the compiler often cannot reliably optimize away. This crate provides a "wrapper" around standard iterators that must be irreversibly converted into an iterator before its elements can be consumed.

Example

use iter_seq::{const_repeat, Sequence};

fn main() {
    let odd_squares = const_repeat()
        .enumerate()
        .map(|(i, _)| i as u32)
        .map(|n| (n + 1) * (n + 1));

    let arr: [u32; 128] = odd_squares.const_take_exact::<128>()
        .collect_array();

    for (i, n) in arr.iter().enumerate() {
        assert_eq!((i as u32 + 1) * (i as u32 + 1), *n);
    }
}

No runtime deps