Type parameter concretions and generics

I have the following function , which doesn't compile since PG_DB_POOL is of type bb8::Pool<PostgresConnectionManager>

async fn get_db_pool<'a, DB>(db_type: DBType) -> bb8::Pool<DB>
where
    DB: for<'b> bb8::ManageConnection<Connection = bb8::PooledConnection<'a, DB>>,

{
    match db_type {
        DBType::Oracle => todo!(),
        DBType::MSSql => todo!(),
        DBType::Postgres => {
                 PG_DB_POOL.clone()
        }
    }
}

mismatched types
expected struct Pool<DB>
found struct Pool<PostgresConnectionManager<NoTls>>

This:

fn name<Genric>() -> ThingInvolving<Generic>

Means that the caller gets to choose the type of Generic, and that your code has to be able to return whatever the caller chose.

As you pointed out, you always return something involving PostgresConnectionManager, and not something the caller chose.

You could fix the current error by replacing the generic with the one actual type you actually do return with the current snippet...


...but it looks like you're going to want to return a different DB depending on the DBType; i.e. the value of DBType should determine the type of DB. There's no way to do this without erasing the type in some way.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.