This repository has been archived on 2024-07-25. You can view files and clone it, but cannot push or open issues or pull requests.
ood_persistence/src/bb8_postgres.rs

53 lines
1.3 KiB
Rust
Raw Normal View History

2021-10-12 18:14:02 +03:00
use crate::asyn::{ConnectionClient, PersistencePool};
use crate::error;
pub use bb8::{Pool, PooledConnection};
pub use bb8_postgres::tokio_postgres;
pub use bb8_postgres::PostgresConnectionManager as Manager;
pub type NoTlsManager = Manager<tokio_postgres::NoTls>;
pub type NoTlsPersistence<'p> = Persistence<'p, NoTlsManager>;
pub type NoTlsConnection<'p> = Connection<'p, NoTlsManager>;
pub type NoTlsPool = Pool<NoTlsManager>;
pub type InnerConn<'p, M> = PooledConnection<'p, M>;
pub fn new<M>(pool: &Pool<M>) -> Persistence<M>
where
M: bb8::ManageConnection,
{
Persistence(pool)
}
#[derive(Clone)]
pub struct Persistence<'p, M: bb8::ManageConnection>(&'p Pool<M>);
#[async_trait]
impl<'p, M> PersistencePool for Persistence<'p, M>
where
M: bb8::ManageConnection + Send + Sync,
{
type Conn = Connection<'p, M>;
async fn get_connection(&self) -> error::Result<Self::Conn> {
self.0
.get()
.await
.map_err(|_| error::PersistenceError::GetConnection)
.map(Connection)
}
}
pub struct Connection<'p, M: bb8::ManageConnection>(InnerConn<'p, M>);
impl<'c, M> ConnectionClient for Connection<'c, M>
where
M: bb8::ManageConnection,
{
type InnerConn = InnerConn<'c, M>;
fn inner(&mut self) -> &mut Self::InnerConn {
&mut self.0
}
}