From 0bfd612ec0ca273d31d96dbc4f5322beb23e810e Mon Sep 17 00:00:00 2001 From: Idan Mintz Date: Fri, 4 Jul 2025 13:13:34 -0700 Subject: [PATCH] Add AsyncConnectionWrapper::into_inner Fixes #213. Adds an method which enables reuse of a wrapped async connection. --- src/async_connection_wrapper.rs | 11 +++++++++++ tests/sync_wrapper.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/async_connection_wrapper.rs b/src/async_connection_wrapper.rs index 62ce265..4e11078 100644 --- a/src/async_connection_wrapper.rs +++ b/src/async_connection_wrapper.rs @@ -123,6 +123,17 @@ mod implementation { } } + impl AsyncConnectionWrapper + where + C: crate::AsyncConnection, + { + /// Consumes the [`AsyncConnectionWrapper`] returning the wrapped inner + /// [`AsyncConnection`]. + pub fn into_inner(self) -> C { + self.inner + } + } + impl Deref for AsyncConnectionWrapper { type Target = C; diff --git a/tests/sync_wrapper.rs b/tests/sync_wrapper.rs index 791f89b..576f333 100644 --- a/tests/sync_wrapper.rs +++ b/tests/sync_wrapper.rs @@ -66,3 +66,30 @@ fn check_run_migration() { // just use `run_migrations` here because that's the easiest one without additional setup conn.run_migrations(&migrations).unwrap(); } + +#[tokio::test] +async fn test_sync_wrapper_unwrap() { + let db_url = std::env::var("DATABASE_URL").unwrap(); + + let conn = tokio::task::spawn_blocking(move || { + use diesel::RunQueryDsl; + + let mut conn = AsyncConnectionWrapper::::establish(&db_url).unwrap(); + let res = + diesel::select(1.into_sql::()).get_result::(&mut conn); + assert_eq!(Ok(1), res); + conn + }) + .await + .unwrap(); + + { + use diesel_async::RunQueryDsl; + + let mut conn = conn.into_inner(); + let res = diesel::select(1.into_sql::()) + .get_result::(&mut conn) + .await; + assert_eq!(Ok(1), res); + } +}