-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from yoshuawuyts/ext-traits
Add extension traits
- Loading branch information
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::future::Join; | ||
use crate::future::Race; | ||
use futures_core::Future; | ||
use std::future::IntoFuture; | ||
|
||
use super::join::tuple::Join2; | ||
use super::race::tuple::Race2; | ||
|
||
/// An extension trait for the `Future` trait. | ||
pub trait FutureExt: Future { | ||
/// Wait for both futures to complete. | ||
fn join<S2>(self, other: S2) -> Join2<Self, S2::IntoFuture> | ||
where | ||
Self: Future + Sized, | ||
S2: IntoFuture; | ||
|
||
/// Wait for the first future to complete. | ||
fn race<T, S2>(self, other: S2) -> Race2<T, Self, S2::IntoFuture> | ||
where | ||
Self: Future<Output = T> + Sized, | ||
S2: IntoFuture<Output = T>; | ||
} | ||
|
||
impl<F1> FutureExt for F1 | ||
where | ||
F1: Future, | ||
{ | ||
fn join<F2>(self, other: F2) -> Join2<Self, F2::IntoFuture> | ||
where | ||
Self: Future + Sized, | ||
F2: IntoFuture, | ||
{ | ||
Join::join((self, other)) | ||
} | ||
|
||
fn race<T, S2>(self, other: S2) -> Race2<T, Self, S2::IntoFuture> | ||
where | ||
Self: Future<Output = T> + Sized, | ||
S2: IntoFuture<Output = T>, | ||
{ | ||
Race::race((self, other)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::stream::{IntoStream, Merge}; | ||
use futures_core::Stream; | ||
|
||
use super::{chain::tuple::Chain2, merge::tuple::Merge2, zip::tuple::Zip2, Chain, Zip}; | ||
|
||
/// An extension trait for the `Stream` trait. | ||
pub trait StreamExt: Stream { | ||
/// Combines two streams into a single stream of all their outputs. | ||
fn merge<T, S2>(self, other: S2) -> Merge2<T, Self, S2::IntoStream> | ||
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 | ||
where | ||
S1: Stream, | ||
{ | ||
fn merge<T, S2>(self, other: S2) -> Merge2<T, S1, S2::IntoStream> | ||
where | ||
S1: Stream<Item = T>, | ||
S2: IntoStream<Item = T>, | ||
{ | ||
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())) | ||
} | ||
} |