|
5 | 5 | //! * [deadpool](self::deadpool) |
6 | 6 | //! * [bb8](self::bb8) |
7 | 7 | //! * [mobc](self::mobc) |
8 | | -
|
9 | 8 | use crate::TransactionManager; |
10 | 9 | use crate::{AsyncConnection, SimpleAsyncConnection}; |
| 10 | +use futures::FutureExt; |
11 | 11 | use std::fmt; |
12 | | -use std::marker::PhantomData; |
13 | 12 | use std::ops::DerefMut; |
14 | | -use std::sync::{Arc, Mutex}; |
15 | 13 |
|
16 | 14 | #[cfg(feature = "bb8")] |
17 | 15 | pub mod bb8; |
@@ -48,18 +46,35 @@ impl std::error::Error for PoolError {} |
48 | 46 | /// * [bb8](self::bb8) |
49 | 47 | /// * [mobc](self::mobc) |
50 | 48 | pub struct AsyncDieselConnectionManager<C> { |
51 | | - // use arc/mutex here to make `C` always `Send` + `Sync` |
52 | | - // so that this type is send + sync |
53 | | - p: PhantomData<Arc<Mutex<C>>>, |
| 49 | + setup: |
| 50 | + Box<dyn Fn(&str) -> futures::future::BoxFuture<diesel::ConnectionResult<C>> + Send + Sync>, |
54 | 51 | connection_url: String, |
55 | 52 | } |
56 | 53 |
|
57 | 54 | impl<C> AsyncDieselConnectionManager<C> { |
58 | 55 | /// Returns a new connection manager, |
59 | 56 | /// which establishes connections to the given database URL. |
60 | | - pub fn new(connection_url: impl Into<String>) -> Self { |
| 57 | + pub fn new(connection_url: impl Into<String>) -> Self |
| 58 | + where |
| 59 | + C: AsyncConnection + 'static, |
| 60 | + { |
| 61 | + Self::new_with_setup(connection_url, |url| C::establish(url).boxed()) |
| 62 | + } |
| 63 | + |
| 64 | + /// Construct a new connection manger |
| 65 | + /// with a custom setup procedure |
| 66 | + /// |
| 67 | + /// This can be used to for example establish a SSL secured |
| 68 | + /// postgres connection |
| 69 | + pub fn new_with_setup( |
| 70 | + connection_url: impl Into<String>, |
| 71 | + setup: impl Fn(&str) -> futures::future::BoxFuture<diesel::ConnectionResult<C>> |
| 72 | + + Send |
| 73 | + + Sync |
| 74 | + + 'static, |
| 75 | + ) -> Self { |
61 | 76 | Self { |
62 | | - p: PhantomData, |
| 77 | + setup: Box::new(setup), |
63 | 78 | connection_url: connection_url.into(), |
64 | 79 | } |
65 | 80 | } |
|
0 commit comments