Skip to content

Commit

Permalink
Replace async_trait with native async fns
Browse files Browse the repository at this point in the history
  • Loading branch information
gahag-cw committed Dec 3, 2024
1 parent 58b1e6b commit 7f198df
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 34 deletions.
3 changes: 1 addition & 2 deletions bb8/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
name = "bb8"
version = "0.8.6"
edition = "2021"
rust-version = "1.70"
rust-version = "1.75"
description = "Full-featured async (tokio-based) connection pool (like r2d2)"
license = "MIT"
repository = "https://github.com/djc/bb8"
workspace = ".."
readme = "../README.md"

[dependencies]
async-trait = "0.1"
futures-util = { version = "0.3.2", default-features = false, features = ["alloc"] }
parking_lot = { version = "0.12", optional = true }
tokio = { version = "1.0", features = ["rt", "sync", "time"] }
Expand Down
20 changes: 13 additions & 7 deletions bb8/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::borrow::Cow;
use std::error;
use std::fmt;
use std::future::Future;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::time::Duration;

use async_trait::async_trait;
use futures_util::future::BoxFuture;

use crate::inner::PoolInner;
use crate::internals::Conn;
Expand Down Expand Up @@ -381,23 +382,24 @@ impl<M: ManageConnection> Builder<M> {
}

/// A trait which provides connection-specific functionality.
#[async_trait]
pub trait ManageConnection: Sized + Send + Sync + 'static {
/// The connection type this manager deals with.
type Connection: Send + 'static;
/// The error type returned by `Connection`s.
type Error: fmt::Debug + Send + 'static;

/// Attempts to create a new connection.
async fn connect(&self) -> Result<Self::Connection, Self::Error>;
fn connect(&self) -> impl Future<Output = Result<Self::Connection, Self::Error>> + Send;
/// Determines if the connection is still connected to the database.
async fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error>;
fn is_valid(
&self,
conn: &mut Self::Connection,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
/// Synchronously determine if the connection is no longer usable, if possible.
fn has_broken(&self, conn: &mut Self::Connection) -> bool;
}

/// A trait which provides functionality to initialize a connection
#[async_trait]
pub trait CustomizeConnection<C: Send + 'static, E: 'static>:
fmt::Debug + Send + Sync + 'static
{
Expand All @@ -406,8 +408,12 @@ pub trait CustomizeConnection<C: Send + 'static, E: 'static>:
///
/// The default implementation simply returns `Ok(())`. If this method returns an
/// error, it will be forwarded to the configured error sink.
async fn on_acquire(&self, _connection: &mut C) -> Result<(), E> {
Ok(())
fn on_acquire<'a, 'b, 'r>(&'a self, _connection: &'b mut C) -> BoxFuture<'r, Result<(), E>>
where
'a: 'r,
'b: 'r,
{
Box::pin(async { Ok(()) })
}
}

Expand Down
27 changes: 13 additions & 14 deletions bb8/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use std::task::Poll;
use std::time::Duration;
use std::{error, fmt};

use async_trait::async_trait;
use futures_util::future::{err, lazy, ok, pending, ready, try_join_all, FutureExt};
use futures_util::future::{err, lazy, ok, pending, ready, try_join_all, BoxFuture, FutureExt};
use futures_util::stream::{FuturesUnordered, TryStreamExt};
use tokio::sync::oneshot;
use tokio::time::{sleep, timeout};
Expand Down Expand Up @@ -43,7 +42,6 @@ impl<C> OkManager<C> {
}
}

#[async_trait]
impl<C> ManageConnection for OkManager<C>
where
C: Default + Send + Sync + 'static,
Expand Down Expand Up @@ -78,7 +76,6 @@ impl<C> NthConnectionFailManager<C> {
}
}

#[async_trait]
impl<C> ManageConnection for NthConnectionFailManager<C>
where
C: Default + Send + Sync + 'static,
Expand Down Expand Up @@ -214,7 +211,6 @@ struct BrokenConnectionManager<C> {
_c: PhantomData<C>,
}

#[async_trait]
impl<C: Default + Send + Sync + 'static> ManageConnection for BrokenConnectionManager<C> {
type Connection = C;
type Error = Error;
Expand Down Expand Up @@ -380,7 +376,6 @@ async fn test_now_invalid() {

struct Handler;

#[async_trait]
impl ManageConnection for Handler {
type Connection = FakeConnection;
type Error = Error;
Expand Down Expand Up @@ -689,7 +684,6 @@ async fn test_conns_drop_on_pool_drop() {

struct Handler;

#[async_trait]
impl ManageConnection for Handler {
type Connection = Connection;
type Error = Error;
Expand Down Expand Up @@ -741,7 +735,6 @@ async fn test_retry() {
struct Connection;
struct Handler;

#[async_trait]
impl ManageConnection for Handler {
type Connection = Connection;
type Error = Error;
Expand Down Expand Up @@ -787,7 +780,6 @@ async fn test_conn_fail_once() {
}
}

#[async_trait]
impl ManageConnection for Handler {
type Connection = Connection;
type Error = Error;
Expand Down Expand Up @@ -912,11 +904,19 @@ async fn test_customize_connection_acquire() {
count: AtomicUsize,
}

#[async_trait]
impl<E: 'static> CustomizeConnection<Connection, E> for CountingCustomizer {
async fn on_acquire(&self, connection: &mut Connection) -> Result<(), E> {
connection.custom_field = 1 + self.count.fetch_add(1, Ordering::SeqCst);
Ok(())
fn on_acquire<'a, 'b, 'r>(
&'a self,
connection: &'b mut Connection,
) -> BoxFuture<'r, Result<(), E>>
where
'a: 'r,
'b: 'r,
{
Box::pin(async move {
connection.custom_field = 1 + self.count.fetch_add(1, Ordering::SeqCst);
Ok(())
})
}
}

Expand Down Expand Up @@ -952,7 +952,6 @@ async fn test_broken_connections_dont_starve_pool() {
#[derive(Debug)]
struct Connection;

#[async_trait::async_trait]
impl bb8::ManageConnection for ConnectionManager {
type Connection = Connection;
type Error = Infallible;
Expand Down
3 changes: 1 addition & 2 deletions postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "bb8-postgres"
version = "0.8.1"
edition = "2021"
rust-version = "1.63"
rust-version = "1.75"
description = "Full-featured async (tokio-based) postgres connection pool (like r2d2)"
license = "MIT"
repository = "https://github.com/djc/bb8"
Expand All @@ -19,7 +19,6 @@ repository = "https://github.com/djc/bb8"
"with-time-0_3" = ["tokio-postgres/with-time-0_3"]

[dependencies]
async-trait = "0.1"
bb8 = { version = "0.8", path = "../bb8" }
tokio = { version = "1.0.0", features = ["rt"] }
tokio-postgres = "0.7"
Expand Down
3 changes: 0 additions & 3 deletions postgres/examples/custom_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::BTreeMap;
use std::ops::Deref;
use std::str::FromStr;

use async_trait::async_trait;
use bb8::{CustomizeConnection, Pool};
use bb8_postgres::PostgresConnectionManager;
use tokio_postgres::config::Config;
Expand Down Expand Up @@ -43,7 +42,6 @@ async fn main() {
#[derive(Debug)]
struct Customizer;

#[async_trait]
impl CustomizeConnection<CustomPostgresConnection, Error> for Customizer {
async fn on_acquire(&self, conn: &mut CustomPostgresConnection) -> Result<(), Error> {
conn.custom_state
Expand Down Expand Up @@ -96,7 +94,6 @@ where
}
}

#[async_trait]
impl<Tls> bb8::ManageConnection for CustomPostgresConnectionManager<Tls>
where
Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
Expand Down
2 changes: 0 additions & 2 deletions postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
pub use bb8;
pub use tokio_postgres;

use async_trait::async_trait;
use tokio_postgres::config::Config;
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
use tokio_postgres::{Client, Error, Socket};
Expand Down Expand Up @@ -45,7 +44,6 @@ where
}
}

#[async_trait]
impl<Tls> bb8::ManageConnection for PostgresConnectionManager<Tls>
where
Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
Expand Down
3 changes: 1 addition & 2 deletions redis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
name = "bb8-redis"
version = "0.17.0"
edition = "2021"
rust-version = "1.63"
rust-version = "1.75"
description = "Full-featured async (tokio-based) redis connection pool (like r2d2)"
license = "MIT"
repository = "https://github.com/djc/bb8"

[dependencies]
async-trait = "0.1"
bb8 = { version = "0.8", path = "../bb8" }
redis = { version = "0.27", default-features = false, features = ["tokio-comp"] }

Expand Down
2 changes: 0 additions & 2 deletions redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
pub use bb8;
pub use redis;

use async_trait::async_trait;
use redis::{aio::MultiplexedConnection, ErrorKind};
use redis::{Client, IntoConnectionInfo, RedisError};

Expand All @@ -58,7 +57,6 @@ impl RedisConnectionManager {
}
}

#[async_trait]
impl bb8::ManageConnection for RedisConnectionManager {
type Connection = MultiplexedConnection;
type Error = RedisError;
Expand Down

0 comments on commit 7f198df

Please sign in to comment.