Crate split_stream_by[−][src]
Expand description
This repo is for a rust crate that offers a futures::Stream extension
trait which allows for splitting a Stream into two streams using a
predicate function thats checked on each Stream::Item.
The current version of this crate buffers only one value and only in the scenario where the item yielded from the parent stream is not what the child stream requested per the predicate. In that scenario, the item is stored and the other stream is awakened
use split_stream_by::SplitStreamByExt;
let incoming_stream = futures::stream::iter([0,1,2,3,4,5]);
let (mut even_stream, mut odd_stream) = incoming_stream.split_by(|&n| n % 2 == 0);
tokio::spawn(async move {
while let Some(even_number) = even_stream.next().await {
println!("Even {}",even_number);
}
});
while let Some(odd_number) = odd_stream.next().await {
println!("Odd {}",odd_number);
}A more advanced usage uses split_by_map which allows for extracting
values while splitting
use split_stream_by::{Either,SplitStreamByExt};
struct Request {
//...
}
struct Response {
//...
}
enum Message {
Request(Request),
Response(Response)
}
let incoming_stream = futures::stream::iter([
Message::Request(Request {}),
Message::Response(Response {}),
Message::Response(Response {}),
]);
let (mut request_stream, mut response_stream) = incoming_stream.split_by_map(|item| match item {
Message::Request(req) => Either::Left(req),
Message::Response(res) => Either::Right(res),
});
tokio::spawn(async move {
while let Some(request) = request_stream.next().await {
// ...
}
});
while let Some(response) = response_stream.next().await {
// ...
}Structs
A struct that implements Stream which returns the items where
the predicate returns false
A struct that implements Stream which returns the inner values
where the predicate returns Either::Left(..) when using split_by_map
A struct that implements Stream which returns the inner values
where the predicate returns Either::Right(..) when using split_by_map
A struct that implements Stream which returns the items where
the predicate returns true
Enums
Combines two different futures, streams, or sinks having the same associated types into a single type.
Traits
This is the extension crate would provides the functionality for splitting a stream