@@ -45,60 +45,102 @@ pub trait FutureExt: Future {
45
45
}
46
46
}
47
47
48
- pub async fn and_then < FutA , FutB , F , T , U , E > ( future : FutA , f : F ) -> Result < U , E >
49
- where F : FnOnce ( T ) -> FutB ,
50
- FutA : Future < Output = Result < T , E > > ,
51
- FutB : Future < Output = Result < U , E > > ,
52
- {
53
- match future. await {
54
- Ok ( ok) => {
55
- let new_future = f ( ok) ;
56
- new_future. await
57
- } ,
58
- Err ( err) => Err ( err) ,
59
- }
48
+ #[ async_trait]
49
+ pub trait TryFuture : Future {
50
+ type Ok ;
51
+ type Error ;
52
+
53
+ async fn and_then < U , F , FutB > ( self , f : F ) -> Result < U , Self :: Error >
54
+ where F : FnOnce ( Self :: Ok ) -> FutB + Send ,
55
+ FutB : Future < Output = Result < U , Self :: Error > > + Send ,
56
+ Self : Sized ;
57
+
58
+ async fn or_else < U , F , FutB > ( self , f : F ) -> Result < Self :: Ok , U >
59
+ where F : FnOnce ( Self :: Error ) -> FutB + Send ,
60
+ FutB : Future < Output = Result < Self :: Ok , U > > + Send ,
61
+ Self : Sized ;
62
+
63
+ async fn map_ok < U , F > ( self , f : F ) -> Result < U , Self :: Error >
64
+ where F : FnOnce ( Self :: Ok ) -> U + Send ,
65
+ Self : Sized ;
66
+
67
+ async fn map_err < U , F > ( self , f : F ) -> Result < Self :: Ok , U >
68
+ where F : FnOnce ( Self :: Error ) -> U + Send ,
69
+ Self : Sized ;
70
+
71
+ async fn err_into < U > ( self ) -> Result < Self :: Ok , U >
72
+ where Self :: Error : Into < U > ,
73
+ Self : Sized ;
74
+
75
+ async fn unwrap_or_else < F > ( self , f : F ) -> Self :: Ok
76
+ where F : FnOnce ( Self :: Error ) -> Self :: Ok + Send ,
77
+ Self : Sized ;
60
78
}
61
79
62
- pub async fn or_else < FutA , FutB , F , T , E , U > ( future : FutA , f : F ) -> Result < T , U >
63
- where F : FnOnce ( E ) -> FutB ,
64
- FutA : Future < Output = Result < T , E > > ,
65
- FutB : Future < Output = Result < T , U > > ,
80
+ #[ async_trait]
81
+ impl < T , E , Fut > TryFuture for Fut
82
+ where Fut : ?Sized + Future < Output = Result < T , E > > + Send ,
83
+ T : Send + ' static ,
84
+ E : Send + ' static ,
66
85
{
67
- match future. await {
68
- Ok ( ok) => Ok ( ok) ,
69
- Err ( err) => {
70
- let new_future = f ( err) ;
71
- new_future. await
72
- } ,
86
+ type Ok = T ;
87
+ type Error = E ;
88
+
89
+ async fn and_then < U , F , FutB > ( self , f : F ) -> Result < U , Self :: Error >
90
+ where F : FnOnce ( Self :: Ok ) -> FutB + Send ,
91
+ FutB : Future < Output = Result < U , Self :: Error > > + Send ,
92
+ Self : Sized
93
+ {
94
+ match self . await {
95
+ Ok ( ok) => {
96
+ let new_future = f ( ok) ;
97
+ new_future. await
98
+ } ,
99
+ Err ( err) => Err ( err) ,
100
+ }
73
101
}
74
- }
75
102
76
- pub async fn map_ok < Fut , F , T , U , E > ( future : Fut , f : F ) -> Result < U , E >
77
- where F : FnOnce ( T ) -> U ,
78
- Fut : Future < Output = Result < T , E > > ,
79
- {
80
- future. await . map ( f)
81
- }
103
+ async fn or_else < U , F , FutB > ( self , f : F ) -> Result < Self :: Ok , U >
104
+ where F : FnOnce ( Self :: Error ) -> FutB + Send ,
105
+ FutB : Future < Output = Result < Self :: Ok , U > > + Send ,
106
+ Self : Sized ,
107
+ {
108
+ match self . await {
109
+ Ok ( ok) => Ok ( ok) ,
110
+ Err ( err) => {
111
+ let new_future = f ( err) ;
112
+ new_future. await
113
+ } ,
114
+ }
115
+ }
82
116
83
- pub async fn map_err < Fut , F , T , E , U > ( future : Fut , f : F ) -> Result < T , U >
84
- where F : FnOnce ( E ) -> U ,
85
- Fut : Future < Output = Result < T , E > > ,
86
- {
87
- future . await . map_err ( f)
88
- }
117
+ async fn map_ok < U , F > ( self , f : F ) -> Result < U , Self :: Error >
118
+ where F : FnOnce ( Self :: Ok ) -> U + Send ,
119
+ Self : Sized
120
+ {
121
+ self . await . map ( f)
122
+ }
89
123
90
- pub async fn err_into < Fut , T , E , U > ( future : Fut ) -> Result < T , U >
91
- where Fut : Future < Output = Result < T , E > > ,
92
- E : Into < U > ,
93
- {
94
- future . await . map_err ( Into :: into )
95
- }
124
+ async fn map_err < U , F > ( self , f : F ) -> Result < Self :: Ok , U >
125
+ where F : FnOnce ( Self :: Error ) -> U + Send ,
126
+ Self : Sized ,
127
+ {
128
+ self . await . map_err ( f )
129
+ }
96
130
97
- pub async fn unwrap_or_else < Fut , T , E , F > ( future : Fut , f : F ) -> T
98
- where Fut : Future < Output = Result < T , E > > ,
99
- F : FnOnce ( E ) -> T ,
100
- {
101
- future. await . unwrap_or_else ( f)
131
+ async fn err_into < U > ( self ) -> Result < Self :: Ok , U >
132
+ where Self :: Error : Into < U > ,
133
+ Self : Sized ,
134
+ {
135
+ self . await . map_err ( Into :: into)
136
+ }
137
+
138
+ async fn unwrap_or_else < F > ( self , f : F ) -> Self :: Ok
139
+ where F : FnOnce ( Self :: Error ) -> Self :: Ok + Send ,
140
+ Self : Sized ,
141
+ {
142
+ self . await . unwrap_or_else ( f)
143
+ }
102
144
}
103
145
104
146
pub fn flatten_stream < Fut , St , T > ( future : Fut ) -> impl Stream < Item = T >
@@ -188,7 +230,7 @@ mod tests {
188
230
fn test_and_then_ok ( ) {
189
231
executor:: block_on ( async {
190
232
let future = ready ( Ok :: < i32 , i32 > ( 1 ) ) ;
191
- let new_future = and_then ( future , |x| ready ( Ok :: < i32 , i32 > ( x + 3 ) ) ) ;
233
+ let new_future = future . and_then ( |x| ready ( Ok :: < i32 , i32 > ( x + 3 ) ) ) ;
192
234
assert_eq ! ( new_future. await , Ok ( 4 ) ) ;
193
235
} ) ;
194
236
}
@@ -197,7 +239,7 @@ mod tests {
197
239
fn test_and_then_err ( ) {
198
240
executor:: block_on ( async {
199
241
let future = ready ( Err :: < i32 , i32 > ( 1 ) ) ;
200
- let new_future = and_then ( future , |x| ready ( Ok :: < i32 , i32 > ( x + 3 ) ) ) ;
242
+ let new_future = future . and_then ( |x| ready ( Ok ( x + 3 ) ) ) ;
201
243
assert_eq ! ( new_future. await , Err ( 1 ) ) ;
202
244
} ) ;
203
245
}
@@ -206,7 +248,7 @@ mod tests {
206
248
fn test_or_else ( ) {
207
249
executor:: block_on ( async {
208
250
let future = ready ( Err :: < i32 , i32 > ( 1 ) ) ;
209
- let new_future = or_else ( future , |x| ready ( Err :: < i32 , i32 > ( x + 3 ) ) ) ;
251
+ let new_future = future . or_else ( |x| ready ( Err ( x + 3 ) ) ) ;
210
252
assert_eq ! ( new_future. await , Err ( 4 ) ) ;
211
253
} ) ;
212
254
}
@@ -215,7 +257,7 @@ mod tests {
215
257
fn test_map_ok ( ) {
216
258
executor:: block_on ( async {
217
259
let future = ready ( Ok :: < i32 , i32 > ( 1 ) ) ;
218
- let new_future = map_ok ( future , |x| x + 3 ) ;
260
+ let new_future = future . map_ok ( |x| x + 3 ) ;
219
261
assert_eq ! ( new_future. await , Ok ( 4 ) ) ;
220
262
} ) ;
221
263
}
@@ -224,7 +266,7 @@ mod tests {
224
266
fn test_map_err ( ) {
225
267
executor:: block_on ( async {
226
268
let future = ready ( Err :: < i32 , i32 > ( 1 ) ) ;
227
- let new_future = map_err ( future , |x| x + 3 ) ;
269
+ let new_future = future . map_err ( |x| x + 3 ) ;
228
270
assert_eq ! ( new_future. await , Err ( 4 ) ) ;
229
271
} ) ;
230
272
}
@@ -251,7 +293,7 @@ mod tests {
251
293
fn test_err_into ( ) {
252
294
executor:: block_on ( async {
253
295
let future_err_u8 = ready ( Err :: < ( ) , u8 > ( 1 ) ) ;
254
- let future_err_i32 = err_into :: < _ , _ , _ , i32 > ( future_err_u8 ) ;
296
+ let future_err_i32 = future_err_u8 . err_into ( ) ;
255
297
256
298
assert_eq ! ( future_err_i32. await , Err :: <( ) , i32 >( 1 ) ) ;
257
299
} ) ;
@@ -261,7 +303,7 @@ mod tests {
261
303
fn test_unwrap_or_else ( ) {
262
304
executor:: block_on ( async {
263
305
let future = ready ( Err :: < ( ) , & str > ( "Boom!" ) ) ;
264
- let new_future = unwrap_or_else ( future , |_| ( ) ) ;
306
+ let new_future = future . unwrap_or_else ( |_| ( ) ) ;
265
307
assert_eq ! ( new_future. await , ( ) ) ;
266
308
} ) ;
267
309
}
0 commit comments