@@ -18,42 +18,37 @@ use tokio::task::spawn_blocking;
18
18
19
19
/// An async variant of [`diesel::connection::SimpleConnection`].
20
20
#[ async_trait]
21
- pub trait AsyncSimpleConnection < Conn , ConnErr >
21
+ pub trait AsyncSimpleConnection < Conn >
22
22
where
23
23
Conn : ' static + SimpleConnection ,
24
24
{
25
- async fn batch_execute_async ( & self , query : & str ) -> Result < ( ) , ConnErr > ;
25
+ async fn batch_execute_async ( & self , query : & str ) -> Result < ( ) , DieselError > ;
26
26
}
27
27
28
28
/// An async variant of [`diesel::connection::Connection`].
29
29
#[ async_trait]
30
- pub trait AsyncConnection < Conn , ConnErr > : AsyncSimpleConnection < Conn , ConnErr >
30
+ pub trait AsyncConnection < Conn > : AsyncSimpleConnection < Conn >
31
31
where
32
32
Conn : ' static + DieselConnection ,
33
- ConnErr : From < DieselError > + Send + ' static ,
34
33
Self : Send ,
35
34
{
36
35
type OwnedConnection : Sync + Send + ' static ;
37
36
38
37
#[ doc( hidden) ]
39
- async fn get_owned_connection ( & self ) -> Result < Self :: OwnedConnection , ConnErr > ;
38
+ async fn get_owned_connection ( & self ) -> Self :: OwnedConnection ;
40
39
#[ doc( hidden) ]
41
40
fn as_sync_conn ( owned : & Self :: OwnedConnection ) -> MutexGuard < ' _ , Conn > ;
42
41
#[ doc( hidden) ]
43
42
fn as_async_conn ( owned : & Self :: OwnedConnection ) -> & SingleConnection < Conn > ;
44
43
45
44
/// Runs the function `f` in an context where blocking is safe.
46
- ///
47
- /// Any error may be propagated through `f`, as long as that
48
- /// error type may be constructed from `ConnErr` (as that error
49
- /// type may also be generated).
50
45
async fn run < R , E , Func > ( & self , f : Func ) -> Result < R , E >
51
46
where
52
47
R : Send + ' static ,
53
- E : From < ConnErr > + Send + ' static ,
48
+ E : Send + ' static ,
54
49
Func : FnOnce ( & mut Conn ) -> Result < R , E > + Send + ' static ,
55
50
{
56
- let connection = self . get_owned_connection ( ) . await ? ;
51
+ let connection = self . get_owned_connection ( ) . await ;
57
52
Self :: run_with_connection ( connection, f) . await
58
53
}
59
54
64
59
) -> Result < R , E >
65
60
where
66
61
R : Send + ' static ,
67
- E : From < ConnErr > + Send + ' static ,
62
+ E : Send + ' static ,
68
63
Func : FnOnce ( & mut Conn ) -> Result < R , E > + Send + ' static ,
69
64
{
70
65
spawn_blocking ( move || f ( & mut * Self :: as_sync_conn ( & connection) ) )
79
74
) -> Result < R , E >
80
75
where
81
76
R : Send + ' static ,
82
- E : From < ConnErr > + Send + ' static ,
77
+ E : Send + ' static ,
83
78
Func : FnOnce ( & mut Conn ) -> Result < R , E > + Send + ' static ,
84
79
{
85
80
spawn_blocking ( move || f ( & mut * Self :: as_sync_conn ( & connection) ) )
90
85
async fn transaction < R , E , Func > ( & self , f : Func ) -> Result < R , E >
91
86
where
92
87
R : Send + ' static ,
93
- E : From < DieselError > + From < ConnErr > + Send + ' static ,
88
+ E : From < DieselError > + Send + ' static ,
94
89
Func : FnOnce ( & mut Conn ) -> Result < R , E > + Send + ' static ,
95
90
{
96
91
self . run ( |conn| conn. transaction ( |c| f ( c) ) ) . await
@@ -99,21 +94,21 @@ where
99
94
async fn transaction_async < R , E , Func , Fut , ' a > ( & ' a self , f : Func ) -> Result < R , E >
100
95
where
101
96
R : Send + ' static ,
102
- E : From < DieselError > + From < ConnErr > + Send ,
97
+ E : From < DieselError > + Send + ' static ,
103
98
Fut : Future < Output = Result < R , E > > + Send ,
104
99
Func : FnOnce ( SingleConnection < Conn > ) -> Fut + Send ,
105
100
{
106
101
// Check out a connection once, and use it for the duration of the
107
102
// operation.
108
- let conn = Arc :: new ( self . get_owned_connection ( ) . await ? ) ;
103
+ let conn = Arc :: new ( self . get_owned_connection ( ) . await ) ;
109
104
110
105
// This function mimics the implementation of:
111
106
// https://docs.diesel.rs/master/diesel/connection/trait.TransactionManager.html#method.transaction
112
107
//
113
108
// However, it modifies all callsites to instead issue
114
109
// known-to-be-synchronous operations from an asynchronous context.
115
110
Self :: run_with_shared_connection ( conn. clone ( ) , |conn| {
116
- Conn :: TransactionManager :: begin_transaction ( conn) . map_err ( ConnErr :: from)
111
+ Conn :: TransactionManager :: begin_transaction ( conn) . map_err ( E :: from)
117
112
} )
118
113
. await ?;
119
114
@@ -133,14 +128,14 @@ where
133
128
match f ( async_conn) . await {
134
129
Ok ( value) => {
135
130
Self :: run_with_shared_connection ( conn. clone ( ) , |conn| {
136
- Conn :: TransactionManager :: commit_transaction ( conn) . map_err ( ConnErr :: from)
131
+ Conn :: TransactionManager :: commit_transaction ( conn) . map_err ( E :: from)
137
132
} )
138
133
. await ?;
139
134
Ok ( value)
140
135
}
141
136
Err ( user_error) => {
142
137
match Self :: run_with_shared_connection ( conn. clone ( ) , |conn| {
143
- Conn :: TransactionManager :: rollback_transaction ( conn) . map_err ( ConnErr :: from)
138
+ Conn :: TransactionManager :: rollback_transaction ( conn) . map_err ( E :: from)
144
139
} )
145
140
. await
146
141
{
@@ -154,112 +149,108 @@ where
154
149
155
150
/// An async variant of [`diesel::query_dsl::RunQueryDsl`].
156
151
#[ async_trait]
157
- pub trait AsyncRunQueryDsl < Conn , AsyncConn , E >
152
+ pub trait AsyncRunQueryDsl < Conn , AsyncConn >
158
153
where
159
154
Conn : ' static + DieselConnection ,
160
155
{
161
- async fn execute_async ( self , asc : & AsyncConn ) -> Result < usize , E >
156
+ async fn execute_async ( self , asc : & AsyncConn ) -> Result < usize , DieselError >
162
157
where
163
158
Self : ExecuteDsl < Conn > ;
164
159
165
- async fn load_async < U > ( self , asc : & AsyncConn ) -> Result < Vec < U > , E >
160
+ async fn load_async < U > ( self , asc : & AsyncConn ) -> Result < Vec < U > , DieselError >
166
161
where
167
162
U : Send + ' static ,
168
163
Self : LoadQuery < ' static , Conn , U > ;
169
164
170
- async fn get_result_async < U > ( self , asc : & AsyncConn ) -> Result < U , E >
165
+ async fn get_result_async < U > ( self , asc : & AsyncConn ) -> Result < U , DieselError >
171
166
where
172
167
U : Send + ' static ,
173
168
Self : LoadQuery < ' static , Conn , U > ;
174
169
175
- async fn get_results_async < U > ( self , asc : & AsyncConn ) -> Result < Vec < U > , E >
170
+ async fn get_results_async < U > ( self , asc : & AsyncConn ) -> Result < Vec < U > , DieselError >
176
171
where
177
172
U : Send + ' static ,
178
173
Self : LoadQuery < ' static , Conn , U > ;
179
174
180
- async fn first_async < U > ( self , asc : & AsyncConn ) -> Result < U , E >
175
+ async fn first_async < U > ( self , asc : & AsyncConn ) -> Result < U , DieselError >
181
176
where
182
177
U : Send + ' static ,
183
178
Self : LimitDsl ,
184
179
Limit < Self > : LoadQuery < ' static , Conn , U > ;
185
180
}
186
181
187
182
#[ async_trait]
188
- impl < T , AsyncConn , Conn , E > AsyncRunQueryDsl < Conn , AsyncConn , E > for T
183
+ impl < T , AsyncConn , Conn > AsyncRunQueryDsl < Conn , AsyncConn > for T
189
184
where
190
185
T : ' static + Send + RunQueryDsl < Conn > ,
191
186
Conn : ' static + DieselConnection ,
192
- AsyncConn : Send + Sync + AsyncConnection < Conn , E > ,
193
- E : From < DieselError > + Send + ' static ,
187
+ AsyncConn : Send + Sync + AsyncConnection < Conn > ,
194
188
{
195
- async fn execute_async ( self , asc : & AsyncConn ) -> Result < usize , E >
189
+ async fn execute_async ( self , asc : & AsyncConn ) -> Result < usize , DieselError >
196
190
where
197
191
Self : ExecuteDsl < Conn > ,
198
192
{
199
- asc. run ( |conn| self . execute ( conn) . map_err ( E :: from ) ) . await
193
+ asc. run ( |conn| self . execute ( conn) ) . await
200
194
}
201
195
202
- async fn load_async < U > ( self , asc : & AsyncConn ) -> Result < Vec < U > , E >
196
+ async fn load_async < U > ( self , asc : & AsyncConn ) -> Result < Vec < U > , DieselError >
203
197
where
204
198
U : Send + ' static ,
205
199
Self : LoadQuery < ' static , Conn , U > ,
206
200
{
207
- asc. run ( |conn| self . load ( conn) . map_err ( E :: from ) ) . await
201
+ asc. run ( |conn| self . load ( conn) ) . await
208
202
}
209
203
210
- async fn get_result_async < U > ( self , asc : & AsyncConn ) -> Result < U , E >
204
+ async fn get_result_async < U > ( self , asc : & AsyncConn ) -> Result < U , DieselError >
211
205
where
212
206
U : Send + ' static ,
213
207
Self : LoadQuery < ' static , Conn , U > ,
214
208
{
215
- asc. run ( |conn| self . get_result ( conn) . map_err ( E :: from ) ) . await
209
+ asc. run ( |conn| self . get_result ( conn) ) . await
216
210
}
217
211
218
- async fn get_results_async < U > ( self , asc : & AsyncConn ) -> Result < Vec < U > , E >
212
+ async fn get_results_async < U > ( self , asc : & AsyncConn ) -> Result < Vec < U > , DieselError >
219
213
where
220
214
U : Send + ' static ,
221
215
Self : LoadQuery < ' static , Conn , U > ,
222
216
{
223
- asc. run ( |conn| self . get_results ( conn) . map_err ( E :: from) )
224
- . await
217
+ asc. run ( |conn| self . get_results ( conn) ) . await
225
218
}
226
219
227
- async fn first_async < U > ( self , asc : & AsyncConn ) -> Result < U , E >
220
+ async fn first_async < U > ( self , asc : & AsyncConn ) -> Result < U , DieselError >
228
221
where
229
222
U : Send + ' static ,
230
223
Self : LimitDsl ,
231
224
Limit < Self > : LoadQuery < ' static , Conn , U > ,
232
225
{
233
- asc. run ( |conn| self . first ( conn) . map_err ( E :: from ) ) . await
226
+ asc. run ( |conn| self . first ( conn) ) . await
234
227
}
235
228
}
236
229
237
230
#[ async_trait]
238
- pub trait AsyncSaveChangesDsl < Conn , AsyncConn , E >
231
+ pub trait AsyncSaveChangesDsl < Conn , AsyncConn >
239
232
where
240
233
Conn : ' static + DieselConnection ,
241
234
{
242
- async fn save_changes_async < Output > ( self , asc : & AsyncConn ) -> Result < Output , E >
235
+ async fn save_changes_async < Output > ( self , asc : & AsyncConn ) -> Result < Output , DieselError >
243
236
where
244
237
Self : Sized ,
245
238
Conn : diesel:: query_dsl:: UpdateAndFetchResults < Self , Output > ,
246
239
Output : Send + ' static ;
247
240
}
248
241
249
242
#[ async_trait]
250
- impl < T , AsyncConn , Conn , E > AsyncSaveChangesDsl < Conn , AsyncConn , E > for T
243
+ impl < T , AsyncConn , Conn > AsyncSaveChangesDsl < Conn , AsyncConn > for T
251
244
where
252
245
T : ' static + Send + Sync + diesel:: SaveChangesDsl < Conn > ,
253
246
Conn : ' static + DieselConnection ,
254
- AsyncConn : Send + Sync + AsyncConnection < Conn , E > ,
255
- E : ' static + Send + From < DieselError > ,
247
+ AsyncConn : Send + Sync + AsyncConnection < Conn > ,
256
248
{
257
- async fn save_changes_async < Output > ( self , asc : & AsyncConn ) -> Result < Output , E >
249
+ async fn save_changes_async < Output > ( self , asc : & AsyncConn ) -> Result < Output , DieselError >
258
250
where
259
251
Conn : diesel:: query_dsl:: UpdateAndFetchResults < Self , Output > ,
260
252
Output : Send + ' static ,
261
253
{
262
- asc. run ( |conn| self . save_changes ( conn) . map_err ( E :: from) )
263
- . await
254
+ asc. run ( |conn| self . save_changes ( conn) ) . await
264
255
}
265
256
}
0 commit comments