Skip to content

Commit 220c211

Browse files
committed
updated transactions to expect callback functions to return a ScopedBoxFuture in place of BoxFuture -- this change allows transactions to be called with closures which capture variables rather than move them
1 parent 439a5f1 commit 220c211

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

Cargo.lock

Lines changed: 39 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mysql_common = {version = "0.29.0", optional = true}
2424
bb8 = {version = "0.8", optional = true}
2525
deadpool = {version = "0.9", optional = true}
2626
mobc = {version = "0.7", optional = true}
27+
scoped-futures = { rev = "362cd58", git = "https://github.com/sidechainme/scoped-futures", features = [] }
2728

2829
[dev-dependencies]
2930
tokio = {version = "1.12.0", features = ["rt", "macros", "rt-multi-thread"]}

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ use diesel::backend::Backend;
7070
use diesel::query_builder::{AsQuery, QueryFragment, QueryId};
7171
use diesel::row::Row;
7272
use diesel::{ConnectionResult, QueryResult};
73-
use futures::future::BoxFuture;
7473
use futures::{Future, Stream};
74+
use scoped_futures::ScopedBoxFuture;
7575

7676
#[cfg(feature = "mysql")]
7777
mod mysql;
@@ -221,11 +221,11 @@ where
221221
/// # Ok(())
222222
/// # }
223223
/// ```
224-
async fn transaction<R, E, F>(&mut self, callback: F) -> Result<R, E>
224+
async fn transaction<'a, R, E, F>(&mut self, callback: F) -> Result<R, E>
225225
where
226-
F: FnOnce(&mut Self) -> BoxFuture<Result<R, E>> + Send,
227-
E: From<diesel::result::Error> + Send,
228-
R: Send,
226+
F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a,
227+
E: From<diesel::result::Error> + Send + 'a,
228+
R: Send + 'a,
229229
{
230230
Self::TransactionManager::transaction(self, callback).await
231231
}

src/transaction_manager.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use diesel::result::Error;
22
use diesel::QueryResult;
3-
use futures::future::BoxFuture;
3+
use scoped_futures::ScopedFuture;
44
use std::borrow::Cow;
55
use std::num::NonZeroU32;
66

@@ -50,9 +50,9 @@ pub trait TransactionManager<Conn: AsyncConnection>: Send {
5050
///
5151
/// Each implementation of this function needs to fulfill the documented
5252
/// behaviour of [`AsyncConnection::transaction`]
53-
async fn transaction<F, R, E>(conn: &mut Conn, callback: F) -> Result<R, E>
53+
async fn transaction<'a, F, R, E>(conn: &mut Conn, callback: F) -> Result<R, E>
5454
where
55-
F: FnOnce(&mut Conn) -> BoxFuture<Result<R, E>> + Send,
55+
F: for<'r> FnOnce(&'r mut Conn) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a,
5656
E: From<Error> + Send,
5757
R: Send,
5858
{

0 commit comments

Comments
 (0)