Skip to content

Remove GAT workarounds #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,31 +111,30 @@ pub trait SimpleAsyncConnection {
async fn batch_execute(&mut self, query: &str) -> QueryResult<()>;
}

/// This trait is a workaround to emulate GAT on stable rust
///
/// It is used to specify the return type of `AsyncConnection::load`
/// and `AsyncConnection::execute` which may contain lifetimes
pub trait AsyncConnectionGatWorkaround<'conn, 'query, DB: Backend> {
/// The future returned by `AsyncConnection::execute`
type ExecuteFuture: Future<Output = QueryResult<usize>> + Send;
/// The future returned by `AsyncConnection::load`
type LoadFuture: Future<Output = QueryResult<Self::Stream>> + Send;
/// The inner stream returned by `AsyncConnection::load`
type Stream: Stream<Item = QueryResult<Self::Row>> + Send;
/// The row type used by the stream returned by `AsyncConnection::load`
type Row: Row<'conn, DB>;
}

/// An async connection to a database
///
/// This trait represents a n async database connection. It can be used to query the database through
/// the query dsl provided by diesel, custom extensions or raw sql queries. It essentially mirrors
/// the sync diesel [`Connection`](diesel::connection::Connection) implementation
#[async_trait::async_trait]
pub trait AsyncConnection: SimpleAsyncConnection + Sized + Send
where
for<'a, 'b> Self: AsyncConnectionGatWorkaround<'a, 'b, Self::Backend>,
{
pub trait AsyncConnection: SimpleAsyncConnection + Sized + Send {
/// The future returned by `AsyncConnection::execute`
type ExecuteFuture<'conn, 'query>: Future<Output = QueryResult<usize>> + Send
where
Self: 'conn;
/// The future returned by `AsyncConnection::load`
type LoadFuture<'conn, 'query>: Future<Output = QueryResult<Self::Stream<'conn, 'query>>> + Send
where
Self: 'conn;
/// The inner stream returned by `AsyncConnection::load`
type Stream<'conn, 'query>: Stream<Item = QueryResult<Self::Row<'conn, 'query>>> + Send
where
Self: 'conn;
/// The row type used by the stream returned by `AsyncConnection::load`
type Row<'conn, 'query>: Row<'conn, Self::Backend>
where
Self: 'conn;

/// The backend this type connects to
type Backend: Backend;

Expand Down Expand Up @@ -256,10 +255,7 @@ where
}

#[doc(hidden)]
fn load<'conn, 'query, T>(
&'conn mut self,
source: T,
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::LoadFuture
fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query>
where
T: AsQuery + Send + 'query,
T::Query: QueryFragment<Self::Backend> + QueryId + Send + 'query;
Expand All @@ -268,7 +264,7 @@ where
fn execute_returning_count<'conn, 'query, T>(
&'conn mut self,
source: T,
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::ExecuteFuture
) -> Self::ExecuteFuture<'conn, 'query>
where
T: QueryFragment<Self::Backend> + QueryId + Send + 'query;

Expand Down
31 changes: 10 additions & 21 deletions src/mysql/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::stmt_cache::{PrepareCallback, StmtCache};
use crate::{
AnsiTransactionManager, AsyncConnection, AsyncConnectionGatWorkaround, SimpleAsyncConnection,
};
use crate::{AnsiTransactionManager, AsyncConnection, SimpleAsyncConnection};
use diesel::connection::statement_cache::MaybeCached;
use diesel::mysql::{Mysql, MysqlType};
use diesel::query_builder::{bind_collector::RawBytesBindCollector, QueryFragment, QueryId};
Expand Down Expand Up @@ -36,16 +34,7 @@ impl SimpleAsyncConnection for AsyncMysqlConnection {
}
}

impl<'conn, 'query> AsyncConnectionGatWorkaround<'conn, 'query, Mysql> for AsyncMysqlConnection {
type ExecuteFuture = BoxFuture<'conn, QueryResult<usize>>;
type LoadFuture = BoxFuture<'conn, QueryResult<Self::Stream>>;
type Stream = BoxStream<'conn, QueryResult<Self::Row>>;

type Row = MysqlRow;
}

const CONNECTION_SETUP_QUERIES: &'static [&'static str] = &[
"SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT'))",
const CONNECTION_SETUP_QUERIES: &[&str] = &[
"SET time_zone = '+00:00';",
"SET character_set_client = 'utf8mb4'",
"SET character_set_connection = 'utf8mb4'",
Expand All @@ -54,6 +43,10 @@ const CONNECTION_SETUP_QUERIES: &'static [&'static str] = &[

#[async_trait::async_trait]
impl AsyncConnection for AsyncMysqlConnection {
type ExecuteFuture<'conn, 'query> = BoxFuture<'conn, QueryResult<usize>>;
type LoadFuture<'conn, 'query> = BoxFuture<'conn, QueryResult<Self::Stream<'conn, 'query>>>;
type Stream<'conn, 'query> = BoxStream<'conn, QueryResult<Self::Row<'conn, 'query>>>;
type Row<'conn, 'query> = MysqlRow;
type Backend = Mysql;

type TransactionManager = AnsiTransactionManager;
Expand All @@ -74,10 +67,7 @@ impl AsyncConnection for AsyncMysqlConnection {
})
}

fn load<'conn, 'query, T>(
&'conn mut self,
source: T,
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::LoadFuture
fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query>
where
T: diesel::query_builder::AsQuery + Send,
T::Query: diesel::query_builder::QueryFragment<Self::Backend>
Expand Down Expand Up @@ -133,7 +123,7 @@ impl AsyncConnection for AsyncMysqlConnection {
fn execute_returning_count<'conn, 'query, T>(
&'conn mut self,
source: T,
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::ExecuteFuture
) -> Self::ExecuteFuture<'conn, 'query>
where
T: diesel::query_builder::QueryFragment<Self::Backend>
+ diesel::query_builder::QueryId
Expand Down Expand Up @@ -248,11 +238,10 @@ impl AsyncMysqlConnection {
let stmt = stmt_cache.cached_prepared_statement(query, &metadata, conn, &Mysql);

stmt.and_then(|(stmt, conn)| async move {
let res = update_transaction_manager_status(
update_transaction_manager_status(
callback(conn, stmt, ToSqlHelper { metadata, binds }).await,
transaction_manager,
);
res
)
})
.boxed()
}
Expand Down
27 changes: 8 additions & 19 deletions src/pg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use self::error_helper::ErrorHelper;
use self::row::PgRow;
use self::serialize::ToSqlHelper;
use crate::stmt_cache::{PrepareCallback, StmtCache};
use crate::{
AnsiTransactionManager, AsyncConnection, AsyncConnectionGatWorkaround, SimpleAsyncConnection,
};
use crate::{AnsiTransactionManager, AsyncConnection, SimpleAsyncConnection};
use diesel::connection::statement_cache::PrepareForCache;
use diesel::pg::{
FailedToLookupTypeError, PgMetadataCache, PgMetadataCacheKey, PgMetadataLookup, PgTypeMetadata,
Expand Down Expand Up @@ -110,18 +108,12 @@ impl SimpleAsyncConnection for AsyncPgConnection {
}
}

impl<'conn, 'query> AsyncConnectionGatWorkaround<'conn, 'query, diesel::pg::Pg>
for AsyncPgConnection
{
type LoadFuture = BoxFuture<'query, QueryResult<Self::Stream>>;
type ExecuteFuture = BoxFuture<'query, QueryResult<usize>>;
type Stream = BoxStream<'static, QueryResult<PgRow>>;

type Row = PgRow;
}

#[async_trait::async_trait]
impl AsyncConnection for AsyncPgConnection {
type LoadFuture<'conn, 'query> = BoxFuture<'query, QueryResult<Self::Stream<'conn, 'query>>>;
type ExecuteFuture<'conn, 'query> = BoxFuture<'query, QueryResult<usize>>;
type Stream<'conn, 'query> = BoxStream<'static, QueryResult<PgRow>>;
type Row<'conn, 'query> = PgRow;
type Backend = diesel::pg::Pg;
type TransactionManager = AnsiTransactionManager;

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

fn load<'conn, 'query, T>(
&'conn mut self,
source: T,
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::LoadFuture
fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query>
where
T: AsQuery + Send + 'query,
T::Query: QueryFragment<Self::Backend> + QueryId + Send + 'query,
Expand Down Expand Up @@ -171,7 +160,7 @@ impl AsyncConnection for AsyncPgConnection {
fn execute_returning_count<'conn, 'query, T>(
&'conn mut self,
source: T,
) -> <Self as AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::ExecuteFuture
) -> Self::ExecuteFuture<'conn, 'query>
where
T: QueryFragment<Self::Backend> + QueryId + Send + 'query,
{
Expand Down Expand Up @@ -415,7 +404,7 @@ impl AsyncPgConnection {
.collect::<Vec<_>>();
let res = callback(raw_connection, stmt.clone(), binds).await;
let mut tm = tm.lock().await;
update_transaction_manager_status(res, &mut *tm)
update_transaction_manager_status(res, &mut tm)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pooled_connection/deadpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub type HookErrorCause = deadpool::managed::HookErrorCause<super::PoolError>;
#[async_trait::async_trait]
impl<C> Manager for AsyncDieselConnectionManager<C>
where
C: PoolableConnection + Send,
C: PoolableConnection + Send + 'static,
{
type Type = C;

Expand Down
37 changes: 17 additions & 20 deletions src/pooled_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! * [mobc](self::mobc)

use crate::TransactionManager;
use crate::{AsyncConnection, AsyncConnectionGatWorkaround, SimpleAsyncConnection};
use crate::{AsyncConnection, SimpleAsyncConnection};
use std::fmt;
use std::marker::PhantomData;
use std::ops::DerefMut;
Expand Down Expand Up @@ -65,19 +65,6 @@ impl<C> AsyncDieselConnectionManager<C> {
}
}

impl<'conn, 'query, C, DB> AsyncConnectionGatWorkaround<'conn, 'query, DB> for C
where
DB: diesel::backend::Backend,
C: DerefMut,
C::Target: AsyncConnectionGatWorkaround<'conn, 'query, DB>,
{
type ExecuteFuture =
<C::Target as AsyncConnectionGatWorkaround<'conn, 'query, DB>>::ExecuteFuture;
type LoadFuture = <C::Target as AsyncConnectionGatWorkaround<'conn, 'query, DB>>::LoadFuture;
type Stream = <C::Target as AsyncConnectionGatWorkaround<'conn, 'query, DB>>::Stream;
type Row = <C::Target as AsyncConnectionGatWorkaround<'conn, 'query, DB>>::Row;
}

#[async_trait::async_trait]
impl<C> SimpleAsyncConnection for C
where
Expand All @@ -96,6 +83,16 @@ where
C: DerefMut + Send,
C::Target: AsyncConnection,
{
type ExecuteFuture<'conn, 'query> =
<C::Target as AsyncConnection>::ExecuteFuture<'conn, 'query>
where C::Target: 'conn, C: 'conn;
type LoadFuture<'conn, 'query> = <C::Target as AsyncConnection>::LoadFuture<'conn, 'query>
where C::Target: 'conn, C: 'conn;
type Stream<'conn, 'query> = <C::Target as AsyncConnection>::Stream<'conn, 'query>
where C::Target: 'conn, C: 'conn;
type Row<'conn, 'query> = <C::Target as AsyncConnection>::Row<'conn, 'query>
where C::Target: 'conn, C: 'conn;

type Backend = <C::Target as AsyncConnection>::Backend;

type TransactionManager =
Expand All @@ -107,10 +104,7 @@ where
))
}

fn load<'conn, 'query, T>(
&'conn mut self,
source: T,
) -> <Self as crate::AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::LoadFuture
fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query>
where
T: diesel::query_builder::AsQuery + Send + 'query,
T::Query: diesel::query_builder::QueryFragment<Self::Backend>
Expand All @@ -125,7 +119,7 @@ where
fn execute_returning_count<'conn, 'query, T>(
&'conn mut self,
source: T,
) -> <Self as crate::AsyncConnectionGatWorkaround<'conn, 'query, Self::Backend>>::ExecuteFuture
) -> Self::ExecuteFuture<'conn, 'query>
where
T: diesel::query_builder::QueryFragment<Self::Backend>
+ diesel::query_builder::QueryId
Expand Down Expand Up @@ -208,7 +202,10 @@ pub trait PoolableConnection: AsyncConnection {
/// Check if a connection is still valid
///
/// The default implementation performs a `SELECT 1` query
async fn ping(&mut self) -> diesel::QueryResult<()> {
async fn ping(&mut self) -> diesel::QueryResult<()>
where
for<'a> Self: 'a,
{
use crate::RunQueryDsl;
CheckConnectionQuery.execute(self).await.map(|_| ())
}
Expand Down
Loading