Skip to content

Commit 56f8708

Browse files
authored
Merge pull request #128 from yoshuawuyts/ext-traits
Add extension traits
2 parents 7d6bdc1 + 09b6dfe commit 56f8708

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

src/future/futures_ext.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::future::Join;
2+
use crate::future::Race;
3+
use futures_core::Future;
4+
use std::future::IntoFuture;
5+
6+
use super::join::tuple::Join2;
7+
use super::race::tuple::Race2;
8+
9+
/// An extension trait for the `Future` trait.
10+
pub trait FutureExt: Future {
11+
/// Wait for both futures to complete.
12+
fn join<S2>(self, other: S2) -> Join2<Self, S2::IntoFuture>
13+
where
14+
Self: Future + Sized,
15+
S2: IntoFuture;
16+
17+
/// Wait for the first future to complete.
18+
fn race<T, S2>(self, other: S2) -> Race2<T, Self, S2::IntoFuture>
19+
where
20+
Self: Future<Output = T> + Sized,
21+
S2: IntoFuture<Output = T>;
22+
}
23+
24+
impl<F1> FutureExt for F1
25+
where
26+
F1: Future,
27+
{
28+
fn join<F2>(self, other: F2) -> Join2<Self, F2::IntoFuture>
29+
where
30+
Self: Future + Sized,
31+
F2: IntoFuture,
32+
{
33+
Join::join((self, other))
34+
}
35+
36+
fn race<T, S2>(self, other: S2) -> Race2<T, Self, S2::IntoFuture>
37+
where
38+
Self: Future<Output = T> + Sized,
39+
S2: IntoFuture<Output = T>,
40+
{
41+
Race::race((self, other))
42+
}
43+
}

src/future/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@
6868
//! - `future::RaceOk`: wait for the first _successful_ future in the set to
6969
//! complete, or return an `Err` if *no* futures complete successfully.
7070
//!
71+
pub use futures_ext::FutureExt;
7172
pub use join::Join;
7273
pub use race::Race;
7374
pub use race_ok::RaceOk;
7475
pub use try_join::TryJoin;
7576

77+
mod futures_ext;
7678
pub(crate) mod join;
7779
pub(crate) mod race;
7880
pub(crate) mod race_ok;

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ mod utils;
6666

6767
/// The futures concurrency prelude.
6868
pub mod prelude {
69+
pub use super::future::FutureExt as _;
70+
pub use super::stream::StreamExt as _;
71+
6972
pub use super::future::Join as _;
7073
pub use super::future::Race as _;
7174
pub use super::future::RaceOk as _;

src/stream/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@
5050
pub use chain::Chain;
5151
pub use into_stream::IntoStream;
5252
pub use merge::Merge;
53+
pub use stream_ext::StreamExt;
5354
pub use zip::Zip;
5455

5556
pub(crate) mod chain;
5657
mod into_stream;
5758
pub(crate) mod merge;
59+
mod stream_ext;
5860
pub(crate) mod zip;

src/stream/stream_ext.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)