Skip to content

Commit 1f6ce33

Browse files
authored
chore: remove BoxFuture's (non-breaking) (#3629)
* chore: reduce BoxFuture's when using recursion. * remove BoxFuture's in WithSocket * chore: better document previous changes
1 parent 42ce24d commit 1f6ce33

File tree

8 files changed

+122
-132
lines changed

8 files changed

+122
-132
lines changed

Cargo.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqlx-core/src/net/socket/mod.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,18 @@ where
143143
pub trait WithSocket {
144144
type Output;
145145

146-
fn with_socket<S: Socket>(self, socket: S) -> Self::Output;
146+
fn with_socket<S: Socket>(
147+
self,
148+
socket: S,
149+
) -> impl std::future::Future<Output = Self::Output> + Send;
147150
}
148151

149152
pub struct SocketIntoBox;
150153

151154
impl WithSocket for SocketIntoBox {
152155
type Output = Box<dyn Socket>;
153156

154-
fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
157+
async fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
155158
Box::new(socket)
156159
}
157160
}
@@ -197,7 +200,7 @@ pub async fn connect_tcp<Ws: WithSocket>(
197200
let stream = TcpStream::connect((host, port)).await?;
198201
stream.set_nodelay(true)?;
199202

200-
return Ok(with_socket.with_socket(stream));
203+
return Ok(with_socket.with_socket(stream).await);
201204
}
202205

203206
#[cfg(feature = "_rt-async-std")]
@@ -217,7 +220,7 @@ pub async fn connect_tcp<Ws: WithSocket>(
217220
Ok(s)
218221
});
219222
match stream {
220-
Ok(stream) => return Ok(with_socket.with_socket(stream)),
223+
Ok(stream) => return Ok(with_socket.with_socket(stream).await),
221224
Err(e) => last_err = Some(e),
222225
}
223226
}
@@ -255,7 +258,7 @@ pub async fn connect_uds<P: AsRef<Path>, Ws: WithSocket>(
255258

256259
let stream = UnixStream::connect(path).await?;
257260

258-
return Ok(with_socket.with_socket(stream));
261+
return Ok(with_socket.with_socket(stream).await);
259262
}
260263

261264
#[cfg(feature = "_rt-async-std")]
@@ -265,7 +268,7 @@ pub async fn connect_uds<P: AsRef<Path>, Ws: WithSocket>(
265268

266269
let stream = Async::<UnixStream>::connect(path).await?;
267270

268-
Ok(with_socket.with_socket(stream))
271+
Ok(with_socket.with_socket(stream).await)
269272
}
270273

271274
#[cfg(not(feature = "_rt-async-std"))]

sqlx-core/src/net/tls/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,14 @@ where
7575
Ws: WithSocket,
7676
{
7777
#[cfg(feature = "_tls-native-tls")]
78-
return Ok(with_socket.with_socket(tls_native_tls::handshake(socket, config).await?));
78+
return Ok(with_socket
79+
.with_socket(tls_native_tls::handshake(socket, config).await?)
80+
.await);
7981

8082
#[cfg(all(feature = "_tls-rustls", not(feature = "_tls-native-tls")))]
81-
return Ok(with_socket.with_socket(tls_rustls::handshake(socket, config).await?));
83+
return Ok(with_socket
84+
.with_socket(tls_rustls::handshake(socket, config).await?)
85+
.await);
8286

8387
#[cfg(not(any(feature = "_tls-native-tls", feature = "_tls-rustls")))]
8488
{

sqlx-mysql/src/connection/establish.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use bytes::buf::Buf;
22
use bytes::Bytes;
3-
use futures_core::future::BoxFuture;
43

54
use crate::collation::{CharSet, Collation};
65
use crate::common::StatementCache;
@@ -22,7 +21,7 @@ impl MySqlConnection {
2221
None => crate::net::connect_tcp(&options.host, options.port, do_handshake).await?,
2322
};
2423

25-
let stream = handshake.await?;
24+
let stream = handshake?;
2625

2726
Ok(Self {
2827
inner: Box::new(MySqlConnectionInner {
@@ -187,9 +186,9 @@ impl<'a> DoHandshake<'a> {
187186
}
188187

189188
impl<'a> WithSocket for DoHandshake<'a> {
190-
type Output = BoxFuture<'a, Result<MySqlStream, Error>>;
189+
type Output = Result<MySqlStream, Error>;
191190

192-
fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
193-
Box::pin(self.do_handshake(socket))
191+
async fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
192+
self.do_handshake(socket).await
194193
}
195194
}

sqlx-mysql/src/connection/tls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub(super) async fn maybe_upgrade<S: Socket>(
9494
impl WithSocket for MapStream {
9595
type Output = MySqlStream;
9696

97-
fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
97+
async fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
9898
MySqlStream {
9999
socket: BufferedSocket::new(Box::new(socket)),
100100
server_version: self.server_version,

0 commit comments

Comments
 (0)