1
- use futures :: future:: Future ;
1
+ use core :: future:: Future ;
2
2
use futures:: stream:: Stream ;
3
-
4
3
use core:: task:: { Poll , Context } ;
4
+ use async_trait:: async_trait;
5
5
6
6
pub async fn ready < T > ( value : T ) -> T {
7
7
value
8
8
}
9
9
10
- pub async fn map < Fut , U , F > ( future : Fut , f : F ) -> U
11
- where F : FnOnce ( Fut :: Output ) -> U ,
12
- Fut : Future ,
13
- {
14
- f ( future. await )
15
- }
10
+ impl < T : ?Sized > FutureExt for T where T : Future { }
16
11
17
- pub async fn then < FutA , FutB , F > ( future : FutA , f : F ) -> FutB :: Output
18
- where F : FnOnce ( FutA :: Output ) -> FutB ,
19
- FutA : Future ,
20
- FutB : Future ,
21
- {
22
- let new_future = f ( future. await ) ;
23
- new_future. await
12
+ #[ async_trait]
13
+ pub trait FutureExt : Future {
14
+ async fn map < U , F > ( self , f : F ) -> U
15
+ where F : FnOnce ( Self :: Output ) -> U + Send ,
16
+ Self : Sized ,
17
+ {
18
+ f ( self . await )
19
+ }
20
+
21
+ async fn then < Fut , F > ( self , f : F ) -> Fut :: Output
22
+ where F : FnOnce ( Self :: Output ) -> Fut + Send ,
23
+ Fut : Future + Send ,
24
+ Self : Sized ,
25
+ {
26
+ let new_future = f ( self . await ) ;
27
+ new_future. await
28
+ }
29
+
30
+ async fn flatten ( self ) -> <<Self as Future >:: Output as Future >:: Output
31
+ where <Self as Future >:: Output : Future + Send ,
32
+ Self : Sized ,
33
+ {
34
+ let nested_future = self . await ;
35
+ nested_future. await
36
+ }
37
+
38
+ async fn inspect < F > ( self , f : F ) -> Self :: Output
39
+ where F : FnOnce ( & Self :: Output ) + Send ,
40
+ Self : Sized ,
41
+ {
42
+ let future_result = self . await ;
43
+ f ( & future_result) ;
44
+ future_result
45
+ }
24
46
}
25
47
48
+
49
+
26
50
pub async fn and_then < FutA , FutB , F , T , U , E > ( future : FutA , f : F ) -> Result < U , E >
27
51
where F : FnOnce ( T ) -> FutB ,
28
52
FutA : Future < Output = Result < T , E > > ,
@@ -65,23 +89,6 @@ pub async fn map_err<Fut, F, T, E, U>(future: Fut, f: F) -> Result<T, U>
65
89
future. await . map_err ( f)
66
90
}
67
91
68
- pub async fn flatten < FutA , FutB > ( future : FutA ) -> FutB :: Output
69
- where FutA : Future < Output = FutB > ,
70
- FutB : Future ,
71
- {
72
- let nested_future = future. await ;
73
- nested_future. await
74
- }
75
-
76
- pub async fn inspect < Fut , F > ( future : Fut , f : F ) -> Fut :: Output
77
- where Fut : Future ,
78
- F : FnOnce ( & Fut :: Output ) ,
79
- {
80
- let future_result = future. await ;
81
- f ( & future_result) ;
82
- future_result
83
- }
84
-
85
92
pub async fn err_into < Fut , T , E , U > ( future : Fut ) -> Result < T , U >
86
93
where Fut : Future < Output = Result < T , E > > ,
87
94
E : Into < U > ,
@@ -165,7 +172,7 @@ mod tests {
165
172
fn test_map ( ) {
166
173
executor:: block_on ( async {
167
174
let future = ready ( 1 ) ;
168
- let new_future = map ( future , |x| x + 3 ) ;
175
+ let new_future = future . map ( |x| x + 3 ) ;
169
176
assert_eq ! ( new_future. await , 4 ) ;
170
177
} ) ;
171
178
}
@@ -174,7 +181,7 @@ mod tests {
174
181
fn test_then ( ) {
175
182
executor:: block_on ( async {
176
183
let future = ready ( 1 ) ;
177
- let new_future = then ( future , |x| ready ( x + 3 ) ) ;
184
+ let new_future = future . then ( |x| ready ( x + 3 ) ) ;
178
185
assert_eq ! ( new_future. await , 4 ) ;
179
186
} ) ;
180
187
}
@@ -228,7 +235,7 @@ mod tests {
228
235
fn test_flatten ( ) {
229
236
executor:: block_on ( async {
230
237
let nested_future = ready ( ready ( 1 ) ) ;
231
- let future = flatten ( nested_future ) ;
238
+ let future = nested_future . flatten ( ) ;
232
239
assert_eq ! ( future. await , 1 ) ;
233
240
} ) ;
234
241
}
@@ -237,7 +244,7 @@ mod tests {
237
244
fn test_inspect ( ) {
238
245
executor:: block_on ( async {
239
246
let future = ready ( 1 ) ;
240
- let new_future = inspect ( future , |& x| assert_eq ! ( x, 1 ) ) ;
247
+ let new_future = future . inspect ( |& x| assert_eq ! ( x, 1 ) ) ;
241
248
assert_eq ! ( new_future. await , 1 ) ;
242
249
} ) ;
243
250
}
0 commit comments