Skip to content

Commit 203a2b8

Browse files
committed
Remove BoxFuture for Connection, MigrateDatabase, TransactionManager, ConnectOptions, TestSupport, PgPoolCopyExt
1 parent 0ef5879 commit 203a2b8

File tree

13 files changed

+196
-232
lines changed

13 files changed

+196
-232
lines changed

sqlx-core/src/any/connection/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ impl AnyConnection {
6161
#[cfg(feature = "migrate")]
6262
pub(crate) fn get_migrate(
6363
&mut self,
64-
) -> crate::Result<&mut (dyn crate::migrate::Migrate + Send + 'static)> {
64+
) -> crate::Result<&mut (dyn crate::migrate::Migrate + Send + 'static)>
65+
{
6566
self.backend.as_migrate()
6667
}
6768
}

sqlx-core/src/any/driver.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ impl AnyDriver {
6767
{
6868
Self {
6969
migrate_database: Some(AnyMigrateDatabase {
70-
create_database: DebugFn(DB::create_database),
71-
database_exists: DebugFn(DB::database_exists),
72-
drop_database: DebugFn(DB::drop_database),
73-
force_drop_database: DebugFn(DB::force_drop_database),
70+
create_database: DebugFn(|url| Box::pin(async move { DB::create_database(url).await })),
71+
database_exists: DebugFn(|url| Box::pin(async move { DB::database_exists(url).await })),
72+
drop_database: DebugFn(|url| Box::pin(async move { DB::drop_database(url).await })),
73+
force_drop_database: DebugFn(|url| Box::pin(async move { DB::force_drop_database(url).await })),
7474
}),
7575
..Self::without_migrate::<DB>()
7676
}

sqlx-core/src/any/migrate.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,32 @@ use futures_core::future::BoxFuture;
66
use std::time::Duration;
77

88
impl MigrateDatabase for Any {
9-
fn create_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
10-
Box::pin(async {
11-
driver::from_url_str(url)?
12-
.get_migrate_database()?
13-
.create_database(url)
14-
.await
15-
})
9+
async fn create_database(url: &str) -> Result<(), Error> {
10+
driver::from_url_str(url)?
11+
.get_migrate_database()?
12+
.create_database(url)
13+
.await
1614
}
1715

18-
fn database_exists(url: &str) -> BoxFuture<'_, Result<bool, Error>> {
19-
Box::pin(async {
20-
driver::from_url_str(url)?
21-
.get_migrate_database()?
22-
.database_exists(url)
23-
.await
24-
})
16+
async fn database_exists(url: &str) -> Result<bool, Error> {
17+
driver::from_url_str(url)?
18+
.get_migrate_database()?
19+
.database_exists(url)
20+
.await
2521
}
2622

27-
fn drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
28-
Box::pin(async {
29-
driver::from_url_str(url)?
30-
.get_migrate_database()?
31-
.drop_database(url)
32-
.await
33-
})
23+
async fn drop_database(url: &str) -> Result<(), Error> {
24+
driver::from_url_str(url)?
25+
.get_migrate_database()?
26+
.drop_database(url)
27+
.await
3428
}
3529

36-
fn force_drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
37-
Box::pin(async {
38-
driver::from_url_str(url)?
39-
.get_migrate_database()?
40-
.force_drop_database(url)
41-
.await
42-
})
30+
async fn force_drop_database(url: &str) -> Result<(), Error> {
31+
driver::from_url_str(url)?
32+
.get_migrate_database()?
33+
.force_drop_database(url)
34+
.await
4335
}
4436
}
4537

sqlx-core/src/connection.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub trait Connection: Send {
7777
R: Send,
7878
E: From<Error> + Send,
7979
{
80-
Box::pin(async move {
80+
async move {
8181
let mut transaction = self.begin().await?;
8282
let ret = callback(&mut transaction).await;
8383

@@ -93,7 +93,7 @@ pub trait Connection: Send {
9393
Err(err)
9494
}
9595
}
96-
})
96+
}
9797
}
9898

9999
/// The number of statements currently cached in the connection.
@@ -138,22 +138,22 @@ pub trait Connection: Send {
138138
/// A value of [`Options`][Self::Options] is parsed from the provided connection string. This parsing
139139
/// is database-specific.
140140
#[inline]
141-
fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
141+
fn connect(url: &str) -> impl Future<Output = Result<Self, Error>> + Send + 'static
142142
where
143143
Self: Sized,
144144
{
145145
let options = url.parse();
146146

147-
Box::pin(async move { Self::connect_with(&options?).await })
147+
async move { Self::connect_with(&options?).await }
148148
}
149149

150150

151151
/// Establish a new database connection with the provided options.
152-
fn connect_with(options: &Self::Options) -> BoxFuture<'_, Result<Self, Error>>
152+
fn connect_with(options: &Self::Options) -> impl Future<Output = Result<Self, Error>> + Send + '_
153153
where
154154
Self: Sized
155155
{
156-
Box::pin(options.connect())
156+
options.connect()
157157
}
158158
}
159159

sqlx-core/src/migrate/migrate.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@ use crate::error::Error;
22
use crate::migrate::{AppliedMigration, MigrateError, Migration};
33
use futures_core::future::BoxFuture;
44
use std::time::Duration;
5+
use std::future::Future;
56

67
pub trait MigrateDatabase {
78
// create database in url
89
// uses a maintenance database depending on driver
9-
fn create_database(url: &str) -> BoxFuture<'_, Result<(), Error>>;
10+
fn create_database(url: &str) -> impl Future<Output = Result<(), Error>> + Send + '_;
1011

1112
// check if the database in url exists
1213
// uses a maintenance database depending on driver
13-
fn database_exists(url: &str) -> BoxFuture<'_, Result<bool, Error>>;
14+
fn database_exists(url: &str) -> impl Future<Output = Result<bool, Error>> + Send + '_;
1415

1516
// drop database in url
1617
// uses a maintenance database depending on driver
17-
fn drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>>;
18+
fn drop_database(url: &str) -> impl Future<Output = Result<(), Error>> + Send + '_;
1819

1920
// force drop database in url
2021
// uses a maintenance database depending on driver
21-
fn force_drop_database(_url: &str) -> BoxFuture<'_, Result<(), Error>> {
22-
Box::pin(async { Err(MigrateError::ForceNotSupported)? })
22+
fn force_drop_database(_url: &str) -> impl Future<Output = Result<(), Error>> + Send + '_ {
23+
async { Err(MigrateError::ForceNotSupported)? }
2324
}
2425
}
2526

sqlx-core/src/testing/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ pub trait TestSupport: Database {
2424
///
2525
/// The implementation may require `DATABASE_URL` to be set in order to manage databases.
2626
/// The user credentials it contains must have the privilege to create and drop databases.
27-
fn test_context(args: &TestArgs) -> BoxFuture<'_, Result<TestContext<Self>, Error>>;
27+
fn test_context(args: &TestArgs) -> impl Future<Output = Result<TestContext<Self>, Error>> + Send + '_;
2828

29-
fn cleanup_test(db_name: &str) -> BoxFuture<'_, Result<(), Error>>;
29+
fn cleanup_test(db_name: &str) -> impl Future<Output = Result<(), Error>> + Send + '_;
3030

3131
/// Cleanup any test databases that are no longer in-use.
3232
///
3333
/// Returns a count of the databases deleted, if possible.
3434
///
3535
/// The implementation may require `DATABASE_URL` to be set in order to manage databases.
3636
/// The user credentials it contains must have the privilege to create and drop databases.
37-
fn cleanup_test_dbs() -> BoxFuture<'static, Result<Option<usize>, Error>>;
37+
fn cleanup_test_dbs() -> impl Future<Output = Result<Option<usize>, Error>> + Send + 'static;
3838

3939
/// Take a snapshot of the current state of the database (data only).
4040
///

sqlx-core/src/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl<'c, 't, DB: Database> crate::acquire::Acquire<'t> for &'t mut Transaction<'
233233
type Connection = &'t mut <DB as Database>::Connection;
234234

235235
#[inline]
236-
fn acquire(self) -> BoxFuture<'t, Result<Self::Connection, Error>> {
236+
fn acquire(self) -> BoxFuture<'t, Result<Self::Connection, Error>> {
237237
Box::pin(futures_util::future::ok(&mut **self))
238238
}
239239

sqlx-mysql/src/migrate.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,40 @@ fn parse_for_maintenance(url: &str) -> Result<(MySqlConnectOptions, String), Err
3131
}
3232

3333
impl MigrateDatabase for MySql {
34-
fn create_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
35-
Box::pin(async move {
36-
let (options, database) = parse_for_maintenance(url)?;
37-
let mut conn = options.connect().await?;
34+
async fn create_database(url: &str) -> Result<(), Error> {
35+
let (options, database) = parse_for_maintenance(url)?;
36+
let mut conn = options.connect().await?;
3837

39-
let _ = conn
40-
.execute(&*format!("CREATE DATABASE `{database}`"))
41-
.await?;
38+
let _ = conn
39+
.execute(&*format!("CREATE DATABASE `{database}`"))
40+
.await?;
4241

43-
Ok(())
44-
})
42+
Ok(())
4543
}
4644

47-
fn database_exists(url: &str) -> BoxFuture<'_, Result<bool, Error>> {
48-
Box::pin(async move {
49-
let (options, database) = parse_for_maintenance(url)?;
50-
let mut conn = options.connect().await?;
45+
async fn database_exists(url: &str) -> Result<bool, Error> {
46+
let (options, database) = parse_for_maintenance(url)?;
47+
let mut conn = options.connect().await?;
5148

52-
let exists: bool = query_scalar(
53-
"select exists(SELECT 1 from INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?)",
54-
)
55-
.bind(database)
56-
.fetch_one(&mut conn)
57-
.await?;
49+
let exists: bool = query_scalar(
50+
"select exists(SELECT 1 from INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?)",
51+
)
52+
.bind(database)
53+
.fetch_one(&mut conn)
54+
.await?;
5855

59-
Ok(exists)
60-
})
56+
Ok(exists)
6157
}
6258

63-
fn drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
64-
Box::pin(async move {
65-
let (options, database) = parse_for_maintenance(url)?;
66-
let mut conn = options.connect().await?;
59+
async fn drop_database(url: &str) -> Result<(), Error> {
60+
let (options, database) = parse_for_maintenance(url)?;
61+
let mut conn = options.connect().await?;
6762

68-
let _ = conn
69-
.execute(&*format!("DROP DATABASE IF EXISTS `{database}`"))
70-
.await?;
63+
let _ = conn
64+
.execute(&*format!("DROP DATABASE IF EXISTS `{database}`"))
65+
.await?;
7166

72-
Ok(())
73-
})
67+
Ok(())
7468
}
7569
}
7670

sqlx-mysql/src/testing/mod.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,46 +26,42 @@ static MASTER_POOL: OnceCell<Pool<MySql>> = OnceCell::new();
2626
static DO_CLEANUP: AtomicBool = AtomicBool::new(true);
2727

2828
impl TestSupport for MySql {
29-
fn test_context(args: &TestArgs) -> BoxFuture<'_, Result<TestContext<Self>, Error>> {
30-
Box::pin(async move { test_context(args).await })
29+
async fn test_context(args: &TestArgs) -> Result<TestContext<Self>, Error> {
30+
test_context(args).await
3131
}
3232

33-
fn cleanup_test(db_name: &str) -> BoxFuture<'_, Result<(), Error>> {
34-
Box::pin(async move {
35-
let mut conn = MASTER_POOL
36-
.get()
37-
.expect("cleanup_test() invoked outside `#[sqlx::test]")
38-
.acquire()
39-
.await?;
33+
async fn cleanup_test(db_name: &str) -> Result<(), Error> {
34+
let mut conn = MASTER_POOL
35+
.get()
36+
.expect("cleanup_test() invoked outside `#[sqlx::test]")
37+
.acquire()
38+
.await?;
4039

41-
let db_id = db_id(db_name);
40+
let db_id = db_id(db_name);
4241

43-
conn.execute(&format!("drop database if exists {db_name};")[..])
44-
.await?;
42+
conn.execute(&format!("drop database if exists {db_name};")[..])
43+
.await?;
4544

46-
query("delete from _sqlx_test_databases where db_id = ?")
47-
.bind(db_id)
48-
.execute(&mut *conn)
49-
.await?;
45+
query("delete from _sqlx_test_databases where db_id = ?")
46+
.bind(db_id)
47+
.execute(&mut *conn)
48+
.await?;
5049

51-
Ok(())
52-
})
50+
Ok(())
5351
}
5452

55-
fn cleanup_test_dbs() -> BoxFuture<'static, Result<Option<usize>, Error>> {
56-
Box::pin(async move {
57-
let url = dotenvy::var("DATABASE_URL").expect("DATABASE_URL must be set");
53+
async fn cleanup_test_dbs() -> Result<Option<usize>, Error> {
54+
let url = dotenvy::var("DATABASE_URL").expect("DATABASE_URL must be set");
5855

59-
let mut conn = MySqlConnection::connect(&url).await?;
56+
let mut conn = MySqlConnection::connect(&url).await?;
6057

61-
let now = SystemTime::now()
62-
.duration_since(SystemTime::UNIX_EPOCH)
63-
.unwrap();
58+
let now = SystemTime::now()
59+
.duration_since(SystemTime::UNIX_EPOCH)
60+
.unwrap();
6461

65-
let num_deleted = do_cleanup(&mut conn, now).await?;
66-
let _ = conn.close().await;
67-
Ok(Some(num_deleted))
68-
})
62+
let num_deleted = do_cleanup(&mut conn, now).await?;
63+
let _ = conn.close().await;
64+
Ok(Some(num_deleted))
6965
}
7066

7167
fn snapshot(

0 commit comments

Comments
 (0)