| Crates.io | stream-find |
| lib.rs | stream-find |
| version | 0.3.0 |
| created_at | 2025-03-22 03:33:37.82083+00 |
| updated_at | 2025-03-23 23:13:02.936622+00 |
| description | Add a `find` and `find_map` methods to any stream. |
| homepage | |
| repository | https://github.com/NattapongSiri/stream-find-rs |
| max_upload_size | |
| id | 1601512 |
| size | 25,804 |
A crate that provide trat StreamFind which add method find to any futures::stream::Stream object.
use stream_find::StreamFind;stream_obj.find(async |item| {// return true if match, otherwise return false})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.");
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.