diff --git a/postgres/CHANGELOG.md b/postgres/CHANGELOG.md index a2bc859..50c6f35 100644 --- a/postgres/CHANGELOG.md +++ b/postgres/CHANGELOG.md @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Add back `async_trait` to the `GenericClient` trait. + The removal of `async_trait` caused some errors for code using + the generic client interface. A test was added to ensure future + removals of `async_trait` will not cause this code to break. + ## [0.13.2] - 2024-05-07 - Add WASM support diff --git a/postgres/Cargo.toml b/postgres/Cargo.toml index 5419422..45872d4 100644 --- a/postgres/Cargo.toml +++ b/postgres/Cargo.toml @@ -20,6 +20,7 @@ rt_async-std_1 = ["deadpool/rt_async-std_1"] serde = ["deadpool/serde", "dep:serde"] [dependencies] +async-trait = "0.1.80" deadpool = { path = "../", version = "0.12.0", default-features = false, features = [ "managed", ] } diff --git a/postgres/src/generic_client.rs b/postgres/src/generic_client.rs index 6895e55..96f4855 100644 --- a/postgres/src/generic_client.rs +++ b/postgres/src/generic_client.rs @@ -3,12 +3,12 @@ //! - The `client()` method is not available. //! - The `prepare_cached()` and `prepare_typed_cached()` are //! added. -use std::future::Future; - use tokio_postgres::types::{BorrowToSql, ToSql, Type}; use tokio_postgres::RowStream; use tokio_postgres::{Error, Row, Statement, ToStatement}; +use async_trait::async_trait; + use crate::{Client, ClientWrapper, Transaction}; mod private { @@ -18,22 +18,15 @@ mod private { /// A trait allowing abstraction over connections and transactions. /// /// This trait is "sealed", and cannot be implemented outside of this crate. +#[async_trait] pub trait GenericClient: Sync + private::Sealed { /// Like `Client::execute`. - fn execute( - &self, - query: &T, - params: &[&(dyn ToSql + Sync)], - ) -> impl Future> + Send + async fn execute(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result where T: ?Sized + ToStatement + Sync + Send; /// Like `Client::execute_raw`. - fn execute_raw( - &self, - statement: &T, - params: I, - ) -> impl Future> + Send + async fn execute_raw(&self, statement: &T, params: I) -> Result where T: ?Sized + ToStatement + Sync + Send, P: BorrowToSql, @@ -41,38 +34,30 @@ pub trait GenericClient: Sync + private::Sealed { I::IntoIter: ExactSizeIterator; /// Like `Client::query`. - fn query( - &self, - query: &T, - params: &[&(dyn ToSql + Sync)], - ) -> impl Future, Error>> + Send + async fn query(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result, Error> where T: ?Sized + ToStatement + Sync + Send; /// Like `Client::query_one`. - fn query_one( + async fn query_one( &self, statement: &T, params: &[&(dyn ToSql + Sync)], - ) -> impl Future> + Send + ) -> Result where T: ?Sized + ToStatement + Sync + Send; /// Like `Client::query_opt`. - fn query_opt( + async fn query_opt( &self, statement: &T, params: &[&(dyn ToSql + Sync)], - ) -> impl Future, Error>> + Send + ) -> Result, Error> where T: ?Sized + ToStatement + Sync + Send; /// Like `Client::query_raw`. - fn query_raw( - &self, - statement: &T, - params: I, - ) -> impl Future> + Send + async fn query_raw(&self, statement: &T, params: I) -> Result where T: ?Sized + ToStatement + Sync + Send, P: BorrowToSql, @@ -80,34 +65,31 @@ pub trait GenericClient: Sync + private::Sealed { I::IntoIter: ExactSizeIterator; /// Like `Client::prepare`. - fn prepare(&self, query: &str) -> impl Future> + Send; + async fn prepare(&self, query: &str) -> Result; /// Like `Client::prepare_typed`. - fn prepare_typed( + async fn prepare_typed( &self, query: &str, parameter_types: &[Type], - ) -> impl Future> + Send; + ) -> Result; /// Like [`Client::prepare_cached`]. - fn prepare_cached(&self, query: &str) -> impl Future> + Send; + async fn prepare_cached(&self, query: &str) -> Result; /// Like [`Client::prepare_typed_cached`] - fn prepare_typed_cached( - &self, - query: &str, - types: &[Type], - ) -> impl Future> + Send; + async fn prepare_typed_cached(&self, query: &str, types: &[Type]) -> Result; /// Like `Client::transaction`. - fn transaction(&mut self) -> impl Future, Error>> + Send; + async fn transaction(&mut self) -> Result, Error>; /// Like `Client::batch_execute`. - fn batch_execute(&self, query: &str) -> impl Future> + Send; + async fn batch_execute(&self, query: &str) -> Result<(), Error>; } impl private::Sealed for Client {} +#[async_trait] impl GenericClient for Client { async fn execute(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result where @@ -196,6 +178,7 @@ impl GenericClient for Client { impl private::Sealed for Transaction<'_> {} +#[async_trait] #[allow(clippy::needless_lifetimes)] impl GenericClient for Transaction<'_> { async fn execute(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result