Skip to content

Commit 86b8572

Browse files
committed
Remove unnecessary BoxFutures
1 parent ebf04ff commit 86b8572

File tree

16 files changed

+260
-308
lines changed

16 files changed

+260
-308
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,40 +71,40 @@ impl Connection for AnyConnection {
7171

7272
type Options = AnyConnectOptions;
7373

74-
fn close(self) -> BoxFuture<'static, Result<(), Error>> {
75-
self.backend.close()
74+
async fn close(self) -> Result<(), Error> {
75+
self.backend.close().await
7676
}
7777

78-
fn close_hard(self) -> BoxFuture<'static, Result<(), Error>> {
79-
self.backend.close()
78+
async fn close_hard(self) -> Result<(), Error> {
79+
self.backend.close().await
8080
}
8181

82-
fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> {
83-
self.backend.ping()
82+
async fn ping(&mut self) -> Result<(), Error> {
83+
self.backend.ping().await
8484
}
8585

86-
fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
86+
async fn begin(&mut self) -> Result<Transaction<'_, Self::Database>, Error>
8787
where
8888
Self: Sized,
8989
{
90-
Transaction::begin(self)
90+
Transaction::begin(self).await
9191
}
9292

9393
fn cached_statements_size(&self) -> usize {
9494
self.backend.cached_statements_size()
9595
}
9696

97-
fn clear_cached_statements(&mut self) -> BoxFuture<'_, crate::Result<()>> {
98-
self.backend.clear_cached_statements()
97+
async fn clear_cached_statements(&mut self) -> crate::Result<()> {
98+
self.backend.clear_cached_statements().await
9999
}
100100

101101
fn shrink_buffers(&mut self) {
102102
self.backend.shrink_buffers()
103103
}
104104

105105
#[doc(hidden)]
106-
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
107-
self.backend.flush()
106+
async fn flush(&mut self) -> Result<(), Error> {
107+
self.backend.flush().await
108108
}
109109

110110
#[doc(hidden)]

sqlx-core/src/any/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ impl ConnectOptions for AnyConnectOptions {
4848
}
4949

5050
#[inline]
51-
fn connect(&self) -> BoxFuture<'_, Result<AnyConnection, Error>> {
52-
AnyConnection::connect(self)
51+
async fn connect(&self) -> Result<AnyConnection, Error> {
52+
AnyConnection::connect(self).await
5353
}
5454

5555
fn log_statements(mut self, level: LevelFilter) -> Self {

sqlx-core/src/any/transaction.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use futures_util::future::BoxFuture;
2-
31
use crate::any::{Any, AnyConnection};
42
use crate::error::Error;
53
use crate::transaction::TransactionManager;
@@ -9,16 +7,16 @@ pub struct AnyTransactionManager;
97
impl TransactionManager for AnyTransactionManager {
108
type Database = Any;
119

12-
fn begin(conn: &mut AnyConnection) -> BoxFuture<'_, Result<(), Error>> {
13-
conn.backend.begin()
10+
async fn begin(conn: &mut AnyConnection) -> Result<(), Error> {
11+
conn.backend.begin().await
1412
}
1513

16-
fn commit(conn: &mut AnyConnection) -> BoxFuture<'_, Result<(), Error>> {
17-
conn.backend.commit()
14+
async fn commit(conn: &mut AnyConnection) -> Result<(), Error> {
15+
conn.backend.commit().await
1816
}
1917

20-
fn rollback(conn: &mut AnyConnection) -> BoxFuture<'_, Result<(), Error>> {
21-
conn.backend.rollback()
18+
async fn rollback(conn: &mut AnyConnection) -> Result<(), Error> {
19+
conn.backend.rollback().await
2220
}
2321

2422
fn start_rollback(conn: &mut AnyConnection) {

sqlx-core/src/connection.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::database::{Database, HasStatementCache};
2+
use std::future::Future;
23
use crate::error::Error;
34

45
use crate::transaction::Transaction;
@@ -31,21 +32,21 @@ pub trait Connection: Send {
3132
///
3233
/// Therefore it is recommended to call `.close()` on a connection when you are done using it
3334
/// and to `.await` the result to ensure the termination message is sent.
34-
fn close(self) -> BoxFuture<'static, Result<(), Error>>;
35+
fn close(self) -> impl Future<Output = Result<(), Error>> + Send + 'static;
3536

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

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

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

@@ -66,7 +67,7 @@ pub trait Connection: Send {
6667
/// })).await
6768
/// # }
6869
/// ```
69-
fn transaction<'a, F, R, E>(&'a mut self, callback: F) -> BoxFuture<'a, Result<R, E>>
70+
fn transaction<'a, F, R, E>(&'a mut self, callback: F) -> impl Future<Output = Result<R, E>> + Send + 'a
7071
where
7172
for<'c> F: FnOnce(&'c mut Transaction<'_, Self::Database>) -> BoxFuture<'c, Result<R, E>>
7273
+ 'a
@@ -105,11 +106,11 @@ pub trait Connection: Send {
105106

106107
/// Removes all statements from the cache, closing them on the server if
107108
/// needed.
108-
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>
109+
fn clear_cached_statements(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_
109110
where
110111
Self::Database: HasStatementCache,
111112
{
112-
Box::pin(async move { Ok(()) })
113+
async { Ok(()) }
113114
}
114115

115116
/// Restore any buffers in the connection to their default capacity, if possible.
@@ -127,7 +128,7 @@ pub trait Connection: Send {
127128
fn shrink_buffers(&mut self);
128129

129130
#[doc(hidden)]
130-
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;
131+
fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_;
131132

132133
#[doc(hidden)]
133134
fn should_flush(&self) -> bool;
@@ -136,7 +137,7 @@ pub trait Connection: Send {
136137
///
137138
/// A value of [`Options`][Self::Options] is parsed from the provided connection string. This parsing
138139
/// is database-specific.
139-
#[inline]
140+
#[inline]
140141
fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
141142
where
142143
Self: Sized,
@@ -146,12 +147,13 @@ pub trait Connection: Send {
146147
Box::pin(async move { Self::connect_with(&options?).await })
147148
}
148149

150+
149151
/// Establish a new database connection with the provided options.
150-
fn connect_with(options: &Self::Options) -> BoxFuture<'_, Result<Self, Error>>
152+
fn connect_with<'a>(options: &'a Self::Options) -> BoxFuture<'_, Result<Self, Error>>
151153
where
152-
Self: Sized,
154+
Self: Sized + 'a,
153155
{
154-
options.connect()
156+
Box::pin(options.connect())
155157
}
156158
}
157159

@@ -219,7 +221,7 @@ pub trait ConnectOptions: 'static + Send + Sync + FromStr<Err = Error> + Debug +
219221
}
220222

221223
/// Establish a new database connection with the options specified by `self`.
222-
fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
224+
fn connect(&self) -> impl Future<Output = Result<Self::Connection, Error>> + Send + '_
223225
where
224226
Self::Connection: Sized;
225227

sqlx-core/src/transaction.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::future::Future;
23
use std::fmt::{self, Debug, Formatter};
34
use std::ops::{Deref, DerefMut};
45

@@ -18,17 +19,18 @@ pub trait TransactionManager {
1819
/// Begin a new transaction or establish a savepoint within the active transaction.
1920
fn begin(
2021
conn: &mut <Self::Database as Database>::Connection,
21-
) -> BoxFuture<'_, Result<(), Error>>;
22+
) -> impl Future<Output = Result<(), Error>> + Send;
2223

2324
/// Commit the active transaction or release the most recent savepoint.
2425
fn commit(
2526
conn: &mut <Self::Database as Database>::Connection,
26-
) -> BoxFuture<'_, Result<(), Error>>;
27+
) -> impl Future<Output = Result<(), Error>> + Send;
2728

2829
/// Abort the active transaction or restore from the most recent savepoint.
2930
fn rollback(
3031
conn: &mut <Self::Database as Database>::Connection,
31-
) -> BoxFuture<'_, Result<(), Error>>;
32+
) -> impl Future<Output = Result<(), Error>> + Send;
33+
3234

3335
/// Starts to abort the active transaction or restore from the most recent snapshot.
3436
fn start_rollback(conn: &mut <Self::Database as Database>::Connection);

sqlx-mysql/src/connection/mod.rs

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::fmt::{self, Debug, Formatter};
22

3-
use futures_core::future::BoxFuture;
43
use futures_util::FutureExt;
54
pub(crate) use sqlx_core::connection::*;
65
pub(crate) use stream::{MySqlStream, Waiting};
@@ -52,66 +51,58 @@ impl Connection for MySqlConnection {
5251

5352
type Options = MySqlConnectOptions;
5453

55-
fn close(mut self) -> BoxFuture<'static, Result<(), Error>> {
56-
Box::pin(async move {
57-
self.inner.stream.send_packet(Quit).await?;
58-
self.inner.stream.shutdown().await?;
54+
async fn close(mut self) -> Result<(), Error> {
55+
self.inner.stream.send_packet(Quit).await?;
56+
self.inner.stream.shutdown().await?;
5957

60-
Ok(())
61-
})
58+
Ok(())
6259
}
6360

64-
fn close_hard(mut self) -> BoxFuture<'static, Result<(), Error>> {
65-
Box::pin(async move {
66-
self.inner.stream.shutdown().await?;
67-
Ok(())
68-
})
61+
async fn close_hard(mut self) -> Result<(), Error> {
62+
self.inner.stream.shutdown().await?;
63+
Ok(())
6964
}
7065

71-
fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> {
72-
Box::pin(async move {
73-
self.inner.stream.wait_until_ready().await?;
74-
self.inner.stream.send_packet(Ping).await?;
75-
self.inner.stream.recv_ok().await?;
66+
async fn ping(&mut self) -> Result<(), Error> {
67+
self.inner.stream.wait_until_ready().await?;
68+
self.inner.stream.send_packet(Ping).await?;
69+
self.inner.stream.recv_ok().await?;
7670

77-
Ok(())
78-
})
71+
Ok(())
7972
}
8073

8174
#[doc(hidden)]
82-
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
83-
self.inner.stream.wait_until_ready().boxed()
75+
async fn flush(&mut self) -> Result<(), Error> {
76+
self.inner.stream.wait_until_ready().await
8477
}
8578

8679
fn cached_statements_size(&self) -> usize {
8780
self.inner.cache_statement.len()
8881
}
8982

90-
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
91-
Box::pin(async move {
92-
while let Some((statement_id, _)) = self.inner.cache_statement.remove_lru() {
93-
self.inner
94-
.stream
95-
.send_packet(StmtClose {
96-
statement: statement_id,
97-
})
98-
.await?;
99-
}
100-
101-
Ok(())
102-
})
83+
async fn clear_cached_statements(&mut self) -> Result<(), Error> {
84+
while let Some((statement_id, _)) = self.inner.cache_statement.remove_lru() {
85+
self.inner
86+
.stream
87+
.send_packet(StmtClose {
88+
statement: statement_id,
89+
})
90+
.await?;
91+
}
92+
93+
Ok(())
10394
}
10495

10596
#[doc(hidden)]
10697
fn should_flush(&self) -> bool {
10798
!self.inner.stream.write_buffer().is_empty()
10899
}
109100

110-
fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
101+
async fn begin(&mut self) -> Result<Transaction<'_, Self::Database>, Error>
111102
where
112103
Self: Sized,
113104
{
114-
Transaction::begin(self)
105+
Transaction::begin(self).await
115106
}
116107

117108
fn shrink_buffers(&mut self) {

0 commit comments

Comments
 (0)