From 39eadd6d6db5eadfe1bc51c1e12411c31bac85df Mon Sep 17 00:00:00 2001 From: Thomas de Zeeuw Date: Wed, 22 Jun 2022 22:00:28 +0000 Subject: [PATCH] Implement Clone for PoolOptions (#1919) 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. --- sqlx-core/src/pool/options.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/sqlx-core/src/pool/options.rs b/sqlx-core/src/pool/options.rs index 3e951cb292..6cd6282bf0 100644 --- a/sqlx-core/src/pool/options.rs +++ b/sqlx-core/src/pool/options.rs @@ -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]. @@ -40,10 +41,11 @@ 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` directly is the path of least resistance from /// the perspectives of both API designer and consumer. +#[derive(Clone)] pub struct PoolOptions { pub(crate) test_before_acquire: bool, pub(crate) after_connect: Option< - Box< + Arc< dyn Fn(&mut DB::Connection, PoolConnectionMetadata) -> BoxFuture<'_, Result<(), Error>> + 'static + Send @@ -51,7 +53,7 @@ pub struct PoolOptions { >, >, pub(crate) before_acquire: Option< - Box< + Arc< dyn Fn( &mut DB::Connection, PoolConnectionMetadata, @@ -62,7 +64,7 @@ pub struct PoolOptions { >, >, pub(crate) after_release: Option< - Box< + Arc< dyn Fn( &mut DB::Connection, PoolConnectionMetadata, @@ -268,7 +270,7 @@ impl PoolOptions { /// .await?; /// /// Ok(()) - /// })) + /// })) /// .connect("postgres:// …").await?; /// # Ok(()) /// # } @@ -284,7 +286,7 @@ impl PoolOptions { + Send + Sync, { - self.after_connect = Some(Box::new(callback)); + self.after_connect = Some(Arc::new(callback)); self } @@ -337,7 +339,7 @@ impl PoolOptions { + Send + Sync, { - self.before_acquire = Some(Box::new(callback)); + self.before_acquire = Some(Arc::new(callback)); self } @@ -394,7 +396,7 @@ impl PoolOptions { + Send + Sync, { - self.after_release = Some(Box::new(callback)); + self.after_release = Some(Arc::new(callback)); self }