'Closing a PostgreSQL Client Connection in Rust
I am new to Rust and I am using the Postgres crate. I am trying to create a wrapper around the PostgreSQL client, and I have defined this Struct:
pub struct PostGresSqlClient {
user_name: String,
host_name: String,
port: String,
client: Client,
}
Later, I am trying to implement the Drop trait on this Struct. Right now, it just tries to close the connection to the Client, but later it may do different things.
impl Drop for PostGresSqlClient {
fn drop(&mut self) {
self.client.close();
}
}
However, I am running into this issue:
error[E0507]: cannot move out of `self.client` which is behind a mutable reference
--> data_lib/src/lib.rs:106:13
|
106 | self.client.close();
| ^^^^^^^^^^^ move occurs because `self.client` has type `Client`, which does not implement the `Copy` trait
I do not know how to resolve this and any help would be appreciated.
Solution 1:[1]
You do not need to do it, from the documentation:
Closes the client’s connection to the server.
This is equivalent to Client’s Drop implementation, except that it returns any error encountered to the caller.
The Client connection is already closed when dropped, and your inner Client will be dropped altogether with the outer one.
Think that close takes an owned Client (self) which will be dropped after function call.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Netwave |
