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
.
```rust use splitstreamby::SplitStreamByExt;
let incomingstream = futures::stream::iter([0,1,2,3,4,5]); let (mut evenstream, mut oddstream) = incomingstream.split_by(|&n| n % 2 == 0);
tokio::spawn(async move { while let Some(evennumber) = evenstream.next().await { println!("Even {}",even_number); } });
while let Some(oddnumber) = oddstream.next().await { println!("Odd {}",odd_number); } ```
A more advanced usage uses split_by_map
which allows for extracting
values while splitting
```rust use splitstreamby::{Either,SplitStreamByExt};
struct Request { //... }
struct Response { //... }
enum Message { Request(Request), Response(Response) }
let incomingstream = futures::stream::iter([ Message::Request(Request {}), Message::Response(Response {}), Message::Response(Response {}), ]); let (mut requeststream, mut responsestream) = incomingstream.splitbymap(|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 { // ... } ```
In its current implementation, it only buffers up to one element in the scenario where the stream that requests the next item is not the one that matches the predicate. In the future, this might be changed to be configurable using a const generic parameter