Skip to content

Commit

Permalink
use GAT to elide StreamTrait lifetime (#1161)
Browse files Browse the repository at this point in the history
  • Loading branch information
nappa85 authored Nov 6, 2022
1 parent 9952aa6 commit 9d25ee9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
10 changes: 6 additions & 4 deletions src/database/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
fn stream(
fn stream<'a>(
&'a self,
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
Expand Down
8 changes: 4 additions & 4 deletions src/database/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 {
Ok(match self {
#[cfg(feature = "sqlx-mysql")]
Expand Down
10 changes: 4 additions & 6 deletions src/database/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 {
let conn = self.conn.lock().await;
Ok(crate::TransactionStream::build(
Expand Down
10 changes: 5 additions & 5 deletions src/executor/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ where
db: &'a C,
) -> Result<impl Stream<Item = Result<E::Model, DbErr>> + 'b + Send, DbErr>
where
C: ConnectionTrait + StreamTrait<'a> + Send,
C: ConnectionTrait + StreamTrait + Send,
{
self.into_model().stream(db).await
}
Expand Down Expand Up @@ -329,7 +329,7 @@ where
db: &'a C,
) -> Result<impl Stream<Item = Result<(E::Model, Option<F::Model>), DbErr>> + 'b, DbErr>
where
C: ConnectionTrait + StreamTrait<'a> + Send,
C: ConnectionTrait + StreamTrait + Send,
{
self.into_model().stream(db).await
}
Expand Down Expand Up @@ -367,7 +367,7 @@ where
db: &'a C,
) -> Result<impl Stream<Item = Result<(E::Model, Option<F::Model>), DbErr>> + 'b + Send, DbErr>
where
C: ConnectionTrait + StreamTrait<'a> + Send,
C: ConnectionTrait + StreamTrait + Send,
{
self.into_model().stream(db).await
}
Expand Down Expand Up @@ -453,7 +453,7 @@ where
db: &'a C,
) -> Result<Pin<Box<dyn Stream<Item = Result<S::Item, DbErr>> + 'b + Send>>, DbErr>
where
C: ConnectionTrait + StreamTrait<'a> + Send,
C: ConnectionTrait + StreamTrait + Send,
S: 'b,
S::Item: Send,
{
Expand Down Expand Up @@ -739,7 +739,7 @@ where
db: &'a C,
) -> Result<Pin<Box<dyn Stream<Item = Result<S::Item, DbErr>> + 'b + Send>>, DbErr>
where
C: ConnectionTrait + StreamTrait<'a> + Send,
C: ConnectionTrait + StreamTrait + Send,
S: 'b,
S::Item: Send,
{
Expand Down

0 comments on commit 9d25ee9

Please sign in to comment.