Skip to content

Commit

Permalink
Implement Clone for PoolOptions (#1919)
Browse files Browse the repository at this point in the history
To not break the API we need to use an Arc instead of a Box for the
callback functions. Alternatively we could require all the function to
be Clone, but that would be a breaking change.
  • Loading branch information
Thomasdezeeuw authored Jun 22, 2022
1 parent 3d4861f commit 39eadd6
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions sqlx-core/src/pool/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::pool::inner::PoolInner;
use crate::pool::Pool;
use futures_core::future::BoxFuture;
use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Configuration options for [`Pool`][super::Pool].
Expand Down Expand Up @@ -40,18 +41,19 @@ use std::time::{Duration, Instant};
/// parameter everywhere, and `Box` is in the prelude so it doesn't need to be manually imported,
/// so having the closure return `Pin<Box<dyn Future>` directly is the path of least resistance from
/// the perspectives of both API designer and consumer.
#[derive(Clone)]
pub struct PoolOptions<DB: Database> {
pub(crate) test_before_acquire: bool,
pub(crate) after_connect: Option<
Box<
Arc<
dyn Fn(&mut DB::Connection, PoolConnectionMetadata) -> BoxFuture<'_, Result<(), Error>>
+ 'static
+ Send
+ Sync,
>,
>,
pub(crate) before_acquire: Option<
Box<
Arc<
dyn Fn(
&mut DB::Connection,
PoolConnectionMetadata,
Expand All @@ -62,7 +64,7 @@ pub struct PoolOptions<DB: Database> {
>,
>,
pub(crate) after_release: Option<
Box<
Arc<
dyn Fn(
&mut DB::Connection,
PoolConnectionMetadata,
Expand Down Expand Up @@ -268,7 +270,7 @@ impl<DB: Database> PoolOptions<DB> {
/// .await?;
///
/// Ok(())
/// }))
/// }))
/// .connect("postgres:// …").await?;
/// # Ok(())
/// # }
Expand All @@ -284,7 +286,7 @@ impl<DB: Database> PoolOptions<DB> {
+ Send
+ Sync,
{
self.after_connect = Some(Box::new(callback));
self.after_connect = Some(Arc::new(callback));
self
}

Expand Down Expand Up @@ -337,7 +339,7 @@ impl<DB: Database> PoolOptions<DB> {
+ Send
+ Sync,
{
self.before_acquire = Some(Box::new(callback));
self.before_acquire = Some(Arc::new(callback));
self
}

Expand Down Expand Up @@ -394,7 +396,7 @@ impl<DB: Database> PoolOptions<DB> {
+ Send
+ Sync,
{
self.after_release = Some(Box::new(callback));
self.after_release = Some(Arc::new(callback));
self
}

Expand Down

0 comments on commit 39eadd6

Please sign in to comment.