6 releases

Uses old Rust 2015

0.1.5 Dec 13, 2019
0.1.4 May 4, 2018
0.1.3 Oct 25, 2017
0.1.2 Dec 11, 2016
Download history 303/week @ 2021-04-04 616/week @ 2021-04-11 641/week @ 2021-04-18 458/week @ 2021-04-25 653/week @ 2021-05-02 349/week @ 2021-05-09 357/week @ 2021-05-16 183/week @ 2021-05-23 402/week @ 2021-05-30 719/week @ 2021-06-06 709/week @ 2021-06-13 683/week @ 2021-06-20 926/week @ 2021-06-27 953/week @ 2021-07-04 533/week @ 2021-07-11 1284/week @ 2021-07-18

1,124 downloads per month
Used in 13 crates (11 directly)

MIT/Apache

54KB
1.5K 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

  • std