Skip to content

fix: avoid where Self: 'a clause on GATs #106

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 4 commits into from
Sep 8, 2023
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
26 changes: 14 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,13 @@ pub trait SimpleAsyncConnection {
#[async_trait::async_trait]
pub trait AsyncConnection: SimpleAsyncConnection + Sized + Send {
/// The future returned by `AsyncConnection::execute`
type ExecuteFuture<'conn, 'query>: Future<Output = QueryResult<usize>> + Send
where
Self: 'conn;
type ExecuteFuture<'conn, 'query>: Future<Output = QueryResult<usize>> + Send;
/// The future returned by `AsyncConnection::load`
type LoadFuture<'conn, 'query>: Future<Output = QueryResult<Self::Stream<'conn, 'query>>> + Send
where
Self: 'conn;
type LoadFuture<'conn, 'query>: Future<Output = QueryResult<Self::Stream<'conn, 'query>>> + Send;
/// The inner stream returned by `AsyncConnection::load`
type Stream<'conn, 'query>: Stream<Item = QueryResult<Self::Row<'conn, 'query>>> + Send
where
Self: 'conn;
type Stream<'conn, 'query>: Stream<Item = QueryResult<Self::Row<'conn, 'query>>> + Send;
/// The row type used by the stream returned by `AsyncConnection::load`
type Row<'conn, 'query>: Row<'conn, Self::Backend>
where
Self: 'conn;
type Row<'conn, 'query>: Row<'conn, Self::Backend>;

/// The backend this type connects to
type Backend: Backend;
Expand Down Expand Up @@ -341,4 +333,14 @@ pub trait AsyncConnection: SimpleAsyncConnection + Sized + Send {
fn transaction_state(
&mut self,
) -> &mut <Self::TransactionManager as TransactionManager<Self>>::TransactionStateData;

// These functions allow the associated types (`ExecuteFuture`, `LoadFuture`, etc.) to
// compile without a `where Self: '_` clause. This is needed the because bound causes
// lifetime issues when using `transaction()` with generic `AsyncConnection`s.
//
// See: https://github.com/rust-lang/rust/issues/87479
#[doc(hidden)]
fn _silence_lint_on_execute_future(_: Self::ExecuteFuture<'_, '_>) {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment to these function explaining why they exist + linking to the relevant rust issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment and link added

#[doc(hidden)]
fn _silence_lint_on_load_future(_: Self::LoadFuture<'_, '_>) {}
}
12 changes: 4 additions & 8 deletions src/pooled_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,10 @@ where
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;
<C::Target as AsyncConnection>::ExecuteFuture<'conn, 'query>;
type LoadFuture<'conn, 'query> = <C::Target as AsyncConnection>::LoadFuture<'conn, 'query>;
type Stream<'conn, 'query> = <C::Target as AsyncConnection>::Stream<'conn, 'query>;
type Row<'conn, 'query> = <C::Target as AsyncConnection>::Row<'conn, 'query>;

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

Expand Down
4 changes: 3 additions & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ mod pooling;
mod sync_wrapper;
mod type_check;

async fn transaction_test(conn: &mut TestConnection) -> QueryResult<()> {
async fn transaction_test<C: AsyncConnection<Backend = TestBackend>>(
conn: &mut C,
) -> QueryResult<()> {
let res = conn
.transaction::<i32, diesel::result::Error, _>(|conn| {
Box::pin(async move {
Expand Down