use GAT to elide StreamTrait lifetime (#1161)

This commit is contained in:
Marco Napetti 2022-11-06 05:55:21 +01:00 committed by GitHub
parent 9952aa6740
commit 9d25ee9ac1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 19 deletions

View File

@ -34,15 +34,17 @@ pub trait ConnectionTrait: Sync {
} }
/// Stream query results /// Stream query results
pub trait StreamTrait<'a>: Send + Sync { pub trait StreamTrait: Send + Sync {
/// Create a stream for the [QueryResult] /// Create a stream for the [QueryResult]
type Stream: Stream<Item = Result<QueryResult, DbErr>> + Send; type Stream<'a>: Stream<Item = Result<QueryResult, DbErr>> + Send
where
Self: 'a;
/// Execute a [Statement] and return a stream of results /// Execute a [Statement] and return a stream of results
fn stream( fn stream<'a>(
&'a self, &'a self,
stmt: Statement, stmt: Statement,
) -> Pin<Box<dyn Future<Output = Result<Self::Stream, DbErr>> + 'a + Send>>; ) -> Pin<Box<dyn Future<Output = Result<Self::Stream<'a>, DbErr>> + 'a + Send>>;
} }
/// Spawn database transaction /// Spawn database transaction

View File

@ -165,15 +165,15 @@ impl ConnectionTrait for DatabaseConnection {
} }
#[async_trait::async_trait] #[async_trait::async_trait]
impl<'a> StreamTrait<'a> for DatabaseConnection { impl StreamTrait for DatabaseConnection {
type Stream = crate::QueryStream; type Stream<'a> = crate::QueryStream;
#[instrument(level = "trace")] #[instrument(level = "trace")]
#[allow(unused_variables, unreachable_code)] #[allow(unused_variables, unreachable_code)]
fn stream( fn stream<'a>(
&'a self, &'a self,
stmt: Statement, stmt: Statement,
) -> Pin<Box<dyn Future<Output = Result<Self::Stream, DbErr>> + 'a + Send>> { ) -> Pin<Box<dyn Future<Output = Result<Self::Stream<'a>, DbErr>> + 'a + Send>> {
Box::pin(async move { Box::pin(async move {
Ok(match self { Ok(match self {
#[cfg(feature = "sqlx-mysql")] #[cfg(feature = "sqlx-mysql")]

View File

@ -388,16 +388,14 @@ impl ConnectionTrait for DatabaseTransaction {
} }
} }
#[async_trait::async_trait] impl StreamTrait for DatabaseTransaction {
#[allow(unused_variables)] type Stream<'a> = TransactionStream<'a>;
impl<'a> StreamTrait<'a> for DatabaseTransaction {
type Stream = TransactionStream<'a>;
#[instrument(level = "trace")] #[instrument(level = "trace")]
fn stream( fn stream<'a>(
&'a self, &'a self,
stmt: Statement, stmt: Statement,
) -> Pin<Box<dyn Future<Output = Result<Self::Stream, DbErr>> + 'a + Send>> { ) -> Pin<Box<dyn Future<Output = Result<Self::Stream<'a>, DbErr>> + 'a + Send>> {
Box::pin(async move { Box::pin(async move {
let conn = self.conn.lock().await; let conn = self.conn.lock().await;
Ok(crate::TransactionStream::build( Ok(crate::TransactionStream::build(

View File

@ -275,7 +275,7 @@ where
db: &'a C, db: &'a C,
) -> Result<impl Stream<Item = Result<E::Model, DbErr>> + 'b + Send, DbErr> ) -> Result<impl Stream<Item = Result<E::Model, DbErr>> + 'b + Send, DbErr>
where where
C: ConnectionTrait + StreamTrait<'a> + Send, C: ConnectionTrait + StreamTrait + Send,
{ {
self.into_model().stream(db).await self.into_model().stream(db).await
} }
@ -329,7 +329,7 @@ where
db: &'a C, db: &'a C,
) -> Result<impl Stream<Item = Result<(E::Model, Option<F::Model>), DbErr>> + 'b, DbErr> ) -> Result<impl Stream<Item = Result<(E::Model, Option<F::Model>), DbErr>> + 'b, DbErr>
where where
C: ConnectionTrait + StreamTrait<'a> + Send, C: ConnectionTrait + StreamTrait + Send,
{ {
self.into_model().stream(db).await self.into_model().stream(db).await
} }
@ -367,7 +367,7 @@ where
db: &'a C, db: &'a C,
) -> Result<impl Stream<Item = Result<(E::Model, Option<F::Model>), DbErr>> + 'b + Send, DbErr> ) -> Result<impl Stream<Item = Result<(E::Model, Option<F::Model>), DbErr>> + 'b + Send, DbErr>
where where
C: ConnectionTrait + StreamTrait<'a> + Send, C: ConnectionTrait + StreamTrait + Send,
{ {
self.into_model().stream(db).await self.into_model().stream(db).await
} }
@ -453,7 +453,7 @@ where
db: &'a C, db: &'a C,
) -> Result<Pin<Box<dyn Stream<Item = Result<S::Item, DbErr>> + 'b + Send>>, DbErr> ) -> Result<Pin<Box<dyn Stream<Item = Result<S::Item, DbErr>> + 'b + Send>>, DbErr>
where where
C: ConnectionTrait + StreamTrait<'a> + Send, C: ConnectionTrait + StreamTrait + Send,
S: 'b, S: 'b,
S::Item: Send, S::Item: Send,
{ {
@ -739,7 +739,7 @@ where
db: &'a C, db: &'a C,
) -> Result<Pin<Box<dyn Stream<Item = Result<S::Item, DbErr>> + 'b + Send>>, DbErr> ) -> Result<Pin<Box<dyn Stream<Item = Result<S::Item, DbErr>> + 'b + Send>>, DbErr>
where where
C: ConnectionTrait + StreamTrait<'a> + Send, C: ConnectionTrait + StreamTrait + Send,
S: 'b, S: 'b,
S::Item: Send, S::Item: Send,
{ {