Skip to content

Commit 70b6bd7

Browse files
committed
Workaround for rust-lang/rust#46415
1 parent 23947aa commit 70b6bd7

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

src/conn/mod.rs

+38-27
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use mysql_common::{
1616
},
1717
};
1818

19-
use std::{fmt, mem, str::FromStr, sync::Arc};
19+
use std::{fmt, future::Future, mem, pin::Pin, str::FromStr, sync::Arc};
2020

2121
use crate::{
2222
conn::{pool::Pool, stmt_cache::StmtCache},
@@ -265,12 +265,16 @@ impl Conn {
265265
}
266266
}
267267

268-
async fn continue_auth(self) -> Result<Conn> {
269-
match self.inner.auth_plugin {
270-
AuthPlugin::MysqlNativePassword => self.continue_mysql_native_password_auth().await,
271-
AuthPlugin::CachingSha2Password => self.continue_caching_sha2_password_auth().await,
272-
_ => unreachable!(),
273-
}
268+
fn continue_auth(self) -> Pin<Box<dyn Future<Output = Result<Conn>> + Send>> {
269+
// NOTE: we need to box this since it may recurse
270+
// see https://github.com/rust-lang/rust/issues/46415#issuecomment-528099782
271+
Box::pin(async move {
272+
match self.inner.auth_plugin {
273+
AuthPlugin::MysqlNativePassword => self.continue_mysql_native_password_auth().await,
274+
AuthPlugin::CachingSha2Password => self.continue_caching_sha2_password_auth().await,
275+
_ => unreachable!(),
276+
}
277+
})
274278
}
275279

276280
async fn continue_caching_sha2_password_auth(self) -> Result<Conn> {
@@ -389,19 +393,23 @@ impl Conn {
389393
/// Returns new connection on success or self on error.
390394
///
391395
/// Won't try to reconnect if socket connection is already enforced in `Opts`.
392-
async fn reconnect_via_socket_if_needed(self) -> Result<Conn> {
393-
if let Some(socket) = self.inner.socket.as_ref() {
394-
let opts = self.inner.opts.clone();
395-
if opts.get_socket().is_none() {
396-
let mut builder = OptsBuilder::from_opts(opts);
397-
builder.socket(Some(&**socket));
398-
match Conn::new(builder).await {
399-
Ok(conn) => return Ok(conn),
400-
Err(_) => return Ok(self),
396+
fn reconnect_via_socket_if_needed(self) -> Pin<Box<dyn Future<Output = Result<Conn>> + Send>> {
397+
// NOTE: we need to box this since it may recurse
398+
// see https://github.com/rust-lang/rust/issues/46415#issuecomment-528099782
399+
Box::pin(async move {
400+
if let Some(socket) = self.inner.socket.as_ref() {
401+
let opts = self.inner.opts.clone();
402+
if opts.get_socket().is_none() {
403+
let mut builder = OptsBuilder::from_opts(opts);
404+
builder.socket(Some(&**socket));
405+
match Conn::new(builder).await {
406+
Ok(conn) => return Ok(conn),
407+
Err(_) => return Ok(self),
408+
}
401409
}
402410
}
403-
}
404-
Ok(self)
411+
Ok(self)
412+
})
405413
}
406414

407415
/// Returns future that resolves to `Conn` with socket address stored in it.
@@ -482,15 +490,18 @@ impl Conn {
482490
}
483491
}
484492

485-
async fn cleanup(self) -> Result<Conn> {
486-
// NOTE: we must box to avoid a recursive async fn
487-
if self.inner.has_result.is_some() {
488-
Box::pin(async move { self.drop_result().await?.cleanup().await }).await
489-
} else if self.inner.in_transaction {
490-
Box::pin(async move { self.rollback_transaction().await?.cleanup().await }).await
491-
} else {
492-
Ok(self)
493-
}
493+
fn cleanup(self) -> Pin<Box<dyn Future<Output = Result<Conn>> + Send>> {
494+
// NOTE: we need to box this since it may recurse
495+
// see https://github.com/rust-lang/rust/issues/46415#issuecomment-528099782
496+
Box::pin(async move {
497+
if self.inner.has_result.is_some() {
498+
self.drop_result().await?.cleanup().await
499+
} else if self.inner.in_transaction {
500+
self.rollback_transaction().await?.cleanup().await
501+
} else {
502+
Ok(self)
503+
}
504+
})
494505
}
495506
}
496507

0 commit comments

Comments
 (0)