@@ -8,29 +8,75 @@ use futures_core::Stream;
8
8
///
9
9
/// This stream is returned by `join!`.
10
10
#[ derive( Debug ) ]
11
- pub struct JoinStream < ' a , L , R , T > {
12
- left : & ' a mut L ,
13
- right : & ' a mut R ,
11
+ pub struct JoinStream < L , R , T > {
12
+ left : L ,
13
+ right : R ,
14
14
_marker : PhantomData < T > ,
15
15
}
16
16
17
- impl < L , R , T > Unpin for JoinStream < ' _ , L , R , T > { }
17
+ impl < L , R , T > Unpin for JoinStream < L , R , T > { }
18
18
19
- impl < ' a , L , R , T > Stream for JoinStream < ' a , L , R , T >
19
+ impl < L , R , T > JoinStream < L , R , T > {
20
+ #[ doc( hidden) ]
21
+ pub fn new ( left : L , right : R ) -> Self {
22
+ Self {
23
+ left,
24
+ right,
25
+ _marker : PhantomData ,
26
+ }
27
+ }
28
+ }
29
+
30
+ impl < L , R , T > Stream for JoinStream < L , R , T >
20
31
where
21
32
L : Stream < Item = T > + Unpin ,
22
33
R : Stream < Item = T > + Unpin ,
23
34
{
24
35
type Item = T ;
25
36
26
37
fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
27
- if let Poll :: Ready ( Some ( item) ) = Pin :: new ( & mut * self . left ) . poll_next ( cx) {
38
+ if let Poll :: Ready ( Some ( item) ) = Pin :: new ( & mut self . left ) . poll_next ( cx) {
28
39
// The first stream made progress. The JoinStream needs to be polled
29
40
// again to check the progress of the second stream.
30
41
cx. waker ( ) . wake_by_ref ( ) ;
31
42
Poll :: Ready ( Some ( item) )
32
43
} else {
33
- Pin :: new ( & mut * self . right ) . poll_next ( cx)
44
+ Pin :: new ( & mut self . right ) . poll_next ( cx)
34
45
}
35
46
}
36
47
}
48
+
49
+ /// Combines multiple streams into a single stream of all their outputs.
50
+ ///
51
+ /// This macro is only usable inside of async functions, closures, and blocks.
52
+ ///
53
+ /// # Examples
54
+ ///
55
+ /// ```
56
+ /// # futures::executor::block_on(async {
57
+ /// use async_macros::join_stream as join;
58
+ /// use futures::stream::{self, StreamExt};
59
+ /// use futures::future::ready;
60
+ ///
61
+ /// let a = &mut stream::once(ready(1u8));
62
+ /// let b = &mut stream::once(ready(2u8));
63
+ /// let c = &mut stream::once(ready(3u8));
64
+ ///
65
+ /// let mut s = join!(a, b, c);
66
+ ///
67
+ /// assert_eq!(s.next().await, Some(1u8));
68
+ /// assert_eq!(s.next().await, Some(2u8));
69
+ /// assert_eq!(s.next().await, Some(3u8));
70
+ /// assert_eq!(s.next().await, None);
71
+ /// # });
72
+ /// ```
73
+ #[ macro_export]
74
+ macro_rules! join_stream {
75
+ ( $stream1: ident, $stream2: ident, $( $stream: ident) ,* $( , ) ?) => { {
76
+ let joined = $crate:: JoinStream :: new( $stream1, $stream2) ;
77
+ $(
78
+ let joined = $crate:: JoinStream :: new( joined, $stream) ;
79
+ ) *
80
+ joined
81
+ } } ;
82
+ }
0 commit comments