Skip to content

AsyncConnectionWrapper: Implement Deref<Target = C> #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/async_connection_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub use self::implementation::AsyncConnectionWrapper;

mod implementation {
use diesel::connection::{Instrumentation, SimpleConnection};
use std::ops::{Deref, DerefMut};

use super::*;

Expand All @@ -122,6 +123,20 @@ mod implementation {
}
}

impl<C, B> Deref for AsyncConnectionWrapper<C, B> {
type Target = C;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl<C, B> DerefMut for AsyncConnectionWrapper<C, B> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}

impl<C, B> diesel::connection::SimpleConnection for AsyncConnectionWrapper<C, B>
where
C: crate::SimpleAsyncConnection,
Expand Down
20 changes: 19 additions & 1 deletion tests/sync_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use diesel::migration::Migration;
use diesel::prelude::*;
use diesel::{Connection, IntoSql};
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;

#[test]
fn test_sync_wrapper() {
use diesel::RunQueryDsl;

let db_url = std::env::var("DATABASE_URL").unwrap();
let mut conn = AsyncConnectionWrapper::<crate::TestConnection>::establish(&db_url).unwrap();

Expand All @@ -12,8 +14,24 @@ fn test_sync_wrapper() {
assert_eq!(Ok(1), res);
}

#[tokio::test]
async fn test_sync_wrapper_async_query() {
use diesel_async::{AsyncConnection, RunQueryDsl};

let db_url = std::env::var("DATABASE_URL").unwrap();
let conn = crate::TestConnection::establish(&db_url).await.unwrap();
let mut conn = AsyncConnectionWrapper::<_>::from(conn);

let res = diesel::select(1.into_sql::<diesel::sql_types::Integer>())
.get_result::<i32>(&mut conn)
.await;
assert_eq!(Ok(1), res);
}

#[tokio::test]
async fn test_sync_wrapper_under_runtime() {
use diesel::RunQueryDsl;

let db_url = std::env::var("DATABASE_URL").unwrap();
tokio::task::spawn_blocking(move || {
let mut conn = AsyncConnectionWrapper::<crate::TestConnection>::establish(&db_url).unwrap();
Expand Down
Loading