#find #stream #stream-ext #stream-find

stream-find

Add a find and find_map methods to any stream

4 releases (2 breaking)

Uses new Rust 2024

0.3.0 Mar 23, 2025
0.2.0 Mar 23, 2025
0.1.1 Mar 22, 2025
0.1.0 Mar 22, 2025

#371 in Asynchronous

Download history 293/week @ 2025-03-19 18/week @ 2025-03-26

311 downloads per month

MIT license

15KB
175 lines

stream-find

A crate that provide trat StreamFind which add method find to any futures::stream::Stream object.

How to use

  • use stream_find::StreamFind;
  • stream_obj.find(async |item| {// return true if match, otherwise return false})

Example

use stream_find::StreamFind;
use futures::stream::{iter, StreamExt};

const START: usize = 0;
const END: usize = 100;
const TARGET: usize = 0;
let mut stream = iter(START..END);
let result = stream.find(async |item| {
    *item == TARGET
}).await;
assert_eq!(result.unwrap(), TARGET, "Expect to found something.");
assert_eq!(stream.next().await.expect("to yield next value"), TARGET + 1, "Expect stream to be resumable and it immediately stop after it found first match.");

Breaking change

0.2.0

Switch from manually implement Future trait on Find struct to relying on futures::stream::StreamExt::next method to yield a value and pass the yielded value as ref to predicate function. This greatly improve performance. It is showed in several tests that this approach is at least 7 times faster than the original.

Dependencies

~0.6–0.8MB
~15K SLoC