Skip to content
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

use GAT to elide StreamTrait lifetime #1161

Merged
merged 4 commits into from
Nov 6, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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