Skip to content

Commit 7f198df

Browse files
committed
Replace async_trait with native async fns
1 parent 58b1e6b commit 7f198df

File tree

8 files changed

+29
-34
lines changed

8 files changed

+29
-34
lines changed

bb8/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
name = "bb8"
33
version = "0.8.6"
44
edition = "2021"
5-
rust-version = "1.70"
5+
rust-version = "1.75"
66
description = "Full-featured async (tokio-based) connection pool (like r2d2)"
77
license = "MIT"
88
repository = "https://github.com/djc/bb8"
99
workspace = ".."
1010
readme = "../README.md"
1111

1212
[dependencies]
13-
async-trait = "0.1"
1413
futures-util = { version = "0.3.2", default-features = false, features = ["alloc"] }
1514
parking_lot = { version = "0.12", optional = true }
1615
tokio = { version = "1.0", features = ["rt", "sync", "time"] }

bb8/src/api.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::borrow::Cow;
22
use std::error;
33
use std::fmt;
4+
use std::future::Future;
45
use std::marker::PhantomData;
56
use std::ops::{Deref, DerefMut};
67
use std::time::Duration;
78

8-
use async_trait::async_trait;
9+
use futures_util::future::BoxFuture;
910

1011
use crate::inner::PoolInner;
1112
use crate::internals::Conn;
@@ -381,23 +382,24 @@ impl<M: ManageConnection> Builder<M> {
381382
}
382383

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

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

399402
/// A trait which provides functionality to initialize a connection
400-
#[async_trait]
401403
pub trait CustomizeConnection<C: Send + 'static, E: 'static>:
402404
fmt::Debug + Send + Sync + 'static
403405
{
@@ -406,8 +408,12 @@ pub trait CustomizeConnection<C: Send + 'static, E: 'static>:
406408
///
407409
/// The default implementation simply returns `Ok(())`. If this method returns an
408410
/// error, it will be forwarded to the configured error sink.
409-
async fn on_acquire(&self, _connection: &mut C) -> Result<(), E> {
410-
Ok(())
411+
fn on_acquire<'a, 'b, 'r>(&'a self, _connection: &'b mut C) -> BoxFuture<'r, Result<(), E>>
412+
where
413+
'a: 'r,
414+
'b: 'r,
415+
{
416+
Box::pin(async { Ok(()) })
411417
}
412418
}
413419

bb8/tests/test.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use std::task::Poll;
99
use std::time::Duration;
1010
use std::{error, fmt};
1111

12-
use async_trait::async_trait;
13-
use futures_util::future::{err, lazy, ok, pending, ready, try_join_all, FutureExt};
12+
use futures_util::future::{err, lazy, ok, pending, ready, try_join_all, BoxFuture, FutureExt};
1413
use futures_util::stream::{FuturesUnordered, TryStreamExt};
1514
use tokio::sync::oneshot;
1615
use tokio::time::{sleep, timeout};
@@ -43,7 +42,6 @@ impl<C> OkManager<C> {
4342
}
4443
}
4544

46-
#[async_trait]
4745
impl<C> ManageConnection for OkManager<C>
4846
where
4947
C: Default + Send + Sync + 'static,
@@ -78,7 +76,6 @@ impl<C> NthConnectionFailManager<C> {
7876
}
7977
}
8078

81-
#[async_trait]
8279
impl<C> ManageConnection for NthConnectionFailManager<C>
8380
where
8481
C: Default + Send + Sync + 'static,
@@ -214,7 +211,6 @@ struct BrokenConnectionManager<C> {
214211
_c: PhantomData<C>,
215212
}
216213

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

381377
struct Handler;
382378

383-
#[async_trait]
384379
impl ManageConnection for Handler {
385380
type Connection = FakeConnection;
386381
type Error = Error;
@@ -689,7 +684,6 @@ async fn test_conns_drop_on_pool_drop() {
689684

690685
struct Handler;
691686

692-
#[async_trait]
693687
impl ManageConnection for Handler {
694688
type Connection = Connection;
695689
type Error = Error;
@@ -741,7 +735,6 @@ async fn test_retry() {
741735
struct Connection;
742736
struct Handler;
743737

744-
#[async_trait]
745738
impl ManageConnection for Handler {
746739
type Connection = Connection;
747740
type Error = Error;
@@ -787,7 +780,6 @@ async fn test_conn_fail_once() {
787780
}
788781
}
789782

790-
#[async_trait]
791783
impl ManageConnection for Handler {
792784
type Connection = Connection;
793785
type Error = Error;
@@ -912,11 +904,19 @@ async fn test_customize_connection_acquire() {
912904
count: AtomicUsize,
913905
}
914906

915-
#[async_trait]
916907
impl<E: 'static> CustomizeConnection<Connection, E> for CountingCustomizer {
917-
async fn on_acquire(&self, connection: &mut Connection) -> Result<(), E> {
918-
connection.custom_field = 1 + self.count.fetch_add(1, Ordering::SeqCst);
919-
Ok(())
908+
fn on_acquire<'a, 'b, 'r>(
909+
&'a self,
910+
connection: &'b mut Connection,
911+
) -> BoxFuture<'r, Result<(), E>>
912+
where
913+
'a: 'r,
914+
'b: 'r,
915+
{
916+
Box::pin(async move {
917+
connection.custom_field = 1 + self.count.fetch_add(1, Ordering::SeqCst);
918+
Ok(())
919+
})
920920
}
921921
}
922922

@@ -952,7 +952,6 @@ async fn test_broken_connections_dont_starve_pool() {
952952
#[derive(Debug)]
953953
struct Connection;
954954

955-
#[async_trait::async_trait]
956955
impl bb8::ManageConnection for ConnectionManager {
957956
type Connection = Connection;
958957
type Error = Infallible;

postgres/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "bb8-postgres"
33
version = "0.8.1"
44
edition = "2021"
5-
rust-version = "1.63"
5+
rust-version = "1.75"
66
description = "Full-featured async (tokio-based) postgres connection pool (like r2d2)"
77
license = "MIT"
88
repository = "https://github.com/djc/bb8"
@@ -19,7 +19,6 @@ repository = "https://github.com/djc/bb8"
1919
"with-time-0_3" = ["tokio-postgres/with-time-0_3"]
2020

2121
[dependencies]
22-
async-trait = "0.1"
2322
bb8 = { version = "0.8", path = "../bb8" }
2423
tokio = { version = "1.0.0", features = ["rt"] }
2524
tokio-postgres = "0.7"

postgres/examples/custom_state.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::collections::BTreeMap;
22
use std::ops::Deref;
33
use std::str::FromStr;
44

5-
use async_trait::async_trait;
65
use bb8::{CustomizeConnection, Pool};
76
use bb8_postgres::PostgresConnectionManager;
87
use tokio_postgres::config::Config;
@@ -43,7 +42,6 @@ async fn main() {
4342
#[derive(Debug)]
4443
struct Customizer;
4544

46-
#[async_trait]
4745
impl CustomizeConnection<CustomPostgresConnection, Error> for Customizer {
4846
async fn on_acquire(&self, conn: &mut CustomPostgresConnection) -> Result<(), Error> {
4947
conn.custom_state
@@ -96,7 +94,6 @@ where
9694
}
9795
}
9896

99-
#[async_trait]
10097
impl<Tls> bb8::ManageConnection for CustomPostgresConnectionManager<Tls>
10198
where
10299
Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,

postgres/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
pub use bb8;
55
pub use tokio_postgres;
66

7-
use async_trait::async_trait;
87
use tokio_postgres::config::Config;
98
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
109
use tokio_postgres::{Client, Error, Socket};
@@ -45,7 +44,6 @@ where
4544
}
4645
}
4746

48-
#[async_trait]
4947
impl<Tls> bb8::ManageConnection for PostgresConnectionManager<Tls>
5048
where
5149
Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,

redis/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
name = "bb8-redis"
33
version = "0.17.0"
44
edition = "2021"
5-
rust-version = "1.63"
5+
rust-version = "1.75"
66
description = "Full-featured async (tokio-based) redis connection pool (like r2d2)"
77
license = "MIT"
88
repository = "https://github.com/djc/bb8"
99

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

redis/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
pub use bb8;
3939
pub use redis;
4040

41-
use async_trait::async_trait;
4241
use redis::{aio::MultiplexedConnection, ErrorKind};
4342
use redis::{Client, IntoConnectionInfo, RedisError};
4443

@@ -58,7 +57,6 @@ impl RedisConnectionManager {
5857
}
5958
}
6059

61-
#[async_trait]
6260
impl bb8::ManageConnection for RedisConnectionManager {
6361
type Connection = MultiplexedConnection;
6462
type Error = RedisError;

0 commit comments

Comments
 (0)