简体   繁体   English

在 Rust 中关闭 PostgreSQL 客户端连接

[英]Closing a PostgreSQL Client Connection in Rust

I am new to Rust and I am using the Postgres crate.我是 Rust 的新手,我正在使用 Postgres crate。 I am trying to create a wrapper around the PostgreSQL client, and I have defined this Struct:我正在尝试围绕 PostgreSQL 客户端创建一个包装器,并且我已经定义了这个结构:

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.后来,我试图在这个结构上实现 Drop trait。 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.我不知道如何解决这个问题,任何帮助将不胜感激。

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.这等效于 Client 的 Drop 实现,只是它将遇到的任何错误返回给调用者。

The Client connection is already closed when dropped, and your inner Client will be dropped altogether with the outer one. Client连接在删除时已经关闭,您的内部Client将与外部客户端一起被删除。

Think that close takes an owned Client ( self ) which will be dropped after function call.认为close需要一个拥有的Clientself ),它将在函数调用后被丢弃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM