Skip to content

Commit

Permalink
Add back async_trait to GenericClient interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bikeshedder committed Jun 4, 2024
1 parent 88a11a8 commit d752433
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
5 changes: 5 additions & 0 deletions postgres/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }
Expand Down
57 changes: 20 additions & 37 deletions postgres/src/generic_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -18,96 +18,78 @@ 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<T>(
&self,
query: &T,
params: &[&(dyn ToSql + Sync)],
) -> impl Future<Output = Result<u64, Error>> + Send
async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send;

/// Like `Client::execute_raw`.
fn execute_raw<P, I, T>(
&self,
statement: &T,
params: I,
) -> impl Future<Output = Result<u64, Error>> + Send
async fn execute_raw<P, I, T>(&self, statement: &T, params: I) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
P: BorrowToSql,
I: IntoIterator<Item = P> + Sync + Send,
I::IntoIter: ExactSizeIterator;

/// Like `Client::query`.
fn query<T>(
&self,
query: &T,
params: &[&(dyn ToSql + Sync)],
) -> impl Future<Output = Result<Vec<Row>, Error>> + Send
async fn query<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send;

/// Like `Client::query_one`.
fn query_one<T>(
async fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> impl Future<Output = Result<Row, Error>> + Send
) -> Result<Row, Error>
where
T: ?Sized + ToStatement + Sync + Send;

/// Like `Client::query_opt`.
fn query_opt<T>(
async fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)],
) -> impl Future<Output = Result<Option<Row>, Error>> + Send
) -> Result<Option<Row>, Error>
where
T: ?Sized + ToStatement + Sync + Send;

/// Like `Client::query_raw`.
fn query_raw<T, P, I>(
&self,
statement: &T,
params: I,
) -> impl Future<Output = Result<RowStream, Error>> + Send
async fn query_raw<T, P, I>(&self, statement: &T, params: I) -> Result<RowStream, Error>
where
T: ?Sized + ToStatement + Sync + Send,
P: BorrowToSql,
I: IntoIterator<Item = P> + Sync + Send,
I::IntoIter: ExactSizeIterator;

/// Like `Client::prepare`.
fn prepare(&self, query: &str) -> impl Future<Output = Result<Statement, Error>> + Send;
async fn prepare(&self, query: &str) -> Result<Statement, Error>;

/// Like `Client::prepare_typed`.
fn prepare_typed(
async fn prepare_typed(
&self,
query: &str,
parameter_types: &[Type],
) -> impl Future<Output = Result<Statement, Error>> + Send;
) -> Result<Statement, Error>;

/// Like [`Client::prepare_cached`].
fn prepare_cached(&self, query: &str) -> impl Future<Output = Result<Statement, Error>> + Send;
async fn prepare_cached(&self, query: &str) -> Result<Statement, Error>;

/// Like [`Client::prepare_typed_cached`]
fn prepare_typed_cached(
&self,
query: &str,
types: &[Type],
) -> impl Future<Output = Result<Statement, Error>> + Send;
async fn prepare_typed_cached(&self, query: &str, types: &[Type]) -> Result<Statement, Error>;

/// Like `Client::transaction`.
fn transaction(&mut self) -> impl Future<Output = Result<Transaction<'_>, Error>> + Send;
async fn transaction(&mut self) -> Result<Transaction<'_>, Error>;

/// Like `Client::batch_execute`.
fn batch_execute(&self, query: &str) -> impl Future<Output = Result<(), Error>> + Send;
async fn batch_execute(&self, query: &str) -> Result<(), Error>;
}

impl private::Sealed for Client {}

#[async_trait]
impl GenericClient for Client {
async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
Expand Down Expand Up @@ -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<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
Expand Down

0 comments on commit d752433

Please sign in to comment.