Skip to content

Commit

Permalink
impl chain,zip on stream ext
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Apr 5, 2023
1 parent 5418488 commit 09b6dfe
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/stream/stream_ext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::stream::{IntoStream, Merge};
use futures_core::Stream;

use super::merge::tuple::Merge2;
use super::{chain::tuple::Chain2, merge::tuple::Merge2, zip::tuple::Zip2, Chain, Zip};

/// An extension trait for the `Stream` trait.
pub trait StreamExt: Stream {
Expand All @@ -10,6 +10,18 @@ pub trait StreamExt: Stream {
where
Self: Stream<Item = T> + Sized,
S2: IntoStream<Item = T>;

/// Takes two streams and creates a new stream over all in sequence
fn chain<T, S2>(self, other: S2) -> Chain2<Self, S2::IntoStream>
where
Self: Stream<Item = T> + Sized,
S2: IntoStream<Item = T>;

/// ‘Zips up’ multiple streams into a single stream of pairs.
fn zip<T, S2>(self, other: S2) -> Zip2<Self, S2::IntoStream>
where
Self: Stream<Item = T> + Sized,
S2: IntoStream<Item = T>;
}

impl<S1> StreamExt for S1
Expand All @@ -23,4 +35,22 @@ where
{
Merge::merge((self, other))
}

fn chain<T, S2>(self, other: S2) -> Chain2<Self, S2::IntoStream>
where
Self: Stream<Item = T> + Sized,
S2: IntoStream<Item = T>,
{
// TODO(yosh): fix the bounds on the tuple impl
Chain::chain((self, other.into_stream()))
}

fn zip<T, S2>(self, other: S2) -> Zip2<Self, S2::IntoStream>
where
Self: Stream<Item = T> + Sized,
S2: IntoStream<Item = T>,
{
// TODO(yosh): fix the bounds on the tuple impl
Zip::zip((self, other.into_stream()))
}
}

0 comments on commit 09b6dfe

Please sign in to comment.