Trait split_stream_by::SplitStreamByExt[][src]

pub trait SplitStreamByExt: Stream {
    fn split_by<P>(
        self,
        predicate: P
    ) -> (TrueSplitBy<Self::Item, Self, P>, FalseSplitBy<Self::Item, Self, P>)
    where
        P: Fn(&Self::Item) -> bool,
        Self: Sized
, { ... }
fn split_by_map<L, R, P>(
        self,
        predicate: P
    ) -> (LeftSplitByMap<Self::Item, L, R, Self, P>, RightSplitByMap<Self::Item, L, R, Self, P>)
    where
        P: Fn(Self::Item) -> Either<L, R>,
        Self: Sized
, { ... } }
Expand description

This is the extension crate would provides the functionality for splitting a stream

Provided methods

This takes ownership of a stream and returns two streams based on a predicate. When the predicate returns true, the item will appear in the first of the pair of streams returned. Items that return false will go into the second of the pair of streams

use split_stream_by::SplitStreamByExt;

let incoming_stream = futures::stream::iter([0,1,2,3,4,5]);
let (even_stream, odd_stream) = incoming_stream.split_by(|&n| n % 2 == 0);

This takes ownership of a stream and returns two streams based on a predicate. The predicate takes an item by value and returns Either::Left(..) or Either::Right(..) where the inner values of Left and Right become the items of the two respective streams

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),
});

Implementors