From 9d25ee9ac1c17c14865658ab9aabfffa8a19ee57 Mon Sep 17 00:00:00 2001 From: Marco Napetti <7566389+nappa85@users.noreply.github.com> Date: Sun, 6 Nov 2022 05:55:21 +0100 Subject: [PATCH] use GAT to elide StreamTrait lifetime (#1161) --- src/database/connection.rs | 10 ++++++---- src/database/db_connection.rs | 8 ++++---- src/database/transaction.rs | 10 ++++------ src/executor/select.rs | 10 +++++----- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/database/connection.rs b/src/database/connection.rs index 4c270c049..4919ecb25 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -34,15 +34,17 @@ pub trait ConnectionTrait: Sync { } /// Stream query results -pub trait StreamTrait<'a>: Send + Sync { +pub trait StreamTrait: Send + Sync { /// Create a stream for the [QueryResult] - type Stream: Stream> + Send; + type Stream<'a>: Stream> + Send + where + Self: 'a; /// Execute a [Statement] and return a stream of results - fn stream( + fn stream<'a>( &'a self, stmt: Statement, - ) -> Pin> + 'a + Send>>; + ) -> Pin, DbErr>> + 'a + Send>>; } /// Spawn database transaction diff --git a/src/database/db_connection.rs b/src/database/db_connection.rs index 3fa48ec0e..7fd51633b 100644 --- a/src/database/db_connection.rs +++ b/src/database/db_connection.rs @@ -165,15 +165,15 @@ impl ConnectionTrait for DatabaseConnection { } #[async_trait::async_trait] -impl<'a> StreamTrait<'a> for DatabaseConnection { - type Stream = crate::QueryStream; +impl StreamTrait for DatabaseConnection { + type Stream<'a> = crate::QueryStream; #[instrument(level = "trace")] #[allow(unused_variables, unreachable_code)] - fn stream( + fn stream<'a>( &'a self, stmt: Statement, - ) -> Pin> + 'a + Send>> { + ) -> Pin, DbErr>> + 'a + Send>> { Box::pin(async move { Ok(match self { #[cfg(feature = "sqlx-mysql")] diff --git a/src/database/transaction.rs b/src/database/transaction.rs index 953eba220..85fd304a6 100644 --- a/src/database/transaction.rs +++ b/src/database/transaction.rs @@ -388,16 +388,14 @@ impl ConnectionTrait for DatabaseTransaction { } } -#[async_trait::async_trait] -#[allow(unused_variables)] -impl<'a> StreamTrait<'a> for DatabaseTransaction { - type Stream = TransactionStream<'a>; +impl StreamTrait for DatabaseTransaction { + type Stream<'a> = TransactionStream<'a>; #[instrument(level = "trace")] - fn stream( + fn stream<'a>( &'a self, stmt: Statement, - ) -> Pin> + 'a + Send>> { + ) -> Pin, DbErr>> + 'a + Send>> { Box::pin(async move { let conn = self.conn.lock().await; Ok(crate::TransactionStream::build( diff --git a/src/executor/select.rs b/src/executor/select.rs index ed798d940..a31b2a831 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -275,7 +275,7 @@ where db: &'a C, ) -> Result> + 'b + Send, DbErr> where - C: ConnectionTrait + StreamTrait<'a> + Send, + C: ConnectionTrait + StreamTrait + Send, { self.into_model().stream(db).await } @@ -329,7 +329,7 @@ where db: &'a C, ) -> Result), DbErr>> + 'b, DbErr> where - C: ConnectionTrait + StreamTrait<'a> + Send, + C: ConnectionTrait + StreamTrait + Send, { self.into_model().stream(db).await } @@ -367,7 +367,7 @@ where db: &'a C, ) -> Result), DbErr>> + 'b + Send, DbErr> where - C: ConnectionTrait + StreamTrait<'a> + Send, + C: ConnectionTrait + StreamTrait + Send, { self.into_model().stream(db).await } @@ -453,7 +453,7 @@ where db: &'a C, ) -> Result> + 'b + Send>>, DbErr> where - C: ConnectionTrait + StreamTrait<'a> + Send, + C: ConnectionTrait + StreamTrait + Send, S: 'b, S::Item: Send, { @@ -739,7 +739,7 @@ where db: &'a C, ) -> Result> + 'b + Send>>, DbErr> where - C: ConnectionTrait + StreamTrait<'a> + Send, + C: ConnectionTrait + StreamTrait + Send, S: 'b, S::Item: Send, {