db_pool/sync/
wrapper.rs

1use std::ops::Deref;
2
3use r2d2::Pool;
4
5use super::{
6    backend::r#trait::Backend, conn_pool::SingleUseConnectionPool, db_pool::ReusableConnectionPool,
7};
8
9/// Connection pool wrapper to facilitate the use of pools in code under test and reusable pools in tests
10pub enum PoolWrapper<B: Backend> {
11    /// Connection pool used in code under test
12    Pool(Pool<B::ConnectionManager>),
13    /// Reusable connection pool used in tests
14    ReusablePool(ReusableConnectionPool<'static, B>),
15    /// Single-use connection pool used in tests
16    SingleUsePool(SingleUseConnectionPool<B>),
17}
18
19impl<B: Backend> Deref for PoolWrapper<B> {
20    type Target = Pool<B::ConnectionManager>;
21
22    fn deref(&self) -> &Self::Target {
23        match self {
24            Self::Pool(pool) => pool,
25            Self::ReusablePool(pool) => pool,
26            Self::SingleUsePool(pool) => pool,
27        }
28    }
29}
30
31impl<B: Backend> From<ReusableConnectionPool<'static, B>> for PoolWrapper<B> {
32    fn from(value: ReusableConnectionPool<'static, B>) -> Self {
33        Self::ReusablePool(value)
34    }
35}
36
37impl<B: Backend> From<SingleUseConnectionPool<B>> for PoolWrapper<B> {
38    fn from(value: SingleUseConnectionPool<B>) -> Self {
39        Self::SingleUsePool(value)
40    }
41}