I'm trying to make a method in a struct that interacts with a tokio_postgres pool. The code looks something like this:
pub struct DatabaseHandler<M> where M: ManageConnection {
pool: Pool<M>
}
impl<M> DatabaseHandler<M> where M: ManageConnection {
pub async fn exists(&self, contract_address: &str) -> Result<bool, DatabaseError<M::Error>> {
let conn = self.pool.get().await?;
let result = conn.query_one(
"SELECT contract_address FROM contracts WHERE contract_address = $1",
&[
&contract_address.trim_start_matches("0x")
]
).await?;
Ok(result.is_some())
}
}
The type of pool
is Pool<PostgresConnectionManager<NoTls>>
, but I'm wanting to use generics here to accept other possible types as well (for example, one that uses TLS for connections).
The problem with this implementation is when using the query_one
method (as well as other methods like transaction
), I get this error:
no method named 'query_one' found for struct 'PooledConnection<'_, M>' in the current scope E0599 method not found in 'PooledConnection<'_, M>'
I understand why I get this error, since those methods don't exist in the ManageConnection
trait. What I'm struggling with is what trait to use for the generics for these methods to be used. I tried using ManageConnection + GenericClient
, as well as just GenericClient
, but had the same problem. It also appears that the methods in GenericClient
are not public.