Skip to content

Commit 5d99234

Browse files
taiki-ecramertj
authored andcommitted
Change join and try_join to functions
1 parent 43a29f7 commit 5d99234

File tree

9 files changed

+327
-322
lines changed

9 files changed

+327
-322
lines changed

futures-channel/tests/mpsc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use futures::channel::{mpsc, oneshot};
44
use futures::executor::{block_on, block_on_stream};
5-
use futures::future::{FutureExt, poll_fn};
5+
use futures::future::{join, poll_fn};
66
use futures::stream::{Stream, StreamExt};
77
use futures::sink::{Sink, SinkExt};
88
use futures::task::{Context, Poll};
@@ -98,10 +98,10 @@ fn send_recv_threads_no_capacity() {
9898
let mut readyrx = block_on_stream(readyrx);
9999
let t = thread::spawn(move || {
100100
let mut readytx = readytx.sink_map_err(|_| panic!());
101-
let (send_res_1, send_res_2) = block_on(tx.send(1).join(readytx.send(())));
101+
let (send_res_1, send_res_2) = block_on(join(tx.send(1), readytx.send(())));
102102
send_res_1.unwrap();
103103
send_res_2.unwrap();
104-
block_on(tx.send(2).join(readytx.send(())));
104+
block_on(join(tx.send(2), readytx.send(())));
105105
});
106106

107107
readyrx.next();

futures-util/src/future/join.rs

Lines changed: 137 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use core::pin::Pin;
66
use futures_core::future::Future;
77
use futures_core::task::{Context, Poll};
88
use pin_utils::unsafe_pinned;
9+
use super::assert_future;
910

1011
macro_rules! generate {
1112
($(
@@ -33,7 +34,7 @@ macro_rules! generate {
3334
}
3435

3536
impl<$($Fut: Future),*> $Join<$($Fut),*> {
36-
pub(super) fn new($($Fut: $Fut),*) -> $Join<$($Fut),*> {
37+
fn new($($Fut: $Fut),*) -> $Join<$($Fut),*> {
3738
$Join {
3839
$($Fut: maybe_done($Fut)),*
3940
}
@@ -68,15 +69,146 @@ macro_rules! generate {
6869
}
6970

7071
generate! {
71-
/// Future for the [`join`](super::FutureExt::join) method.
72+
/// Future for the [`join`](join()) function.
7273
(Join, <Fut1, Fut2>),
7374

74-
/// Future for the [`join3`](super::FutureExt::join3) method.
75+
/// Future for the [`join3`] function.
7576
(Join3, <Fut1, Fut2, Fut3>),
7677

77-
/// Future for the [`join4`](super::FutureExt::join4) method.
78+
/// Future for the [`join4`] function.
7879
(Join4, <Fut1, Fut2, Fut3, Fut4>),
7980

80-
/// Future for the [`join5`](super::FutureExt::join5) method.
81+
/// Future for the [`join5`] function.
8182
(Join5, <Fut1, Fut2, Fut3, Fut4, Fut5>),
8283
}
84+
85+
/// Joins the result of two futures, waiting for them both to complete.
86+
///
87+
/// This function will return a new future which awaits both futures to
88+
/// complete. The returned future will finish with a tuple of both results.
89+
///
90+
/// Note that this function consumes the passed futures and returns a
91+
/// wrapped version of it.
92+
///
93+
/// # Examples
94+
///
95+
/// ```
96+
/// #![feature(async_await, await_macro, futures_api)]
97+
/// # futures::executor::block_on(async {
98+
/// use futures::future;
99+
///
100+
/// let a = future::ready(1);
101+
/// let b = future::ready(2);
102+
/// let pair = future::join(a, b);
103+
///
104+
/// assert_eq!(await!(pair), (1, 2));
105+
/// # });
106+
/// ```
107+
pub fn join<Fut1, Fut2>(future1: Fut1, future2: Fut2) -> Join<Fut1, Fut2>
108+
where
109+
Fut1: Future,
110+
Fut2: Future,
111+
{
112+
let f = Join::new(future1, future2);
113+
assert_future::<(Fut1::Output, Fut2::Output), _>(f)
114+
}
115+
116+
/// Same as [`join`](join()), but with more futures.
117+
///
118+
/// # Examples
119+
///
120+
/// ```
121+
/// #![feature(async_await, await_macro, futures_api)]
122+
/// # futures::executor::block_on(async {
123+
/// use futures::future;
124+
///
125+
/// let a = future::ready(1);
126+
/// let b = future::ready(2);
127+
/// let c = future::ready(3);
128+
/// let tuple = future::join3(a, b, c);
129+
///
130+
/// assert_eq!(await!(tuple), (1, 2, 3));
131+
/// # });
132+
/// ```
133+
pub fn join3<Fut1, Fut2, Fut3>(
134+
future1: Fut1,
135+
future2: Fut2,
136+
future3: Fut3,
137+
) -> Join3<Fut1, Fut2, Fut3>
138+
where
139+
Fut1: Future,
140+
Fut2: Future,
141+
Fut3: Future,
142+
{
143+
Join3::new(future1, future2, future3)
144+
}
145+
146+
/// Same as [`join`](join()), but with more futures.
147+
///
148+
/// # Examples
149+
///
150+
/// ```
151+
/// #![feature(async_await, await_macro, futures_api)]
152+
/// # futures::executor::block_on(async {
153+
/// use futures::future;
154+
///
155+
/// let a = future::ready(1);
156+
/// let b = future::ready(2);
157+
/// let c = future::ready(3);
158+
/// let d = future::ready(4);
159+
/// let tuple = future::join4(a, b, c, d);
160+
///
161+
/// assert_eq!(await!(tuple), (1, 2, 3, 4));
162+
/// # });
163+
/// ```
164+
pub fn join4<Fut1, Fut2, Fut3, Fut4>(
165+
future1: Fut1,
166+
future2: Fut2,
167+
future3: Fut3,
168+
future4: Fut4,
169+
) -> Join4<Fut1, Fut2, Fut3, Fut4>
170+
where
171+
Fut1: Future,
172+
Fut2: Future,
173+
Fut3: Future,
174+
Fut3: Future,
175+
Fut4: Future,
176+
{
177+
Join4::new(future1, future2, future3, future4)
178+
}
179+
180+
/// Same as [`join`](join()), but with more futures.
181+
///
182+
/// # Examples
183+
///
184+
/// ```
185+
/// #![feature(async_await, await_macro, futures_api)]
186+
/// # futures::executor::block_on(async {
187+
/// use futures::future;
188+
///
189+
/// let a = future::ready(1);
190+
/// let b = future::ready(2);
191+
/// let c = future::ready(3);
192+
/// let d = future::ready(4);
193+
/// let e = future::ready(5);
194+
/// let tuple = future::join5(a, b, c, d, e);
195+
///
196+
/// assert_eq!(await!(tuple), (1, 2, 3, 4, 5));
197+
/// # });
198+
/// ```
199+
pub fn join5<Fut1, Fut2, Fut3, Fut4, Fut5>(
200+
future1: Fut1,
201+
future2: Fut2,
202+
future3: Fut3,
203+
future4: Fut4,
204+
future5: Fut5,
205+
) -> Join5<Fut1, Fut2, Fut3, Fut4, Fut5>
206+
where
207+
Fut1: Future,
208+
Fut2: Future,
209+
Fut3: Future,
210+
Fut4: Future,
211+
Fut5: Future,
212+
{
213+
Join5::new(future1, future2, future3, future4, future5)
214+
}

futures-util/src/future/mod.rs

Lines changed: 8 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ pub use self::poll_fn::{poll_fn, PollFn};
3333
mod ready;
3434
pub use self::ready::{ready, ok, err, Ready};
3535

36+
mod join;
37+
pub use self::join::{join, join3, join4, join5, Join, Join3, Join4, Join5};
38+
39+
#[cfg(feature = "alloc")]
40+
mod join_all;
41+
#[cfg(feature = "alloc")]
42+
pub use self::join_all::{join_all, JoinAll};
43+
3644
// Combinators
3745
mod flatten;
3846
pub use self::flatten::Flatten;
@@ -46,9 +54,6 @@ pub use self::fuse::Fuse;
4654
mod into_stream;
4755
pub use self::into_stream::IntoStream;
4856

49-
mod join;
50-
pub use self::join::{Join, Join3, Join4, Join5};
51-
5257
mod map;
5358
pub use self::map::Map;
5459

@@ -69,11 +74,6 @@ pub use self::unit_error::UnitError;
6974
mod chain;
7075
pub(crate) use self::chain::Chain;
7176

72-
#[cfg(feature = "alloc")]
73-
mod join_all;
74-
#[cfg(feature = "alloc")]
75-
pub use self::join_all::{join_all, JoinAll};
76-
7777
cfg_target_has_atomic! {
7878
#[cfg(feature = "alloc")]
7979
mod abortable;
@@ -218,139 +218,6 @@ pub trait FutureExt: Future {
218218
}
219219
*/
220220

221-
/// Joins the result of two futures, waiting for them both to complete.
222-
///
223-
/// This function will return a new future which awaits both this and the
224-
/// `other` future to complete. The returned future will finish with a tuple
225-
/// of both results.
226-
///
227-
/// Note that this function consumes the receiving future and returns a
228-
/// wrapped version of it.
229-
///
230-
/// # Examples
231-
///
232-
/// ```
233-
/// #![feature(async_await, await_macro, futures_api)]
234-
/// # futures::executor::block_on(async {
235-
/// use futures::future::{self, FutureExt};
236-
///
237-
/// let a = future::ready(1);
238-
/// let b = future::ready(2);
239-
/// let pair = a.join(b);
240-
///
241-
/// assert_eq!(await!(pair), (1, 2));
242-
/// # });
243-
/// ```
244-
fn join<Fut2>(self, other: Fut2) -> Join<Self, Fut2>
245-
where
246-
Fut2: Future,
247-
Self: Sized,
248-
{
249-
let f = Join::new(self, other);
250-
assert_future::<(Self::Output, Fut2::Output), _>(f)
251-
}
252-
253-
/// Same as `join`, but with more futures.
254-
///
255-
/// # Examples
256-
///
257-
/// ```
258-
/// #![feature(async_await, await_macro, futures_api)]
259-
/// # futures::executor::block_on(async {
260-
/// use futures::future::{self, FutureExt};
261-
///
262-
/// let a = future::ready(1);
263-
/// let b = future::ready(2);
264-
/// let c = future::ready(3);
265-
/// let tuple = a.join3(b, c);
266-
///
267-
/// assert_eq!(await!(tuple), (1, 2, 3));
268-
/// # });
269-
/// ```
270-
fn join3<Fut2, Fut3>(
271-
self,
272-
future2: Fut2,
273-
future3: Fut3,
274-
) -> Join3<Self, Fut2, Fut3>
275-
where
276-
Fut2: Future,
277-
Fut3: Future,
278-
Self: Sized,
279-
{
280-
Join3::new(self, future2, future3)
281-
}
282-
283-
/// Same as `join`, but with more futures.
284-
///
285-
/// # Examples
286-
///
287-
/// ```
288-
/// #![feature(async_await, await_macro, futures_api)]
289-
/// # futures::executor::block_on(async {
290-
/// use futures::future::{self, FutureExt};
291-
///
292-
/// let a = future::ready(1);
293-
/// let b = future::ready(2);
294-
/// let c = future::ready(3);
295-
/// let d = future::ready(4);
296-
/// let tuple = a.join4(b, c, d);
297-
///
298-
/// assert_eq!(await!(tuple), (1, 2, 3, 4));
299-
/// # });
300-
/// ```
301-
fn join4<Fut2, Fut3, Fut4>(
302-
self,
303-
future2: Fut2,
304-
future3: Fut3,
305-
future4: Fut4,
306-
) -> Join4<Self, Fut2, Fut3, Fut4>
307-
where
308-
Fut2: Future,
309-
Fut3: Future,
310-
Fut3: Future,
311-
Fut4: Future,
312-
Self: Sized,
313-
{
314-
Join4::new(self, future2, future3, future4)
315-
}
316-
317-
/// Same as `join`, but with more futures.
318-
///
319-
/// # Examples
320-
///
321-
/// ```
322-
/// #![feature(async_await, await_macro, futures_api)]
323-
/// # futures::executor::block_on(async {
324-
/// use futures::future::{self, FutureExt};
325-
///
326-
/// let a = future::ready(1);
327-
/// let b = future::ready(2);
328-
/// let c = future::ready(3);
329-
/// let d = future::ready(4);
330-
/// let e = future::ready(5);
331-
/// let tuple = a.join5(b, c, d, e);
332-
///
333-
/// assert_eq!(await!(tuple), (1, 2, 3, 4, 5));
334-
/// # });
335-
/// ```
336-
fn join5<Fut2, Fut3, Fut4, Fut5>(
337-
self,
338-
future2: Fut2,
339-
future3: Fut3,
340-
future4: Fut4,
341-
future5: Fut5,
342-
) -> Join5<Self, Fut2, Fut3, Fut4, Fut5>
343-
where
344-
Fut2: Future,
345-
Fut3: Future,
346-
Fut3: Future,
347-
Fut4: Future,
348-
Fut5: Future,
349-
Self: Sized,
350-
{
351-
Join5::new(self, future2, future3, future4, future5)
352-
}
353-
354221
/* ToDo: futures-core cannot implement Future for Either anymore because of
355222
the orphan rule. Remove? Implement our own `Either`?
356223
/// Wrap this future in an `Either` future, making it the left-hand variant

futures-util/src/sink/fanout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<Si1, Si2, Item> Sink<Item> for Fanout<Si1, Si2>
8989
#[cfg(test)]
9090
#[cfg(feature = "std")]
9191
mod tests {
92-
use crate::future::FutureExt;
92+
use crate::future::join3;
9393
use crate::sink::SinkExt;
9494
use crate::stream::{self, StreamExt};
9595
use futures_executor::block_on;
@@ -108,7 +108,7 @@ mod tests {
108108

109109
let collect_fut1 = rx1.collect::<Vec<_>>();
110110
let collect_fut2 = rx2.collect::<Vec<_>>();
111-
let (_, vec1, vec2) = block_on(fwd.join3(collect_fut1, collect_fut2));
111+
let (_, vec1, vec2) = block_on(join3(fwd, collect_fut1, collect_fut2));
112112

113113
let expected = (0..10).collect::<Vec<_>>();
114114

0 commit comments

Comments
 (0)