@@ -6,46 +6,6 @@ use crate::stream::stream::map::Map;
6
6
use crate :: stream:: { IntoStream , Stream } ;
7
7
use crate :: task:: { Context , Poll } ;
8
8
9
- pin_project ! {
10
- /// This `struct` is created by the [`flat_map`] method on [`Stream`]. See its
11
- /// documentation for more.
12
- ///
13
- /// [`flat_map`]: trait.Stream.html#method.flat_map
14
- /// [`Stream`]: trait.Stream.html
15
- #[ allow( missing_debug_implementations) ]
16
- pub struct FlatMap <S , U , T , F > {
17
- #[ pin]
18
- inner: FlattenCompat <Map <S , F , T , U >, U >,
19
- }
20
- }
21
-
22
- impl < S , U , F > FlatMap < S , U , S :: Item , F >
23
- where
24
- S : Stream ,
25
- U : IntoStream ,
26
- F : FnMut ( S :: Item ) -> U ,
27
- {
28
- pub ( super ) fn new ( stream : S , f : F ) -> FlatMap < S , U , S :: Item , F > {
29
- FlatMap {
30
- inner : FlattenCompat :: new ( stream. map ( f) ) ,
31
- }
32
- }
33
- }
34
-
35
- impl < S , U , F > Stream for FlatMap < S , U , S :: Item , F >
36
- where
37
- S : Stream ,
38
- S :: Item : IntoStream < IntoStream = U , Item = U :: Item > ,
39
- U : Stream ,
40
- F : FnMut ( S :: Item ) -> U ,
41
- {
42
- type Item = U :: Item ;
43
-
44
- fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
45
- self . project ( ) . inner . poll_next ( cx)
46
- }
47
- }
48
-
49
9
pin_project ! {
50
10
/// This `struct` is created by the [`flatten`] method on [`Stream`]. See its
51
11
/// documentation for more.
@@ -55,7 +15,9 @@ pin_project! {
55
15
#[ allow( missing_debug_implementations) ]
56
16
pub struct Flatten <S , U > {
57
17
#[ pin]
58
- inner: FlattenCompat <S , U >
18
+ stream: S ,
19
+ #[ pin]
20
+ inner_stream: Option <U >,
59
21
}
60
22
}
61
23
@@ -66,47 +28,13 @@ where
66
28
{
67
29
pub ( super ) fn new ( stream : S ) -> Flatten < S , S :: Item > {
68
30
Flatten {
69
- inner : FlattenCompat :: new ( stream) ,
70
- }
71
- }
72
- }
73
-
74
- impl < S , U > Stream for Flatten < S , <S :: Item as IntoStream >:: IntoStream >
75
- where
76
- S : Stream ,
77
- S :: Item : IntoStream < IntoStream = U , Item = U :: Item > ,
78
- U : Stream ,
79
- {
80
- type Item = U :: Item ;
81
-
82
- fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
83
- self . project ( ) . inner . poll_next ( cx)
84
- }
85
- }
86
-
87
- pin_project ! {
88
- /// Real logic of both `Flatten` and `FlatMap` which simply delegate to
89
- /// this type.
90
- #[ derive( Clone , Debug ) ]
91
- struct FlattenCompat <S , U > {
92
- #[ pin]
93
- stream: S ,
94
- #[ pin]
95
- frontiter: Option <U >,
96
- }
97
- }
98
-
99
- impl < S , U > FlattenCompat < S , U > {
100
- /// Adapts an iterator by flattening it, for use in `flatten()` and `flat_map()`.
101
- fn new ( stream : S ) -> FlattenCompat < S , U > {
102
- FlattenCompat {
103
31
stream,
104
- frontiter : None ,
32
+ inner_stream : None ,
105
33
}
106
34
}
107
35
}
108
36
109
- impl < S , U > Stream for FlattenCompat < S , U >
37
+ impl < S , U > Stream for Flatten < S , < S :: Item as IntoStream > :: IntoStream >
110
38
where
111
39
S : Stream ,
112
40
S :: Item : IntoStream < IntoStream = U , Item = U :: Item > ,
@@ -117,42 +45,16 @@ where
117
45
fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
118
46
let mut this = self . project ( ) ;
119
47
loop {
120
- if let Some ( inner) = this. frontiter . as_mut ( ) . as_pin_mut ( ) {
48
+ if let Some ( inner) = this. inner_stream . as_mut ( ) . as_pin_mut ( ) {
121
49
if let item @ Some ( _) = futures_core:: ready!( inner. poll_next( cx) ) {
122
50
return Poll :: Ready ( item) ;
123
51
}
124
52
}
125
53
126
54
match futures_core:: ready!( this. stream. as_mut( ) . poll_next( cx) ) {
127
55
None => return Poll :: Ready ( None ) ,
128
- Some ( inner) => this. frontiter . set ( Some ( inner. into_stream ( ) ) ) ,
56
+ Some ( inner) => this. inner_stream . set ( Some ( inner. into_stream ( ) ) ) ,
129
57
}
130
58
}
131
59
}
132
60
}
133
-
134
- #[ cfg( test) ]
135
- mod tests {
136
- use super :: FlattenCompat ;
137
-
138
- use crate :: prelude:: * ;
139
- use crate :: task;
140
-
141
- use std:: collections:: VecDeque ;
142
-
143
- #[ test]
144
- fn test_poll_next ( ) -> std:: io:: Result < ( ) > {
145
- let inner1: VecDeque < u8 > = vec ! [ 1 , 2 , 3 ] . into_iter ( ) . collect ( ) ;
146
- let inner2: VecDeque < u8 > = vec ! [ 4 , 5 , 6 ] . into_iter ( ) . collect ( ) ;
147
-
148
- let s: VecDeque < _ > = vec ! [ inner1, inner2] . into_iter ( ) . collect ( ) ;
149
-
150
- task:: block_on ( async move {
151
- let flat = FlattenCompat :: new ( s) ;
152
- let v: Vec < u8 > = flat. collect ( ) . await ;
153
-
154
- assert_eq ! ( v, vec![ 1 , 2 , 3 , 4 , 5 , 6 ] ) ;
155
- Ok ( ( ) )
156
- } )
157
- }
158
- }
0 commit comments