Skip to content

Commit f06c4c2

Browse files
Implement Clone for PoolOptions manually (#2548) (#2553)
* Implement Clone for PoolOptions manually (#2548) Trying to derive `Clone` automatically for `PoolOptions` results in errors when `clone` is actually called. This is because the derive incorrectly determines that `Clone` is _not_ derivable due to the lack of `Clone` implementation on the `DB: Database` type parameter, even though no value of that type is actually stored in a to-be-cloned position (in fact, it's only used for the `Connection` associated type on the type parameter's `Database` trait impl). Manually implementing `Clone` side-steps this issue and insures the type is always actually cloneable. For reference: #2548 * Ran 'cargo fmt' * Simplified Arc cloning
1 parent 8b223e2 commit f06c4c2

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

sqlx-core/src/pool/options.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use std::time::{Duration, Instant};
4141
/// parameter everywhere, and `Box` is in the prelude so it doesn't need to be manually imported,
4242
/// so having the closure return `Pin<Box<dyn Future>` directly is the path of least resistance from
4343
/// the perspectives of both API designer and consumer.
44-
#[derive(Clone)]
4544
pub struct PoolOptions<DB: Database> {
4645
pub(crate) test_before_acquire: bool,
4746
pub(crate) after_connect: Option<
@@ -84,6 +83,27 @@ pub struct PoolOptions<DB: Database> {
8483
pub(crate) parent_pool: Option<Pool<DB>>,
8584
}
8685

86+
// Manually implement `Clone` to avoid a trait bound issue.
87+
//
88+
// See: https://github.com/launchbadge/sqlx/issues/2548
89+
impl<DB: Database> Clone for PoolOptions<DB> {
90+
fn clone(&self) -> Self {
91+
PoolOptions {
92+
test_before_acquire: self.test_before_acquire,
93+
after_connect: self.after_connect.clone(),
94+
before_acquire: self.before_acquire.clone(),
95+
after_release: self.after_release.clone(),
96+
max_connections: self.max_connections,
97+
acquire_timeout: self.acquire_timeout,
98+
min_connections: self.min_connections,
99+
max_lifetime: self.max_lifetime,
100+
idle_timeout: self.idle_timeout,
101+
fair: self.fair,
102+
parent_pool: self.parent_pool.as_ref().map(Pool::clone),
103+
}
104+
}
105+
}
106+
87107
/// Metadata for the connection being processed by a [`PoolOptions`] callback.
88108
#[derive(Debug)] // Don't want to commit to any other trait impls yet.
89109
#[non_exhaustive] // So we can safely add fields in the future.

0 commit comments

Comments
 (0)