Skip to content

Commit 18da19a

Browse files
authored
Merge pull request #194 from Turbo87/deref-mut
AsyncConnectionWrapper: Implement `Deref<Target = C>`
2 parents 004c3e1 + 666dec7 commit 18da19a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/async_connection_wrapper.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pub use self::implementation::AsyncConnectionWrapper;
101101

102102
mod implementation {
103103
use diesel::connection::{Instrumentation, SimpleConnection};
104+
use std::ops::{Deref, DerefMut};
104105

105106
use super::*;
106107

@@ -122,6 +123,20 @@ mod implementation {
122123
}
123124
}
124125

126+
impl<C, B> Deref for AsyncConnectionWrapper<C, B> {
127+
type Target = C;
128+
129+
fn deref(&self) -> &Self::Target {
130+
&self.inner
131+
}
132+
}
133+
134+
impl<C, B> DerefMut for AsyncConnectionWrapper<C, B> {
135+
fn deref_mut(&mut self) -> &mut Self::Target {
136+
&mut self.inner
137+
}
138+
}
139+
125140
impl<C, B> diesel::connection::SimpleConnection for AsyncConnectionWrapper<C, B>
126141
where
127142
C: crate::SimpleAsyncConnection,

tests/sync_wrapper.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use diesel::migration::Migration;
2-
use diesel::prelude::*;
2+
use diesel::{Connection, IntoSql};
33
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
44

55
#[test]
66
fn test_sync_wrapper() {
7+
use diesel::RunQueryDsl;
8+
79
let db_url = std::env::var("DATABASE_URL").unwrap();
810
let mut conn = AsyncConnectionWrapper::<crate::TestConnection>::establish(&db_url).unwrap();
911

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

17+
#[tokio::test]
18+
async fn test_sync_wrapper_async_query() {
19+
use diesel_async::{AsyncConnection, RunQueryDsl};
20+
21+
let db_url = std::env::var("DATABASE_URL").unwrap();
22+
let conn = crate::TestConnection::establish(&db_url).await.unwrap();
23+
let mut conn = AsyncConnectionWrapper::<_>::from(conn);
24+
25+
let res = diesel::select(1.into_sql::<diesel::sql_types::Integer>())
26+
.get_result::<i32>(&mut conn)
27+
.await;
28+
assert_eq!(Ok(1), res);
29+
}
30+
1531
#[tokio::test]
1632
async fn test_sync_wrapper_under_runtime() {
33+
use diesel::RunQueryDsl;
34+
1735
let db_url = std::env::var("DATABASE_URL").unwrap();
1836
tokio::task::spawn_blocking(move || {
1937
let mut conn = AsyncConnectionWrapper::<crate::TestConnection>::establish(&db_url).unwrap();

0 commit comments

Comments
 (0)