Skip to content

Commit

Permalink
chore: remove BoxFuture's (non-breaking) (#3629)
Browse files Browse the repository at this point in the history
* chore: reduce BoxFuture's when using recursion.

* remove BoxFuture's in WithSocket

* chore: better document previous changes
  • Loading branch information
joeydewaal authored Dec 12, 2024
1 parent 42ce24d commit 1f6ce33
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 132 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions sqlx-core/src/net/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,18 @@ where
pub trait WithSocket {
type Output;

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

pub struct SocketIntoBox;

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

fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
async fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
Box::new(socket)
}
}
Expand Down Expand Up @@ -197,7 +200,7 @@ pub async fn connect_tcp<Ws: WithSocket>(
let stream = TcpStream::connect((host, port)).await?;
stream.set_nodelay(true)?;

return Ok(with_socket.with_socket(stream));
return Ok(with_socket.with_socket(stream).await);
}

#[cfg(feature = "_rt-async-std")]
Expand All @@ -217,7 +220,7 @@ pub async fn connect_tcp<Ws: WithSocket>(
Ok(s)
});
match stream {
Ok(stream) => return Ok(with_socket.with_socket(stream)),
Ok(stream) => return Ok(with_socket.with_socket(stream).await),
Err(e) => last_err = Some(e),
}
}
Expand Down Expand Up @@ -255,7 +258,7 @@ pub async fn connect_uds<P: AsRef<Path>, Ws: WithSocket>(

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

return Ok(with_socket.with_socket(stream));
return Ok(with_socket.with_socket(stream).await);
}

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

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

Ok(with_socket.with_socket(stream))
Ok(with_socket.with_socket(stream).await)
}

#[cfg(not(feature = "_rt-async-std"))]
Expand Down
8 changes: 6 additions & 2 deletions sqlx-core/src/net/tls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ where
Ws: WithSocket,
{
#[cfg(feature = "_tls-native-tls")]
return Ok(with_socket.with_socket(tls_native_tls::handshake(socket, config).await?));
return Ok(with_socket
.with_socket(tls_native_tls::handshake(socket, config).await?)
.await);

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

#[cfg(not(any(feature = "_tls-native-tls", feature = "_tls-rustls")))]
{
Expand Down
9 changes: 4 additions & 5 deletions sqlx-mysql/src/connection/establish.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use bytes::buf::Buf;
use bytes::Bytes;
use futures_core::future::BoxFuture;

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

let stream = handshake.await?;
let stream = handshake?;

Ok(Self {
inner: Box::new(MySqlConnectionInner {
Expand Down Expand Up @@ -187,9 +186,9 @@ impl<'a> DoHandshake<'a> {
}

impl<'a> WithSocket for DoHandshake<'a> {
type Output = BoxFuture<'a, Result<MySqlStream, Error>>;
type Output = Result<MySqlStream, Error>;

fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
Box::pin(self.do_handshake(socket))
async fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
self.do_handshake(socket).await
}
}
2 changes: 1 addition & 1 deletion sqlx-mysql/src/connection/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub(super) async fn maybe_upgrade<S: Socket>(
impl WithSocket for MapStream {
type Output = MySqlStream;

fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
async fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
MySqlStream {
socket: BufferedSocket::new(Box::new(socket)),
server_version: self.server_version,
Expand Down
Loading

0 comments on commit 1f6ce33

Please sign in to comment.