Skip to content

Commit 0f891a3

Browse files
authored
Remove unnecessary boxfutures (#3525)
* Remove `BoxFuture`'s from `Connection` and `ConnectOptions` * Remove `BoxFuture`'s for `MigrateDatabase` * Remove `BoxFuture`'s for `TransactionManager` * Remove `BoxFuture`'s for `TestSupport` * Remove `BoxFuture`'s from `PgPoolCopyExt` * Clippy fixes * Box timeout future in debug mode
1 parent a9fb581 commit 0f891a3

File tree

29 files changed

+575
-654
lines changed

29 files changed

+575
-654
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use futures_core::future::BoxFuture;
22
use std::borrow::Cow;
3+
use std::future::Future;
34

45
use crate::any::{Any, AnyConnectOptions};
56
use crate::connection::{ConnectOptions, Connection};
@@ -72,19 +73,21 @@ impl Connection for AnyConnection {
7273

7374
type Options = AnyConnectOptions;
7475

75-
fn close(self) -> BoxFuture<'static, Result<(), Error>> {
76+
fn close(self) -> impl Future<Output = Result<(), Error>> + Send + 'static {
7677
self.backend.close()
7778
}
7879

79-
fn close_hard(self) -> BoxFuture<'static, Result<(), Error>> {
80+
fn close_hard(self) -> impl Future<Output = Result<(), Error>> + Send + 'static {
8081
self.backend.close()
8182
}
8283

83-
fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> {
84+
fn ping(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_ {
8485
self.backend.ping()
8586
}
8687

87-
fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
88+
fn begin(
89+
&mut self,
90+
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_
8891
where
8992
Self: Sized,
9093
{
@@ -94,7 +97,7 @@ impl Connection for AnyConnection {
9497
fn begin_with(
9598
&mut self,
9699
statement: impl Into<Cow<'static, str>>,
97-
) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
100+
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_
98101
where
99102
Self: Sized,
100103
{
@@ -105,7 +108,7 @@ impl Connection for AnyConnection {
105108
self.backend.cached_statements_size()
106109
}
107110

108-
fn clear_cached_statements(&mut self) -> BoxFuture<'_, crate::Result<()>> {
111+
fn clear_cached_statements(&mut self) -> impl Future<Output = crate::Result<()>> + Send + '_ {
109112
self.backend.clear_cached_statements()
110113
}
111114

@@ -114,7 +117,7 @@ impl Connection for AnyConnection {
114117
}
115118

116119
#[doc(hidden)]
117-
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
120+
fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_ {
118121
self.backend.flush()
119122
}
120123

sqlx-core/src/any/driver.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::connection::Connection;
55
use crate::database::Database;
66
use crate::Error;
77
use futures_core::future::BoxFuture;
8+
use futures_util::FutureExt;
89
use std::fmt::{Debug, Formatter};
910
use std::sync::OnceLock;
1011
use url::Url;
@@ -67,10 +68,10 @@ impl AnyDriver {
6768
{
6869
Self {
6970
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),
71+
create_database: DebugFn(|url| DB::create_database(url).boxed()),
72+
database_exists: DebugFn(|url| DB::database_exists(url).boxed()),
73+
drop_database: DebugFn(|url| DB::drop_database(url).boxed()),
74+
force_drop_database: DebugFn(|url| DB::force_drop_database(url).boxed()),
7475
}),
7576
..Self::without_migrate::<DB>()
7677
}

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/any/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::any::AnyConnection;
22
use crate::connection::{ConnectOptions, LogSettings};
33
use crate::error::Error;
4-
use futures_core::future::BoxFuture;
54
use log::LevelFilter;
5+
use std::future::Future;
66
use std::str::FromStr;
77
use std::time::Duration;
88
use url::Url;
@@ -48,7 +48,7 @@ impl ConnectOptions for AnyConnectOptions {
4848
}
4949

5050
#[inline]
51-
fn connect(&self) -> BoxFuture<'_, Result<AnyConnection, Error>> {
51+
fn connect(&self) -> impl Future<Output = Result<AnyConnection, Error>> + Send + '_ {
5252
AnyConnection::connect(self)
5353
}
5454

sqlx-core/src/any/transaction.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use futures_core::future::BoxFuture;
21
use std::borrow::Cow;
2+
use std::future::Future;
33

44
use crate::any::{Any, AnyConnection};
55
use crate::database::Database;
@@ -14,15 +14,15 @@ impl TransactionManager for AnyTransactionManager {
1414
fn begin<'conn>(
1515
conn: &'conn mut AnyConnection,
1616
statement: Option<Cow<'static, str>>,
17-
) -> BoxFuture<'conn, Result<(), Error>> {
17+
) -> impl Future<Output = Result<(), Error>> + Send + 'conn {
1818
conn.backend.begin(statement)
1919
}
2020

21-
fn commit(conn: &mut AnyConnection) -> BoxFuture<'_, Result<(), Error>> {
21+
fn commit(conn: &mut AnyConnection) -> impl Future<Output = Result<(), Error>> + Send + '_ {
2222
conn.backend.commit()
2323
}
2424

25-
fn rollback(conn: &mut AnyConnection) -> BoxFuture<'_, Result<(), Error>> {
25+
fn rollback(conn: &mut AnyConnection) -> impl Future<Output = Result<(), Error>> + Send + '_ {
2626
conn.backend.rollback()
2727
}
2828

sqlx-core/src/connection.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use futures_core::future::BoxFuture;
66
use log::LevelFilter;
77
use std::borrow::Cow;
88
use std::fmt::Debug;
9+
use std::future::Future;
910
use std::str::FromStr;
1011
use std::time::Duration;
1112
use url::Url;
@@ -32,23 +33,23 @@ pub trait Connection: Send {
3233
///
3334
/// Therefore it is recommended to call `.close()` on a connection when you are done using it
3435
/// and to `.await` the result to ensure the termination message is sent.
35-
fn close(self) -> BoxFuture<'static, Result<(), Error>>;
36+
fn close(self) -> impl Future<Output = Result<(), Error>> + Send + 'static;
3637

3738
/// Immediately close the connection without sending a graceful shutdown.
3839
///
3940
/// This should still at least send a TCP `FIN` frame to let the server know we're dying.
4041
#[doc(hidden)]
41-
fn close_hard(self) -> BoxFuture<'static, Result<(), Error>>;
42+
fn close_hard(self) -> impl Future<Output = Result<(), Error>> + Send + 'static;
4243

4344
/// Checks if a connection to the database is still valid.
44-
fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>;
45+
fn ping(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_;
4546

4647
/// Begin a new transaction or establish a savepoint within the active transaction.
4748
///
4849
/// Returns a [`Transaction`] for controlling and tracking the new transaction.
49-
fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
50-
where
51-
Self: Sized;
50+
fn begin(
51+
&mut self,
52+
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_;
5253

5354
/// Begin a new transaction with a custom statement.
5455
///
@@ -59,7 +60,7 @@ pub trait Connection: Send {
5960
fn begin_with(
6061
&mut self,
6162
statement: impl Into<Cow<'static, str>>,
62-
) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
63+
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_
6364
where
6465
Self: Sized,
6566
{
@@ -94,7 +95,10 @@ pub trait Connection: Send {
9495
/// })).await
9596
/// # }
9697
/// ```
97-
fn transaction<'a, F, R, E>(&'a mut self, callback: F) -> BoxFuture<'a, Result<R, E>>
98+
fn transaction<'a, F, R, E>(
99+
&'a mut self,
100+
callback: F,
101+
) -> impl Future<Output = Result<R, E>> + Send + 'a
98102
where
99103
for<'c> F: FnOnce(&'c mut Transaction<'_, Self::Database>) -> BoxFuture<'c, Result<R, E>>
100104
+ 'a
@@ -104,7 +108,7 @@ pub trait Connection: Send {
104108
R: Send,
105109
E: From<Error> + Send,
106110
{
107-
Box::pin(async move {
111+
async move {
108112
let mut transaction = self.begin().await?;
109113
let ret = callback(&mut transaction).await;
110114

@@ -120,7 +124,7 @@ pub trait Connection: Send {
120124
Err(err)
121125
}
122126
}
123-
})
127+
}
124128
}
125129

126130
/// The number of statements currently cached in the connection.
@@ -133,11 +137,11 @@ pub trait Connection: Send {
133137

134138
/// Removes all statements from the cache, closing them on the server if
135139
/// needed.
136-
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>
140+
fn clear_cached_statements(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_
137141
where
138142
Self::Database: HasStatementCache,
139143
{
140-
Box::pin(async move { Ok(()) })
144+
async move { Ok(()) }
141145
}
142146

143147
/// Restore any buffers in the connection to their default capacity, if possible.
@@ -155,7 +159,7 @@ pub trait Connection: Send {
155159
fn shrink_buffers(&mut self);
156160

157161
#[doc(hidden)]
158-
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;
162+
fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_;
159163

160164
#[doc(hidden)]
161165
fn should_flush(&self) -> bool;
@@ -165,17 +169,19 @@ pub trait Connection: Send {
165169
/// A value of [`Options`][Self::Options] is parsed from the provided connection string. This parsing
166170
/// is database-specific.
167171
#[inline]
168-
fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
172+
fn connect(url: &str) -> impl Future<Output = Result<Self, Error>> + Send + 'static
169173
where
170174
Self: Sized,
171175
{
172176
let options = url.parse();
173177

174-
Box::pin(async move { Self::connect_with(&options?).await })
178+
async move { Self::connect_with(&options?).await }
175179
}
176180

177181
/// Establish a new database connection with the provided options.
178-
fn connect_with(options: &Self::Options) -> BoxFuture<'_, Result<Self, Error>>
182+
fn connect_with(
183+
options: &Self::Options,
184+
) -> impl Future<Output = Result<Self, Error>> + Send + '_
179185
where
180186
Self: Sized,
181187
{
@@ -247,7 +253,7 @@ pub trait ConnectOptions: 'static + Send + Sync + FromStr<Err = Error> + Debug +
247253
}
248254

249255
/// Establish a new database connection with the options specified by `self`.
250-
fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
256+
fn connect(&self) -> impl Future<Output = Result<Self::Connection, Error>> + Send + '_
251257
where
252258
Self::Connection: Sized;
253259

sqlx-core/src/migrate/migrate.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
use crate::error::Error;
22
use crate::migrate::{AppliedMigration, MigrateError, Migration};
33
use futures_core::future::BoxFuture;
4+
use std::future::Future;
45
use std::time::Duration;
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/rt/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ pub enum JoinHandle<T> {
2424
}
2525

2626
pub async fn timeout<F: Future>(duration: Duration, f: F) -> Result<F::Output, TimeoutError> {
27+
#[cfg(debug_assertions)]
28+
let f = Box::pin(f);
29+
2730
#[cfg(feature = "_rt-tokio")]
2831
if rt_tokio::available() {
2932
return tokio::time::timeout(duration, f)

0 commit comments

Comments
 (0)