10 releases

0.1.9 Jan 6, 2023
0.1.8 Oct 12, 2022
0.1.7 Sep 8, 2022
0.1.6 Jun 29, 2022
0.1.2 Dec 11, 2016

#27 in Algorithms

Download history 20873/week @ 2023-12-11 21985/week @ 2023-12-18 13936/week @ 2023-12-25 22818/week @ 2024-01-01 25690/week @ 2024-01-08 25123/week @ 2024-01-15 27423/week @ 2024-01-22 27157/week @ 2024-01-29 27160/week @ 2024-02-05 27893/week @ 2024-02-12 27170/week @ 2024-02-19 25600/week @ 2024-02-26 26318/week @ 2024-03-04 25321/week @ 2024-03-11 28119/week @ 2024-03-18 25794/week @ 2024-03-25

106,933 downloads per month
Used in 166 crates (26 directly)

MIT/Apache

97KB
3K SLoC

streaming-iterator

CircleCI

Documentation

Streaming iterators for Rust.

License

Licensed under either of

at your option.

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.


lib.rs:

Streaming iterators.

The iterator APIs in the Rust standard library do not allow elements to be yielded which borrow from the iterator itself. That means, for example, that the std::io::Lines iterator must allocate a new String for each line rather than reusing an internal buffer. The StreamingIterator trait instead provides access to elements being iterated over only by reference rather than by value.

StreamingIterators cannot be used in Rust for loops, but while let loops offer a similar level of ergonomics:

while let Some(item) = iter.next() {
    // work with item
}

However, make sure to only use the above form with a mutable reference to an existing iterator, not with an expression that creates an iterator. For example, the following code will loop forever over the first element of the array:

use streaming_iterator::{convert, StreamingIterator};
let array = [0, 1, 2, 3];

while let Some(item) = convert(array.iter()).next() {
  // This is an infinite loop!
}

While the standard Iterator trait's functionality is based off of the next method, StreamingIterator's functionality is based off of a pair of methods: advance and get. This essentially splits the logic of next in half (in fact, StreamingIterator's next method does nothing but call advance followed by get).

This is required because of Rust's lexical handling of borrows (more specifically a lack of single entry, multiple exit borrows). If StreamingIterator was defined like Iterator with just a required next method, operations like filter would be impossible to define.

No runtime deps

Features