|
| 1 | +use crate::stream::{IntoStream, Merge}; |
| 2 | +use futures_core::Stream; |
| 3 | + |
| 4 | +use super::{chain::tuple::Chain2, merge::tuple::Merge2, zip::tuple::Zip2, Chain, Zip}; |
| 5 | + |
| 6 | +/// An extension trait for the `Stream` trait. |
| 7 | +pub trait StreamExt: Stream { |
| 8 | + /// Combines two streams into a single stream of all their outputs. |
| 9 | + fn merge<T, S2>(self, other: S2) -> Merge2<T, Self, S2::IntoStream> |
| 10 | + where |
| 11 | + Self: Stream<Item = T> + Sized, |
| 12 | + S2: IntoStream<Item = T>; |
| 13 | + |
| 14 | + /// Takes two streams and creates a new stream over all in sequence |
| 15 | + fn chain<T, S2>(self, other: S2) -> Chain2<Self, S2::IntoStream> |
| 16 | + where |
| 17 | + Self: Stream<Item = T> + Sized, |
| 18 | + S2: IntoStream<Item = T>; |
| 19 | + |
| 20 | + /// ‘Zips up’ multiple streams into a single stream of pairs. |
| 21 | + fn zip<T, S2>(self, other: S2) -> Zip2<Self, S2::IntoStream> |
| 22 | + where |
| 23 | + Self: Stream<Item = T> + Sized, |
| 24 | + S2: IntoStream<Item = T>; |
| 25 | +} |
| 26 | + |
| 27 | +impl<S1> StreamExt for S1 |
| 28 | +where |
| 29 | + S1: Stream, |
| 30 | +{ |
| 31 | + fn merge<T, S2>(self, other: S2) -> Merge2<T, S1, S2::IntoStream> |
| 32 | + where |
| 33 | + S1: Stream<Item = T>, |
| 34 | + S2: IntoStream<Item = T>, |
| 35 | + { |
| 36 | + Merge::merge((self, other)) |
| 37 | + } |
| 38 | + |
| 39 | + fn chain<T, S2>(self, other: S2) -> Chain2<Self, S2::IntoStream> |
| 40 | + where |
| 41 | + Self: Stream<Item = T> + Sized, |
| 42 | + S2: IntoStream<Item = T>, |
| 43 | + { |
| 44 | + // TODO(yosh): fix the bounds on the tuple impl |
| 45 | + Chain::chain((self, other.into_stream())) |
| 46 | + } |
| 47 | + |
| 48 | + fn zip<T, S2>(self, other: S2) -> Zip2<Self, S2::IntoStream> |
| 49 | + where |
| 50 | + Self: Stream<Item = T> + Sized, |
| 51 | + S2: IntoStream<Item = T>, |
| 52 | + { |
| 53 | + // TODO(yosh): fix the bounds on the tuple impl |
| 54 | + Zip::zip((self, other.into_stream())) |
| 55 | + } |
| 56 | +} |
0 commit comments