Trait SplitStreamByMapExt

Source
pub trait SplitStreamByMapExt<P, L, R>: Stream {
    // Provided method
    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 { ... }
}
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),
});

Implementors§

Source§

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