stream-find

Crates.iostream-find
lib.rsstream-find
version0.3.0
created_at2025-03-22 03:33:37.82083+00
updated_at2025-03-23 23:13:02.936622+00
descriptionAdd a `find` and `find_map` methods to any stream.
homepage
repositoryhttps://github.com/NattapongSiri/stream-find-rs
max_upload_size
id1601512
size25,804
(NattapongSiri)

documentation

README

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.

Commit count: 8

cargo fmt