Skip to content

Commit 38d74f7

Browse files
gahag-cwdjc
authored andcommitted
Replace async_trait with native async fns
1 parent 2bde6f9 commit 38d74f7

File tree

8 files changed

+35
-39
lines changed

8 files changed

+35
-39
lines changed

bb8/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ 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: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +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};
7+
use std::pin::Pin;
68
use std::time::Duration;
79

8-
use async_trait::async_trait;
9-
1010
use crate::inner::PoolInner;
1111
use crate::internals::Conn;
1212

@@ -381,23 +381,24 @@ impl<M: ManageConnection> Builder<M> {
381381
}
382382

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

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

399401
/// A trait which provides functionality to initialize a connection
400-
#[async_trait]
401402
pub trait CustomizeConnection<C: Send + 'static, E: 'static>:
402403
fmt::Debug + Send + Sync + 'static
403404
{
@@ -406,8 +407,11 @@ pub trait CustomizeConnection<C: Send + 'static, E: 'static>:
406407
///
407408
/// The default implementation simply returns `Ok(())`. If this method returns an
408409
/// error, it will be forwarded to the configured error sink.
409-
async fn on_acquire(&self, _connection: &mut C) -> Result<(), E> {
410-
Ok(())
410+
fn on_acquire<'a>(
411+
&'a self,
412+
_connection: &'a mut C,
413+
) -> Pin<Box<dyn Future<Output = Result<(), E>> + Send + 'a>> {
414+
Box::pin(async { Ok(()) })
411415
}
412416
}
413417

bb8/tests/test.rs

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

12-
use async_trait::async_trait;
1312
use futures_util::future::{err, lazy, ok, pending, ready, try_join_all, FutureExt};
1413
use futures_util::stream::{FuturesUnordered, TryStreamExt};
1514
use tokio::sync::oneshot;
@@ -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,15 @@ 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>(
909+
&'a self,
910+
connection: &'a mut Connection,
911+
) -> Pin<Box<dyn Future<Output = Result<(), E>> + Send + 'a>> {
912+
Box::pin(async move {
913+
connection.custom_field = 1 + self.count.fetch_add(1, Ordering::SeqCst);
914+
Ok(())
915+
})
920916
}
921917
}
922918

@@ -952,7 +948,6 @@ async fn test_broken_connections_dont_starve_pool() {
952948
#[derive(Debug)]
953949
struct Connection;
954950

955-
#[async_trait::async_trait]
956951
impl bb8::ManageConnection for ConnectionManager {
957952
type Connection = Connection;
958953
type Error = Infallible;

postgres/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::collections::BTreeMap;
2+
use std::future::Future;
23
use std::ops::Deref;
4+
use std::pin::Pin;
35
use std::str::FromStr;
46

5-
use async_trait::async_trait;
67
use bb8::{CustomizeConnection, Pool};
78
use bb8_postgres::PostgresConnectionManager;
89
use tokio_postgres::config::Config;
@@ -43,16 +44,20 @@ async fn main() {
4344
#[derive(Debug)]
4445
struct Customizer;
4546

46-
#[async_trait]
4747
impl CustomizeConnection<CustomPostgresConnection, Error> for Customizer {
48-
async fn on_acquire(&self, conn: &mut CustomPostgresConnection) -> Result<(), Error> {
49-
conn.custom_state
50-
.insert(QueryName::BasicSelect, conn.prepare("SELECT 1").await?);
51-
52-
conn.custom_state
53-
.insert(QueryName::Addition, conn.prepare("SELECT 1 + 1 + 1").await?);
54-
55-
Ok(())
48+
fn on_acquire<'a>(
49+
&'a self,
50+
conn: &'a mut CustomPostgresConnection,
51+
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>> {
52+
Box::pin(async {
53+
conn.custom_state
54+
.insert(QueryName::BasicSelect, conn.prepare("SELECT 1").await?);
55+
56+
conn.custom_state
57+
.insert(QueryName::Addition, conn.prepare("SELECT 1 + 1 + 1").await?);
58+
59+
Ok(())
60+
})
5661
}
5762
}
5863

@@ -96,7 +101,6 @@ where
96101
}
97102
}
98103

99-
#[async_trait]
100104
impl<Tls> bb8::ManageConnection for CustomPostgresConnectionManager<Tls>
101105
where
102106
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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ 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)