Skip to content

Commit 9a1facd

Browse files
committed
add future_ext
1 parent 9876a21 commit 9a1facd

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/future/futures_ext.rs

Lines changed: 43 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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;

0 commit comments

Comments
 (0)