Skip to content

Commit 5717b2f

Browse files
authored
Merge pull request #41 from weiznich/use_gats
Remove GAT workarounds
2 parents 7a33c3c + 69ff544 commit 5717b2f

File tree

8 files changed

+138
-232
lines changed

8 files changed

+138
-232
lines changed

src/lib.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,30 @@ pub trait SimpleAsyncConnection {
111111
async fn batch_execute(&mut self, query: &str) -> QueryResult<()>;
112112
}
113113

114-
/// This trait is a workaround to emulate GAT on stable rust
115-
///
116-
/// It is used to specify the return type of `AsyncConnection::load`
117-
/// and `AsyncConnection::execute` which may contain lifetimes
118-
pub trait AsyncConnectionGatWorkaround<'conn, 'query, DB: Backend> {
119-
/// The future returned by `AsyncConnection::execute`
120-
type ExecuteFuture: Future<Output = QueryResult<usize>> + Send;
121-
/// The future returned by `AsyncConnection::load`
122-
type LoadFuture: Future<Output = QueryResult<Self::Stream>> + Send;
123-
/// The inner stream returned by `AsyncConnection::load`
124-
type Stream: Stream<Item = QueryResult<Self::Row>> + Send;
125-
/// The row type used by the stream returned by `AsyncConnection::load`
126-
type Row: Row<'conn, DB>;
127-
}
128-
129114
/// An async connection to a database
130115
///
131116
/// This trait represents a n async database connection. It can be used to query the database through
132117
/// the query dsl provided by diesel, custom extensions or raw sql queries. It essentially mirrors
133118
/// the sync diesel [`Connection`](diesel::connection::Connection) implementation
134119
#[async_trait::async_trait]
135-
pub trait AsyncConnection: SimpleAsyncConnection + Sized + Send
136-
where
137-
for<'a, 'b> Self: AsyncConnectionGatWorkaround<'a, 'b, Self::Backend>,
138-
{
120+
pub trait AsyncConnection: SimpleAsyncConnection + Sized + Send {
121+
/// The future returned by `AsyncConnection::execute`
122+
type ExecuteFuture<'conn, 'query>: Future<Output = QueryResult<usize>> + Send
123+
where
124+
Self: 'conn;
125+
/// The future returned by `AsyncConnection::load`
126+
type LoadFuture<'conn, 'query>: Future<Output = QueryResult<Self::Stream<'conn, 'query>>> + Send
127+
where
128+
Self: 'conn;
129+
/// The inner stream returned by `AsyncConnection::load`
130+
type Stream<'conn, 'query>: Stream<Item = QueryResult<Self::Row<'conn, 'query>>> + Send
131+
where
132+
Self: 'conn;
133+
/// The row type used by the stream returned by `AsyncConnection::load`
134+
type Row<'conn, 'query>: Row<'conn, Self::Backend>
135+
where
136+
Self: 'conn;
137+
139138
/// The backend this type connects to
140139
type Backend: Backend;
141140

@@ -256,10 +255,7 @@ where
256255
}
257256

258257
#[doc(hidden)]
259-
fn load<'conn, 'query, T>(
260-
&'conn mut self,
261-
source: T,
262-
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::LoadFuture
258+
fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query>
263259
where
264260
T: AsQuery + Send + 'query,
265261
T::Query: QueryFragment<Self::Backend> + QueryId + Send + 'query;
@@ -268,7 +264,7 @@ where
268264
fn execute_returning_count<'conn, 'query, T>(
269265
&'conn mut self,
270266
source: T,
271-
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::ExecuteFuture
267+
) -> Self::ExecuteFuture<'conn, 'query>
272268
where
273269
T: QueryFragment<Self::Backend> + QueryId + Send + 'query;
274270

src/mysql/mod.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::stmt_cache::{PrepareCallback, StmtCache};
2-
use crate::{
3-
AnsiTransactionManager, AsyncConnection, AsyncConnectionGatWorkaround, SimpleAsyncConnection,
4-
};
2+
use crate::{AnsiTransactionManager, AsyncConnection, SimpleAsyncConnection};
53
use diesel::connection::statement_cache::MaybeCached;
64
use diesel::mysql::{Mysql, MysqlType};
75
use diesel::query_builder::{bind_collector::RawBytesBindCollector, QueryFragment, QueryId};
@@ -36,16 +34,7 @@ impl SimpleAsyncConnection for AsyncMysqlConnection {
3634
}
3735
}
3836

39-
impl<'conn, 'query> AsyncConnectionGatWorkaround<'conn, 'query, Mysql> for AsyncMysqlConnection {
40-
type ExecuteFuture = BoxFuture<'conn, QueryResult<usize>>;
41-
type LoadFuture = BoxFuture<'conn, QueryResult<Self::Stream>>;
42-
type Stream = BoxStream<'conn, QueryResult<Self::Row>>;
43-
44-
type Row = MysqlRow;
45-
}
46-
47-
const CONNECTION_SETUP_QUERIES: &'static [&'static str] = &[
48-
"SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT'))",
37+
const CONNECTION_SETUP_QUERIES: &[&str] = &[
4938
"SET time_zone = '+00:00';",
5039
"SET character_set_client = 'utf8mb4'",
5140
"SET character_set_connection = 'utf8mb4'",
@@ -54,6 +43,10 @@ const CONNECTION_SETUP_QUERIES: &'static [&'static str] = &[
5443

5544
#[async_trait::async_trait]
5645
impl AsyncConnection for AsyncMysqlConnection {
46+
type ExecuteFuture<'conn, 'query> = BoxFuture<'conn, QueryResult<usize>>;
47+
type LoadFuture<'conn, 'query> = BoxFuture<'conn, QueryResult<Self::Stream<'conn, 'query>>>;
48+
type Stream<'conn, 'query> = BoxStream<'conn, QueryResult<Self::Row<'conn, 'query>>>;
49+
type Row<'conn, 'query> = MysqlRow;
5750
type Backend = Mysql;
5851

5952
type TransactionManager = AnsiTransactionManager;
@@ -74,10 +67,7 @@ impl AsyncConnection for AsyncMysqlConnection {
7467
})
7568
}
7669

77-
fn load<'conn, 'query, T>(
78-
&'conn mut self,
79-
source: T,
80-
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::LoadFuture
70+
fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query>
8171
where
8272
T: diesel::query_builder::AsQuery + Send,
8373
T::Query: diesel::query_builder::QueryFragment<Self::Backend>
@@ -133,7 +123,7 @@ impl AsyncConnection for AsyncMysqlConnection {
133123
fn execute_returning_count<'conn, 'query, T>(
134124
&'conn mut self,
135125
source: T,
136-
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::ExecuteFuture
126+
) -> Self::ExecuteFuture<'conn, 'query>
137127
where
138128
T: diesel::query_builder::QueryFragment<Self::Backend>
139129
+ diesel::query_builder::QueryId
@@ -248,11 +238,10 @@ impl AsyncMysqlConnection {
248238
let stmt = stmt_cache.cached_prepared_statement(query, &metadata, conn, &Mysql);
249239

250240
stmt.and_then(|(stmt, conn)| async move {
251-
let res = update_transaction_manager_status(
241+
update_transaction_manager_status(
252242
callback(conn, stmt, ToSqlHelper { metadata, binds }).await,
253243
transaction_manager,
254-
);
255-
res
244+
)
256245
})
257246
.boxed()
258247
}

src/pg/mod.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use self::error_helper::ErrorHelper;
88
use self::row::PgRow;
99
use self::serialize::ToSqlHelper;
1010
use crate::stmt_cache::{PrepareCallback, StmtCache};
11-
use crate::{
12-
AnsiTransactionManager, AsyncConnection, AsyncConnectionGatWorkaround, SimpleAsyncConnection,
13-
};
11+
use crate::{AnsiTransactionManager, AsyncConnection, SimpleAsyncConnection};
1412
use diesel::connection::statement_cache::PrepareForCache;
1513
use diesel::pg::{
1614
FailedToLookupTypeError, PgMetadataCache, PgMetadataCacheKey, PgMetadataLookup, PgTypeMetadata,
@@ -110,18 +108,12 @@ impl SimpleAsyncConnection for AsyncPgConnection {
110108
}
111109
}
112110

113-
impl<'conn, 'query> AsyncConnectionGatWorkaround<'conn, 'query, diesel::pg::Pg>
114-
for AsyncPgConnection
115-
{
116-
type LoadFuture = BoxFuture<'query, QueryResult<Self::Stream>>;
117-
type ExecuteFuture = BoxFuture<'query, QueryResult<usize>>;
118-
type Stream = BoxStream<'static, QueryResult<PgRow>>;
119-
120-
type Row = PgRow;
121-
}
122-
123111
#[async_trait::async_trait]
124112
impl AsyncConnection for AsyncPgConnection {
113+
type LoadFuture<'conn, 'query> = BoxFuture<'query, QueryResult<Self::Stream<'conn, 'query>>>;
114+
type ExecuteFuture<'conn, 'query> = BoxFuture<'query, QueryResult<usize>>;
115+
type Stream<'conn, 'query> = BoxStream<'static, QueryResult<PgRow>>;
116+
type Row<'conn, 'query> = PgRow;
125117
type Backend = diesel::pg::Pg;
126118
type TransactionManager = AnsiTransactionManager;
127119

@@ -137,10 +129,7 @@ impl AsyncConnection for AsyncPgConnection {
137129
Self::try_from(client).await
138130
}
139131

140-
fn load<'conn, 'query, T>(
141-
&'conn mut self,
142-
source: T,
143-
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::LoadFuture
132+
fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query>
144133
where
145134
T: AsQuery + Send + 'query,
146135
T::Query: QueryFragment<Self::Backend> + QueryId + Send + 'query,
@@ -171,7 +160,7 @@ impl AsyncConnection for AsyncPgConnection {
171160
fn execute_returning_count<'conn, 'query, T>(
172161
&'conn mut self,
173162
source: T,
174-
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::ExecuteFuture
163+
) -> Self::ExecuteFuture<'conn, 'query>
175164
where
176165
T: QueryFragment<Self::Backend> + QueryId + Send + 'query,
177166
{
@@ -415,7 +404,7 @@ impl AsyncPgConnection {
415404
.collect::<Vec<_>>();
416405
let res = callback(raw_connection, stmt.clone(), binds).await;
417406
let mut tm = tm.lock().await;
418-
update_transaction_manager_status(res, &mut *tm)
407+
update_transaction_manager_status(res, &mut tm)
419408
}
420409
}
421410

src/pooled_connection/deadpool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub type HookErrorCause = deadpool::managed::HookErrorCause<super::PoolError>;
6363
#[async_trait::async_trait]
6464
impl<C> Manager for AsyncDieselConnectionManager<C>
6565
where
66-
C: PoolableConnection + Send,
66+
C: PoolableConnection + Send + 'static,
6767
{
6868
type Type = C;
6969

src/pooled_connection/mod.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! * [mobc](self::mobc)
88
99
use crate::TransactionManager;
10-
use crate::{AsyncConnection, AsyncConnectionGatWorkaround, SimpleAsyncConnection};
10+
use crate::{AsyncConnection, SimpleAsyncConnection};
1111
use std::fmt;
1212
use std::marker::PhantomData;
1313
use std::ops::DerefMut;
@@ -65,19 +65,6 @@ impl<C> AsyncDieselConnectionManager<C> {
6565
}
6666
}
6767

68-
impl<'conn, 'query, C, DB> AsyncConnectionGatWorkaround<'conn, 'query, DB> for C
69-
where
70-
DB: diesel::backend::Backend,
71-
C: DerefMut,
72-
C::Target: AsyncConnectionGatWorkaround<'conn, 'query, DB>,
73-
{
74-
type ExecuteFuture =
75-
<C::Target as AsyncConnectionGatWorkaround<'conn, 'query, DB>>::ExecuteFuture;
76-
type LoadFuture = <C::Target as AsyncConnectionGatWorkaround<'conn, 'query, DB>>::LoadFuture;
77-
type Stream = <C::Target as AsyncConnectionGatWorkaround<'conn, 'query, DB>>::Stream;
78-
type Row = <C::Target as AsyncConnectionGatWorkaround<'conn, 'query, DB>>::Row;
79-
}
80-
8168
#[async_trait::async_trait]
8269
impl<C> SimpleAsyncConnection for C
8370
where
@@ -96,6 +83,16 @@ where
9683
C: DerefMut + Send,
9784
C::Target: AsyncConnection,
9885
{
86+
type ExecuteFuture<'conn, 'query> =
87+
<C::Target as AsyncConnection>::ExecuteFuture<'conn, 'query>
88+
where C::Target: 'conn, C: 'conn;
89+
type LoadFuture<'conn, 'query> = <C::Target as AsyncConnection>::LoadFuture<'conn, 'query>
90+
where C::Target: 'conn, C: 'conn;
91+
type Stream<'conn, 'query> = <C::Target as AsyncConnection>::Stream<'conn, 'query>
92+
where C::Target: 'conn, C: 'conn;
93+
type Row<'conn, 'query> = <C::Target as AsyncConnection>::Row<'conn, 'query>
94+
where C::Target: 'conn, C: 'conn;
95+
9996
type Backend = <C::Target as AsyncConnection>::Backend;
10097

10198
type TransactionManager =
@@ -107,10 +104,7 @@ where
107104
))
108105
}
109106

110-
fn load<'conn, 'query, T>(
111-
&'conn mut self,
112-
source: T,
113-
) -> <Self as crate::AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::LoadFuture
107+
fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query>
114108
where
115109
T: diesel::query_builder::AsQuery + Send + 'query,
116110
T::Query: diesel::query_builder::QueryFragment<Self::Backend>
@@ -125,7 +119,7 @@ where
125119
fn execute_returning_count<'conn, 'query, T>(
126120
&'conn mut self,
127121
source: T,
128-
) -> <Self as crate::AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::ExecuteFuture
122+
) -> Self::ExecuteFuture<'conn, 'query>
129123
where
130124
T: diesel::query_builder::QueryFragment<Self::Backend>
131125
+ diesel::query_builder::QueryId
@@ -208,7 +202,10 @@ pub trait PoolableConnection: AsyncConnection {
208202
/// Check if a connection is still valid
209203
///
210204
/// The default implementation performs a `SELECT 1` query
211-
async fn ping(&mut self) -> diesel::QueryResult<()> {
205+
async fn ping(&mut self) -> diesel::QueryResult<()>
206+
where
207+
for<'a> Self: 'a,
208+
{
212209
use crate::RunQueryDsl;
213210
CheckConnectionQuery.execute(self).await.map(|_| ())
214211
}

0 commit comments

Comments
 (0)