@@ -3,6 +3,8 @@ cfg_unstable! {
33 mod flatten;
44 mod race;
55 mod try_race;
6+ mod join;
7+ mod try_join;
68
79 use std:: time:: Duration ;
810
@@ -11,6 +13,8 @@ cfg_unstable! {
1113 use crate :: future:: IntoFuture ;
1214 use race:: Race ;
1315 use try_race:: TryRace ;
16+ use join:: Join ;
17+ use try_join:: TryJoin ;
1418}
1519
1620extension_trait ! {
@@ -264,6 +268,90 @@ extension_trait! {
264268 {
265269 TryRace :: new( self , other)
266270 }
271+
272+ #[ doc = r#"
273+ Waits for two similarly-typed futures to complete.
274+
275+ Awaits multiple futures simultaneously, returning the output of the
276+ futures once both complete.
277+
278+ This function returns a new future which polls both futures
279+ concurrently.
280+
281+ # Examples
282+
283+ ```
284+ # async_std::task::block_on(async {
285+ use async_std::prelude::*;
286+ use async_std::future;
287+
288+ let a = future::ready(1u8);
289+ let b = future::ready(2u8);
290+
291+ let f = a.join(b);
292+ assert_eq!(f.await, (1u8, 2u8));
293+ # });
294+ ```
295+ "# ]
296+ #[ cfg( any( feature = "unstable" , feature = "docs" ) ) ]
297+ #[ cfg_attr( feature = "docs" , doc( cfg( unstable) ) ) ]
298+ fn join<F >(
299+ self ,
300+ other: F
301+ ) -> impl Future <Output = ( <Self as std:: future:: Future >:: Output , <F as std:: future:: Future >:: Output ) > [ Join <Self , F >]
302+ where
303+ Self : std:: future:: Future + Sized ,
304+ F : std:: future:: Future <Output = <Self as std:: future:: Future >:: Output >,
305+ {
306+ Join :: new( self , other)
307+ }
308+
309+ #[ doc = r#"
310+ Waits for two similarly-typed fallible futures to complete.
311+
312+ Awaits multiple futures simultaneously, returning all results once
313+ complete.
314+
315+ `try_join` is similar to [`join`], but returns an error immediately
316+ if a future resolves to an error.
317+
318+ [`join`]: #method.join
319+
320+ # Examples
321+
322+ ```
323+ # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
324+ #
325+ use async_std::prelude::*;
326+ use async_std::future;
327+
328+ let a = future::ready(Err("Error"));
329+ let b = future::ready(Ok(1u8));
330+
331+ let f = a.try_join(b);
332+ assert_eq!(f.await, Err("Error"));
333+
334+ let a = future::ready(Ok::<u8, String>(1u8));
335+ let b = future::ready(Ok::<u8, String>(2u8));
336+
337+ let f = a.try_join(b);
338+ assert_eq!(f.await, Ok((1u8, 2u8)));
339+ #
340+ # Ok(()) }) }
341+ ```
342+ "# ]
343+ #[ cfg( any( feature = "unstable" , feature = "docs" ) ) ]
344+ #[ cfg_attr( feature = "docs" , doc( cfg( unstable) ) ) ]
345+ fn try_join<F , T , E >(
346+ self ,
347+ other: F
348+ ) -> impl Future <Output = Result <( T , T ) , E >> [ TryJoin <Self , F >]
349+ where
350+ Self : std:: future:: Future <Output = Result <T , E >> + Sized ,
351+ F : std:: future:: Future <Output = <Self as std:: future:: Future >:: Output >,
352+ {
353+ TryJoin :: new( self , other)
354+ }
267355 }
268356
269357 impl <F : Future + Unpin + ?Sized > Future for Box <F > {
0 commit comments