@@ -45,51 +45,17 @@ pub trait FutureExt: Future {
45
45
}
46
46
}
47
47
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 ;
78
- }
48
+ impl < T , E , Fut : ?Sized > TryFutureExt < T , E > for Fut where Fut : Future < Output = Result < T , E > > { }
79
49
80
50
#[ 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 ,
85
- {
86
- type Ok = T ;
87
- type Error = E ;
51
+ pub trait TryFutureExt < T , E > : Future < Output = Result < T , E > > {
88
52
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
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 ,
93
59
{
94
60
match self . await {
95
61
Ok ( ok) => {
@@ -100,10 +66,12 @@ impl<T, E, Fut> TryFuture for Fut
100
66
}
101
67
}
102
68
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 ,
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 ,
106
72
Self : Sized ,
73
+ T : Send + ' async_trait ,
74
+ E : Send + ' async_trait ,
107
75
{
108
76
match self . await {
109
77
Ok ( ok) => Ok ( ok) ,
@@ -114,30 +82,38 @@ impl<T, E, Fut> TryFuture for Fut
114
82
}
115
83
}
116
84
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
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 ,
120
90
{
121
91
self . await . map ( f)
122
92
}
123
93
124
- async fn map_err < U , F > ( self , f : F ) -> Result < Self :: Ok , U >
125
- where F : FnOnce ( Self :: Error ) -> U + Send ,
94
+ async fn map_err < U , F > ( self , f : F ) -> Result < T , U >
95
+ where F : FnOnce ( E ) -> U + Send ,
126
96
Self : Sized ,
97
+ T : Send + ' async_trait ,
98
+ E : Send + ' async_trait ,
127
99
{
128
100
self . await . map_err ( f)
129
101
}
130
102
131
- async fn err_into < U > ( self ) -> Result < Self :: Ok , U >
132
- where Self :: Error : Into < U > ,
103
+ async fn err_into < U > ( self ) -> Result < T , U >
104
+ where E : Into < U > ,
133
105
Self : Sized ,
106
+ T : Send + ' async_trait ,
107
+ E : Send + ' async_trait ,
134
108
{
135
109
self . await . map_err ( Into :: into)
136
110
}
137
111
138
- async fn unwrap_or_else < F > ( self , f : F ) -> Self :: Ok
139
- where F : FnOnce ( Self :: Error ) -> Self :: Ok + Send ,
112
+ async fn unwrap_or_else < F > ( self , f : F ) -> T
113
+ where F : FnOnce ( E ) -> T + Send ,
140
114
Self : Sized ,
115
+ T : Send + ' async_trait ,
116
+ E : Send + ' async_trait ,
141
117
{
142
118
self . await . unwrap_or_else ( f)
143
119
}
0 commit comments