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
24
- }
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
+ }
25
20
26
- pub async fn and_then < FutA , FutB , F , T , U , E > ( future : FutA , f : F ) -> Result < U , E >
27
- where F : FnOnce ( T ) -> FutB ,
28
- FutA : Future < Output = Result < T , E > > ,
29
- FutB : Future < Output = Result < U , E > > ,
30
- {
31
- match future. await {
32
- Ok ( ok) => {
33
- let new_future = f ( ok) ;
34
- new_future. await
35
- } ,
36
- Err ( err) => Err ( err) ,
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
37
28
}
38
- }
39
29
40
- pub async fn or_else < FutA , FutB , F , T , E , U > ( future : FutA , f : F ) -> Result < T , U >
41
- where F : FnOnce ( E ) -> FutB ,
42
- FutA : Future < Output = Result < T , E > > ,
43
- FutB : Future < Output = Result < T , U > > ,
44
- {
45
- match future. await {
46
- Ok ( ok) => Ok ( ok) ,
47
- Err ( err) => {
48
- let new_future = f ( err) ;
49
- new_future. await
50
- } ,
30
+ async fn flatten ( self ) -> <Self :: Output as Future >:: Output
31
+ where Self :: Output : Future + Send ,
32
+ Self : Sized ,
33
+ {
34
+ let nested_future = self . await ;
35
+ nested_future. await
51
36
}
52
- }
53
37
54
- pub async fn map_ok < Fut , F , T , U , E > ( future : Fut , f : F ) -> Result < U , E >
55
- where F : FnOnce ( T ) -> U ,
56
- Fut : Future < Output = Result < T , E > > ,
57
- {
58
- future. await . map ( f)
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
+ }
59
46
}
60
47
61
- pub async fn map_err < Fut , F , T , E , U > ( future : Fut , f : F ) -> Result < T , U >
62
- where F : FnOnce ( E ) -> U ,
63
- Fut : Future < Output = Result < T , E > > ,
64
- {
65
- future. await . map_err ( f)
66
- }
48
+ impl < T , E , Fut : ?Sized > TryFutureExt < T , E > for Fut where Fut : Future < Output = Result < T , E > > { }
49
+
50
+ #[ async_trait]
51
+ pub trait TryFutureExt < T , E > : Future < Output = Result < T , E > > {
52
+
53
+ async fn and_then < U , F , FutB > ( self , f : F ) -> Result < U , E >
54
+ where F : FnOnce ( T ) -> FutB + Send ,
55
+ FutB : Future < Output = Result < U , E > > + Send ,
56
+ Self : Sized ,
57
+ T : Send + ' async_trait ,
58
+ E : Send + ' async_trait ,
59
+ {
60
+ match self . await {
61
+ Ok ( ok) => {
62
+ let new_future = f ( ok) ;
63
+ new_future. await
64
+ } ,
65
+ Err ( err) => Err ( err) ,
66
+ }
67
+ }
67
68
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
- }
69
+ async fn or_else < U , F , FutB > ( self , f : F ) -> Result < T , U >
70
+ where F : FnOnce ( E ) -> FutB + Send ,
71
+ FutB : Future < Output = Result < T , U > > + Send ,
72
+ Self : Sized ,
73
+ T : Send + ' async_trait ,
74
+ E : Send + ' async_trait ,
75
+ {
76
+ match self . await {
77
+ Ok ( ok) => Ok ( ok) ,
78
+ Err ( err) => {
79
+ let new_future = f ( err) ;
80
+ new_future. await
81
+ } ,
82
+ }
83
+ }
75
84
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
- }
85
+ async fn map_ok < U , F > ( self , f : F ) -> Result < U , E >
86
+ where F : FnOnce ( T ) -> U + Send ,
87
+ Self : Sized ,
88
+ T : Send + ' async_trait ,
89
+ E : Send + ' async_trait ,
90
+ {
91
+ self . await . map ( f )
92
+ }
84
93
85
- pub async fn err_into < Fut , T , E , U > ( future : Fut ) -> Result < T , U >
86
- where Fut : Future < Output = Result < T , E > > ,
87
- E : Into < U > ,
88
- {
89
- future. await . map_err ( Into :: into)
90
- }
94
+ async fn map_err < U , F > ( self , f : F ) -> Result < T , U >
95
+ where F : FnOnce ( E ) -> U + Send ,
96
+ Self : Sized ,
97
+ T : Send + ' async_trait ,
98
+ E : Send + ' async_trait ,
99
+ {
100
+ self . await . map_err ( f)
101
+ }
91
102
92
- pub async fn unwrap_or_else < Fut , T , E , F > ( future : Fut , f : F ) -> T
93
- where Fut : Future < Output = Result < T , E > > ,
94
- F : FnOnce ( E ) -> T ,
95
- {
96
- future. await . unwrap_or_else ( f)
103
+ async fn err_into < U > ( self ) -> Result < T , U >
104
+ where E : Into < U > ,
105
+ Self : Sized ,
106
+ T : Send + ' async_trait ,
107
+ E : Send + ' async_trait ,
108
+ {
109
+ self . await . map_err ( Into :: into)
110
+ }
111
+
112
+ async fn unwrap_or_else < F > ( self , f : F ) -> T
113
+ where F : FnOnce ( E ) -> T + Send ,
114
+ Self : Sized ,
115
+ T : Send + ' async_trait ,
116
+ E : Send + ' async_trait ,
117
+ {
118
+ self . await . unwrap_or_else ( f)
119
+ }
97
120
}
98
121
99
122
pub fn flatten_stream < Fut , St , T > ( future : Fut ) -> impl Stream < Item = T >
@@ -165,7 +188,7 @@ mod tests {
165
188
fn test_map ( ) {
166
189
executor:: block_on ( async {
167
190
let future = ready ( 1 ) ;
168
- let new_future = map ( future , |x| x + 3 ) ;
191
+ let new_future = future . map ( |x| x + 3 ) ;
169
192
assert_eq ! ( new_future. await , 4 ) ;
170
193
} ) ;
171
194
}
@@ -174,7 +197,7 @@ mod tests {
174
197
fn test_then ( ) {
175
198
executor:: block_on ( async {
176
199
let future = ready ( 1 ) ;
177
- let new_future = then ( future , |x| ready ( x + 3 ) ) ;
200
+ let new_future = future . then ( |x| ready ( x + 3 ) ) ;
178
201
assert_eq ! ( new_future. await , 4 ) ;
179
202
} ) ;
180
203
}
@@ -183,7 +206,7 @@ mod tests {
183
206
fn test_and_then_ok ( ) {
184
207
executor:: block_on ( async {
185
208
let future = ready ( Ok :: < i32 , i32 > ( 1 ) ) ;
186
- let new_future = and_then ( future , |x| ready ( Ok :: < i32 , i32 > ( x + 3 ) ) ) ;
209
+ let new_future = future . and_then ( |x| ready ( Ok :: < i32 , i32 > ( x + 3 ) ) ) ;
187
210
assert_eq ! ( new_future. await , Ok ( 4 ) ) ;
188
211
} ) ;
189
212
}
@@ -192,7 +215,7 @@ mod tests {
192
215
fn test_and_then_err ( ) {
193
216
executor:: block_on ( async {
194
217
let future = ready ( Err :: < i32 , i32 > ( 1 ) ) ;
195
- let new_future = and_then ( future , |x| ready ( Ok :: < i32 , i32 > ( x + 3 ) ) ) ;
218
+ let new_future = future . and_then ( |x| ready ( Ok ( x + 3 ) ) ) ;
196
219
assert_eq ! ( new_future. await , Err ( 1 ) ) ;
197
220
} ) ;
198
221
}
@@ -201,7 +224,7 @@ mod tests {
201
224
fn test_or_else ( ) {
202
225
executor:: block_on ( async {
203
226
let future = ready ( Err :: < i32 , i32 > ( 1 ) ) ;
204
- let new_future = or_else ( future , |x| ready ( Err :: < i32 , i32 > ( x + 3 ) ) ) ;
227
+ let new_future = future . or_else ( |x| ready ( Err ( x + 3 ) ) ) ;
205
228
assert_eq ! ( new_future. await , Err ( 4 ) ) ;
206
229
} ) ;
207
230
}
@@ -210,7 +233,7 @@ mod tests {
210
233
fn test_map_ok ( ) {
211
234
executor:: block_on ( async {
212
235
let future = ready ( Ok :: < i32 , i32 > ( 1 ) ) ;
213
- let new_future = map_ok ( future , |x| x + 3 ) ;
236
+ let new_future = future . map_ok ( |x| x + 3 ) ;
214
237
assert_eq ! ( new_future. await , Ok ( 4 ) ) ;
215
238
} ) ;
216
239
}
@@ -219,7 +242,7 @@ mod tests {
219
242
fn test_map_err ( ) {
220
243
executor:: block_on ( async {
221
244
let future = ready ( Err :: < i32 , i32 > ( 1 ) ) ;
222
- let new_future = map_err ( future , |x| x + 3 ) ;
245
+ let new_future = future . map_err ( |x| x + 3 ) ;
223
246
assert_eq ! ( new_future. await , Err ( 4 ) ) ;
224
247
} ) ;
225
248
}
@@ -228,7 +251,7 @@ mod tests {
228
251
fn test_flatten ( ) {
229
252
executor:: block_on ( async {
230
253
let nested_future = ready ( ready ( 1 ) ) ;
231
- let future = flatten ( nested_future ) ;
254
+ let future = nested_future . flatten ( ) ;
232
255
assert_eq ! ( future. await , 1 ) ;
233
256
} ) ;
234
257
}
@@ -237,7 +260,7 @@ mod tests {
237
260
fn test_inspect ( ) {
238
261
executor:: block_on ( async {
239
262
let future = ready ( 1 ) ;
240
- let new_future = inspect ( future , |& x| assert_eq ! ( x, 1 ) ) ;
263
+ let new_future = future . inspect ( |& x| assert_eq ! ( x, 1 ) ) ;
241
264
assert_eq ! ( new_future. await , 1 ) ;
242
265
} ) ;
243
266
}
@@ -246,7 +269,7 @@ mod tests {
246
269
fn test_err_into ( ) {
247
270
executor:: block_on ( async {
248
271
let future_err_u8 = ready ( Err :: < ( ) , u8 > ( 1 ) ) ;
249
- let future_err_i32 = err_into :: < _ , _ , _ , i32 > ( future_err_u8 ) ;
272
+ let future_err_i32 = future_err_u8 . err_into ( ) ;
250
273
251
274
assert_eq ! ( future_err_i32. await , Err :: <( ) , i32 >( 1 ) ) ;
252
275
} ) ;
@@ -256,7 +279,7 @@ mod tests {
256
279
fn test_unwrap_or_else ( ) {
257
280
executor:: block_on ( async {
258
281
let future = ready ( Err :: < ( ) , & str > ( "Boom!" ) ) ;
259
- let new_future = unwrap_or_else ( future , |_| ( ) ) ;
282
+ let new_future = future . unwrap_or_else ( |_| ( ) ) ;
260
283
assert_eq ! ( new_future. await , ( ) ) ;
261
284
} ) ;
262
285
}
0 commit comments