SplitStreamByMapExt

Trait SplitStreamByMapExt 

Source
pub trait SplitStreamByMapExt<P, L, R>: Stream {
    // Provided methods
    fn split_by_map(
        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 { ... }
    fn split_by_map_buffered<const N: usize>(
        self,
        predicate: P,
    ) -> (LeftSplitByMapBuffered<Self::Item, L, R, Self, P, N>, RightSplitByMapBuffered<Self::Item, L, R, Self, P, N>)
       where P: Fn(Self::Item) -> Either<L, R>,
             Self: Sized { ... }
}
Expand description

This extension trait provides the functionality for splitting a stream by a predicate of type Fn(Self::Item) -> Either<L,R>. The resulting streams will yield types L and R respectively

Provided Methods§

Source

fn split_by_map( 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,

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

fn split_by_map_buffered<const N: usize>( self, predicate: P, ) -> (LeftSplitByMapBuffered<Self::Item, L, R, Self, P, N>, RightSplitByMapBuffered<Self::Item, L, R, Self, P, N>)
where P: Fn(Self::Item) -> Either<L, R>, Self: Sized,

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. This will buffer up to N items of the inactive stream before returning Pending and notifying that stream

use split_stream_by::{Either,SplitStreamByMapExt};
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_buffered::<3>(|item| match item {
	Message::Request(req) => Either::Left(req),
	Message::Response(res) => Either::Right(res),
});

Implementors§

Source§

impl<T, P, L, R> SplitStreamByMapExt<P, L, R> for T
where T: Stream + ?Sized,