Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoolOptions<DB> can't be cloned #2548

Closed
Redrield opened this issue Jun 15, 2023 · 2 comments
Closed

PoolOptions<DB> can't be cloned #2548

Redrield opened this issue Jun 15, 2023 · 2 comments
Labels

Comments

@Redrield
Copy link

Bug Description

Despite the #[derive(Clone)] on PoolOptions in sqlx-core, PoolOptions cannot be cloned due to unsatisfied trait bounds on the database and database connection types.

error[E0599]: the method `clone` exists for struct `PoolOptions<Postgres>`, but its trait bounds were not satisfied
   --> src\lib.rs:106:16
    |
106 |     final_opts.clone();
    |                ^^^^^ method cannot be called on `PoolOptions<Postgres>` due to unsatisfied trait bounds
    |
   ::: C:\Users\KaitlynKenwell\.cargo\registry\src\index.crates.io-6f17d22bba15001f\sqlx-core-0.6.3\src\postgres\database.rs:11:1
    |
11  | pub struct Postgres;
    | ------------------- doesn't satisfy `Postgres: Clone`
    |
   ::: C:\Users\KaitlynKenwell\.cargo\registry\src\index.crates.io-6f17d22bba15001f\sqlx-core-0.6.3\src\pool\options.rs:45:1
    |
45  | pub struct PoolOptions<DB: Database> {
    | ------------------------------------ doesn't satisfy `PoolOptions<Postgres>: Clone`
    |
   ::: C:\Users\KaitlynKenwell\.cargo\registry\src\index.crates.io-6f17d22bba15001f\sqlx-core-0.6.3\src\postgres\connection\mod.rs:31:1
    |
31  | pub struct PgConnection {
    | ----------------------- doesn't satisfy `PgConnection: Clone`
    |
    = note: the following trait bounds were not satisfied:
            `Postgres: Clone`
            which is required by `PoolOptions<Postgres>: Clone`
            `PgConnection: Clone`
            which is required by `PoolOptions<Postgres>: Clone`

Minimal Reproduction

let opts = PgPoolOptions::default();
let opts2 = opts.clone(); // E0599

Info

  • SQLx version: 0.6.3
  • SQLx features enabled: time,uuid,postgres,runtime-tokio-rustls
  • Database server and version: n/a
  • Operating system: Windows 11
  • rustc --version:
❯ rustc -vV    
rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: x86_64-pc-windows-msvc
release: 1.70.0
LLVM version: 16.0.2
@Redrield Redrield added the bug label Jun 15, 2023
@alilleybrinker
Copy link
Contributor

I believe, looking through the source, that clone should be able to be implemented manually, as the DB parameter type itself isn't stored (so the clonability of it doesn't actually matter, although the derive thinks it does). The DB parameter is used to parameterize any parent pool, and to provide the connection type used in the relevant stored function pointers.

abonander pushed a commit that referenced this issue Jul 9, 2023
* 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
@abonander
Copy link
Collaborator

Closed by #2553

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants